Script Format Question

Hi All,

When somebody does this (script below) … what part of the code is part of the if condition? I normally place a brackets around my code so I know what part counts, but this does not have any, so is it (by default) a first line that comes after only?

ie.

if (!GetIsLocationValid(lDestination))
{
CalcSafeLocation(OBJECT_SELF, lDestination, 2.0, FALSE, FALSE);
}

etc.

Furthermore, I would have thought one needed to put a variable in front of CalcSafeLocation … ?

Thanks in advance.

if (!GetIsLocationValid(lDestination))
					CalcSafeLocation(OBJECT_SELF, lDestination, 2.0, FALSE, FALSE);
				float fDelay = 2.0;
				ClearAllActions(TRUE);
		        AssignCommand(OBJECT_SELF, ActionJumpToLocation(lDestination));		
				effect eVis = EffectNWN2SpecialEffectFile("fx_global_earth_elemental_arise.sef");
				DelayCommand(0.1, ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, OBJECT_SELF));
				DelayCommand(0.5, SetScriptHidden(OBJECT_SELF, 0, 0));
				DelayCommand(0.5, PlayCustomAnimationWrapper(OBJECT_SELF, "Appear", 0));
				DelayCommand(0.51, ActionWait(1.48));
				DelayCommand(1.0, RemoveEffectsFromSpell(OBJECT_SELF, 100));
				DelayCommand(1.1, SetPlotFlag(OBJECT_SELF, 0));
				DelayCommand(1.9, PlayCustomAnimationWrapper(OBJECT_SELF, "%", 1));

yes, it usually scopes only the next line

if (!GetIsLocationValid(lDestination))
    lDestination = CalcSafeLocation(OBJECT_SELF, lDestination, 2.0, FALSE, FALSE);

but it gets confusing

if ()
if ()
for ()
else if ()

==

if ()
{
    if ()
    {
        for ()
        {
        }
    }
    else if ()
    {
    }
}

(i think… or it could be)

if ()
{
    if ()
    {
        for ()
        {
        }
    }
}
else if ()
{
}

 
 
( ps. ‘fDelay’ is not used )

1 Like

Hi KevL,

Yes, there does appear to be quite a bit of “iffy” code in these scripts I am looking through … ankheg from download. I’m guessing it was originally for NWN1 perhaps? I normally have to rewrite a script to understand what someone is trying to do. :wink:

So thank you for the explanation. I will add my brackets for clarity and then decide if I am using them or reworking my own version AI. I like some aspects, but much of it appears to be unnecessary.

Cheers, Lance.

1 Like

Don’t know if NWN or NWN2 is relevant. I think we use the same style but what I look here is quite basic. It’s the same as if I write

if condition
expression
if condition 2
expression 2

You don’t need braces when you only have one expression. I hate it though when people do it the way you show. There should at least be an empty line after the expression.

Yes I think the scope of an ‘if’ statement without brackets is closed by the next semi-colon.

@Lance_Botelle Try my Basics series of tutorials. Yes I know that they are written for NwN 1/NwN EE, but the vast majority of what I have written in them transfers over to NwN 2. What you are asking about here is definitely covered in two of them (in spite of my attempt to not repeat myself :grin:).

TR’s Basics - Variables, Types and Functions (TBC)
TR’s Basics - Decisions
TR’s Basics - Boolean Algebra
TR’s Basics - Mostly Operators

@kevL_s Thanks once again for proof-reading them. Any chance you could create a document that covers what is different in NwN2 compared to what I wrote for NwN 1/EE?

TR

2 Likes

Hi TR,

Well, I do have some Tutorials in my library and your ones may well be among them, so I may have this info already. Sometimes, though, I find it easier/quicker to just ask and get a response or clarification. That said, maybe I can glance over some of these to get ahead.

Thanks, Lance.

hey @Tarot_Redhand

not likely tbh.

The differences in grammar/syntax between 1 and 2 could i think be covered in a single paragraph appendix … the only thing that springs to mind is the quirky bitwise NOT (complement) operator in 1, that was changed to standard c-like expectations in 2.

There might also be some differences between the bitwise shift operations …

Other than that I think all the operator precedence, bracketing formalisms, yada yada is identical.

 
am sure there’s a thread we had about some of these things ( 1…3 yr ago ) that mentions the quirky ~ operator and its workaround … but can’t find it …

/iirc

1 Like

It is good practice to use brackets so that bugs are not introduced when adding lines after the initial design. Also makes it easy to insert debugging lines and not introduce bugs.

2 Likes