Problem with variables (SOLVED)

Started by Joe, Sun 03/09/2006 12:31:38

Previous topic - Next topic

Joe

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:
Code: ags

//Script header
float health= 100.00;


Code: ags

//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:

Code: ags

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


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

Code: ags

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


This one isnt shown in the 'lblhealth'.

Hope you understand.
Thankyou.
Copinstar © Oficial Site

Rui 'Trovatore' Pires

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.
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

Joe

Yeah, thanks a lot, it works perfectly.
I had tried that before, but i had fogotten 'export health;'
Thanks again.
Copinstar © Oficial Site

strazer

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:

Code: ags

// global script

int catStat;
export catStat;


Code: ags

// main script header

import int catStat;


monkey0506

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.

Rui 'Trovatore' Pires

Oh, I can't think of an instance where variables defined in script headers might be useful... but it might. Hey, you never know.
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

strazer

Yes, but some kind of warning wouldn't be amiss.

SMF spam blocked by CleanTalk