Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: police brutality on Wed 02/05/2007 08:49:48

Title: counter not working.
Post by: police brutality on Wed 02/05/2007 08:49:48
In the global script, at the top.:

int my_counter5;

in a room at the end of a interaction (that does work):

my_counter5 = 5;

in another room:

if  ( my_counter5 == 5 ) { bunch of stuff

and

if  ( my_counter5 == 0 ) { more stuff


The problem is that the game ALWAYS acts as if my_counter5 == 0. How can I change this?
Title: Re: counter not working.
Post by: Ashen on Wed 02/05/2007 10:08:40
Really obvious thing: Have you exported the int from the Global script, and imported it into the Script Header?
Title: Re: counter not working.
Post by: police brutality on Wed 02/05/2007 10:46:16
import / export?? what's that???
Title: Re: counter not working.
Post by: Ashen on Wed 02/05/2007 10:55:31
I'll take that as a 'No', then...
Read the BFAQ (http://americangirlscouts.org/agswiki/Scripting%2C_Code_%26_Interaction#Working_with_variables:_the_basics) (which you should have done already), it's explained there.

The thing that confuses me, though, is why trying to access the variables in Room scripts doesn't cause an error, if you haven't made them Global. Are you re-declaring my_counter5 in each room?
Title: Re: counter not working.
Post by: police brutality on Wed 02/05/2007 11:10:46
thanks for the link!

and no I didn't add anything, or maybe I did, can't remember, it's 7 am and I've been up since sunday night.
Title: Re: counter not working.
Post by: Khris on Wed 02/05/2007 11:21:33
If you put the declaration into the global script header, you won't get an error even without im-/exporting it. This'll create an instance of the variable for every room, though, and changing it in one room will leave the others set to 0.

This is probably what you did.

To get one var valid in every room, use
int my_counter5;
export my_counter5;
at the top of the global script, outside any function.
Then put import int my_counter5; into the script header.

Note that you won't need the variable type (or []-stuff with arrays) when exporting it, while you'll need type/[] when importing the var.
Title: Re: counter not working.
Post by: Ashen on Wed 02/05/2007 12:18:58
Quote from: KhrisMUC on Wed 02/05/2007 11:21:33
If you put the declaration into the global script header, you won't get an error even without I'm-/exporting it. This'll create an instance of the variable for every room, though, and changing it in one room will leave the others set to 0.

Right, of course. I knew that. ::) Guess I'm a little sleep deprived today, as well.
Title: Re: counter not working.
Post by: police brutality on Thu 03/05/2007 03:16:03
Ok, now it's like this:

At the top of the global script:

int my_counter5;
export my_counter5;


In script header.

import int my_counter5;

In a room at the end of a interaction

my_counter5 = 5;

In another room

if  ( my_counter5 == 5 ) { something }

if  ( my_counter5 == 0 ) { something else  }

but the counter is always zero.

Title: Re: counter not working.
Post by: Gilbert on Thu 03/05/2007 04:19:49
Are you sure that the interaction did carry out so the counter was set properly to 5 ?
Add the following test line after setting it to 5 to see if it's working properly:

Display("The counter is %d", my_counter);

Also make sure that it's not reset to 0 in some other scripts.

Last but not least, make sure that there ISN'T a variable named my_counter declared in the rooms' scripts.
Title: Re: counter not working.
Post by: police brutality on Thu 03/05/2007 04:31:14
You are right, it was resetting to 0 somewhere, because I changed the counter's name to "locker" in all the mentioned spots and it worked. Thanks