Help with global var (SOLVED ty guys)

Started by proskiier, Fri 18/01/2008 21:42:07

Previous topic - Next topic

proskiier

I checked around the help files for some help with my issue and i kinda think i need a global variable but am not so sure how to do it...


I have a character who when you talk to him will need to say/do different things depending on a hotspot in the room he is located in.

Code: ags

function character17_a() {
  // script for Character 17 (Sad Tree): Talk to character

if (fixarm == false) {
cSadtree.Say ("One of the bushes in crazy town stole my brother's arm.");
cSadtree.Say ("Go find his arm and give it back to him and I will give you this egg I found.");
}
else {
cSadtree.Say ("Thanks for helping my brother.");
cSadtree.Say ("Here is your reward as I promised.");
object[0].Visible = false;
PlaySound(2);
cEgo.AddInventory(iEasterEggTv);
}  
}



his brother is a hotspot in the same room that he is in.
and here is his code

Code: ags

// room script file
bool fixarm = false;

#sectionstart hotspot1_a  // DO NOT EDIT OR REMOVE THIS LINE
function hotspot1_a() {
  // script for Hotspot 1 (Sad Tree): Use inventory on hotspot
  
if (cEgo.ActiveInventory == iBranch) {
Display ("You throw the branch to the tree and he puts it on himself.");
object[1].Visible = true;
cEgo.LoseInventory(iBranch);
fixarm = true;
} 
}
#sectionend hotspot1_a  // DO NOT EDIT OR REMOVE THIS LINE



My assumption is that the fixarm wont work on the character because it is only defined in the room code, even though the character is in the same room, so I'm kinda stuck on what to do here...


any help is greatly appreciated. Thanks guys.

NiksterG

I thik you'd have to make fixarm a global variable, like you said. It pretty simple: just take

Code: ags
bool fixarm = false;


and put it at the top of your global script. Then write

Code: ags
export fixarm;


in the global script, and put

Code: ags
import bool fixarm;


at the top of the global script header. That way every script has access to the fixarm variable, including the global and room scripts.
Check out my games! (Not all made with AGS)

Khris

First, to answer your question:
-Move the declaration ("bool fixarm = false;") to the global script, directly above the character17_a function. Then put "export fixarm;" directly below it. (It's actually enough to write "bool fixarm;"; bools are initialized to false if no value is given.)
-The last step to make the var global is to import it: Open the script header, then add this line: "import bool fixarm;"

There's a small bug in the Sad Tree's code:
After the arm is fixed, it's possible to get another iEasterEggTv, and another one, and another one, and so on. That's easily fixed by using an int and setting it to 2:

Code: ags
int fixarm;   // 0 by default
export fixarm;

function character17_a() {
  // script for Character 17 (Sad Tree): Talk to character

  if (fixarm == 0) {
    cSadtree.Say ("One of the bushes in crazy town stole my brother's arm.");
    cSadtree.Say ("Go find his arm and give it back to him and I will give you this egg I found.");
  }
  else if (fixarm == 1) {
    cSadtree.Say ("Thanks for helping my brother.");
    cSadtree.Say ("Here is your reward as I promised.");
    object[0].Visible = false;
    PlaySound(2);
    cEgo.AddInventory(iEasterEggTv);
    fixarm = 2;
  }
  else cSadtree.Say("Thanks again.");
}


In the room script, set fixarm to 1 instead of true.
Since fixarm is an int now, the line in the script header must read: "import int fixarm;"

SMF spam blocked by CleanTalk