Aller au contenu

Photo

Global Variables in a Mod


  • Veuillez vous connecter pour répondre
33 réponses à ce sujet

#26
MagicalMaster

MagicalMaster
  • Members
  • 2 000 messages
Like I said above, you can actually simplify it to
int StartingConditional()
{
    return (GetLocalInt(GetModule(), "beastalive"));
}

Do you see why that is?

#27
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
Not really. Maybe because the variable Beast = 0 when it is initialized and that is the dead setting.

#28
MagicalMaster

MagicalMaster
  • Members
  • 2 000 messages
The default for a local variable is 0.  I admit I'm not sure if there's a difference between deleting and setting it to 0 in the fine details but from your perspective in this case they are the same.

In addition, anything NOT 0 is considered true.  So the (GetLocalInt(GetModule(), "beastalive")) part effectively gets changed into true or false and then that true or false then gets returned.

To simplify it, imagine this code.
    int x;
    x = 5;

    return x;

This will return FALSE if x is 0 and TRUE if x is anything else.

Technically 0 or 1 respectively, I believe, but that's functionally FALSE and TRUE.

Modifié par MagicalMaster, 05 novembre 2013 - 05:53 .


#29
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
The way I did it is like an if/else where it returns both values specifically. I'll take your word for it about the default value--very instructive. In C programming (I think) a bool returns true or false, and can also be read as a number. If you were strapped for memory you could even reference individual bits for whatever two values. I don't know if you could do that in the Toolset.

#30
MagicalMaster

MagicalMaster
  • Members
  • 2 000 messages
Yeah, your way works, this way is just slightly better and takes up less space/instructions.

In C there is no such thing as a Boolean, it simply checks zero (false) or not-zero (true).

#31
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
There are boolean operators in C that evaluate to true or false like == or <.

#32
WhiZard

WhiZard
  • Members
  • 1 204 messages

Groove Widdit wrote...

There are boolean operators in C that evaluate to true or false like == or <.


Yes, but in NWScript TRUE (1) and FALSE (0) are both integers.  Thus commands like if(), while(), etc. take on an integer value for their argument, checking to see if that integer is 0 or not.  Using if(x == TRUE) can be problematic as it will check specifically for the value of 1 and not all non-zero integers as would be the case with if(x).  

#33
FunkySwerve

FunkySwerve
  • Members
  • 1 308 messages

MagicalMaster wrote...

The default for a local variable is 0.  I admit I'm not sure if there's a difference between deleting and setting it to 0 in the fine details but from your perspective in this case they are the same.


There is a difference, yes. Deleting them clears them from memory. Having vars set to 0 uses memory. Easy to see if you store a bunch of variables on an item in the toolset - it also increases file size, whether set to 0 (or "") or something else.

Funky

#34
MagicalMaster

MagicalMaster
  • Members
  • 2 000 messages

FunkySwerve wrote...

There is a difference, yes. Deleting them clears them from memory. Having vars set to 0 uses memory. Easy to see if you store a bunch of variables on an item in the toolset - it also increases file size, whether set to 0 (or "") or something else.


Good to know.  How significant is the process of actually allocating the memory?  In other words, if I'm going to change a variable from 0 to 1 to 0 to 1 and do this every 30 seconds, am I better off changing the value or deleting it?