Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Joe on Sun 03/09/2006 12:31:38

Title: Problem with variables (SOLVED)
Post by: Joe on Sun 03/09/2006 12:31:38
Hello everyone!

I'm making a game which has a float called 'health' but I've got a problem really difficult to explain.
Let's see:

//Script header
float health= 100.00;



//In global script, at repatedly execute
lblhealth.Text=String.Format("Health=%.0f",health);


This code doesnt work very well, because if I call a function like this in the globel script:


//E.g.:At dialog request
health=health+10.00;


It works, but if I call it from room repeatedly executed. E.g.:


//script for Room: Repeatedly execute
//At attack time
health=health- 10.00;


This one isnt shown in the 'lblhealth'.

Hope you understand.
Thankyou.
Title: Re: Problem with variables
Post by: Rui 'Trovatore' Pires on Sun 03/09/2006 12:38:36
Dunno if it's related, but you should never initialize a variable in the script header

Put this at the top of your global script:

float health= 100.00;
export health;

And then, in script header, replace what you had with:

import float health;

See if that helps.
Title: Re: Problem with variables
Post by: Joe on Sun 03/09/2006 12:49:32
Yeah, thanks a lot, it works perfectly.
I had tried that before, but i had fogotten 'export health;'
Thanks again.
Title: Re: Problem with variables (SOLVED)
Post by: strazer on Sun 03/09/2006 13:31:41
Seems to be the same problem then:

Quote from: strazer on Tue 29/08/2006 15:35:58If you define variables in the script header, separate variables are created for the global script and each room script. Changing the variable's value from within a room script thus doesn't affect any of the others.

To create a global variable that you can use outside the global script, you have to define it in the global script, export it and then import it into the script header:


// global script

int catStat;
export catStat;



// main script header

import int catStat;

Title: Re: Problem with variables (SOLVED)
Post by: monkey0506 on Mon 04/09/2006 23:44:56
Hmm...perhaps (and I don't know if this is even reasonable), but maybe AGS could generate a warning if you try to compile with variables defined in script headers that a new copy will be created for each room?

It just seems that this causes a lot of confusion.
Title: Re: Problem with variables (SOLVED)
Post by: Rui 'Trovatore' Pires on Tue 05/09/2006 10:34:21
Oh, I can't think of an instance where variables defined in script headers might be useful... but it might. Hey, you never know.
Title: Re: Problem with variables (SOLVED)
Post by: strazer on Tue 05/09/2006 13:14:33
Yes, but some kind of warning wouldn't be amiss.