Author Topic: Global Variables in a Mod  (Read 1227 times)

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #15 on: October 30, 2013, 07:27:09 am »


               What about creating a faction just to use it as a variable? You could set it from different places, couldn't you, and make a defacto global variable?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Global Variables in a Mod
« Reply #16 on: November 01, 2013, 01:29:19 am »


               I have no Idea what you are saying.  Are you saying to use it as an Int that can only go from 0 -100.
               
               

               
            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #17 on: November 01, 2013, 08:09:45 am »


               Yeah that's what I meant, but I was thinking about it and I remembered how complicated it is to change faction relationships. I think I'm going to try the nondrop item.
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Global Variables in a Mod
« Reply #18 on: November 01, 2013, 10:22:08 am »


               Local variables works as global variables in the sense you mean

why making workaround for something that works?
               
               

               
            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #19 on: November 01, 2013, 07:03:45 pm »


               I'm trying to have a player only speak a line of dialogue only if a Beast is alive, so I'm trying to declare the variable iBeast in the OnModuleLoad and setting it to 1--the "alive" setting--and then setting to 0 in the critter's OnDeath script. Compiles, but doesn't work.
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Global Variables in a Mod
« Reply #20 on: November 02, 2013, 12:38:14 am »


               Because that's a local variable within the script, not the whole module.  You'd want to put this in the OnModuleLoad:

SetLocalInt(OBJECT_SELF, "beastalive", 1);

where OBJECT_SELF is the module.

Then in the OnDeath put:

SetLocalInt(GetModule(), "beastalive", 0);

You could also technically put

DeleteLocalInt(GetModule(), "beastalive");

since that effectively sets it to 0.

Then, in the conversation itself, you'd need something like

if (GetLocalInt(GetModule(), "beastalive")) return TRUE;

where "TRUE" indicates the beast is still alive since the int is still set to 1 (would be 0 if beast was dead).
               
               

               


                     Modifié par MagicalMaster, 02 novembre 2013 - 12:39 .
                     
                  


            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #21 on: November 02, 2013, 03:52:38 am »


               The last line won't compile, it is giving me: INVALID DECLARATION TYPE.
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Global Variables in a Mod
« Reply #22 on: November 02, 2013, 04:01:58 am »


               Assuming it's for a conversation and this script controls what the PC is allowed to say, the whole script should look like:

int StartingConditional()
{
    return (GetLocalInt(GetModule(), "beastalive"));
}

Simplified it a bit more.
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Global Variables in a Mod
« Reply #23 on: November 02, 2013, 04:57:04 am »


               

Groove Widdit wrote...

I'm trying to have a player only speak a line of dialogue only if a Beast is alive, so I'm trying to declare the variable iBeast in the OnModuleLoad and setting it to 1--the "alive" setting--and then setting to 0 in the critter's OnDeath script. Compiles, but doesn't work.

you dont need to do that, instead check if she has been killed and set she has been killed on module like MM suggest by SetLocalInt(GetModule(),"blabla",TRUE); / GetLocalInt(GetModule(),"blabla")
               
               

               
            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #24 on: November 02, 2013, 10:03:25 pm »


               I got it to work! Thanks, people. I had to change the one script to this:
int StartingConditional()
{
if (GetLocalInt(GetModule(), "iBeast")) return TRUE;
return FALSE;
}
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Global Variables in a Mod
« Reply #25 on: November 03, 2013, 12:54:39 am »


               Like I said above, you can actually simplify it to

int StartingConditional()
{
    return (GetLocalInt(GetModule(), "beastalive"));
}

Do you see why that is?
               
               

               
            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #26 on: November 05, 2013, 01:31:14 am »


               Not really. Maybe because the variable Beast = 0 when it is initialized and that is the dead setting.
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Global Variables in a Mod
« Reply #27 on: November 05, 2013, 05:52:27 am »


               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 .
                     
                  


            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #28 on: November 05, 2013, 11:34:12 pm »


               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.
               
               

               
            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
Global Variables in a Mod
« Reply #29 on: November 06, 2013, 01:04:11 am »


               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).