Accessing a global variable from a room script

Started by KingMick, Fri 21/10/2005 05:17:56

Previous topic - Next topic

KingMick

I am sort of new to version 2.7 and I tried something which did not work, I can't figure out why. This is a very simple problem so I'm sure the solution is something I ought to already know but I honestly can't figure it out...

anyway, here's the problem.  I created an int at the beginning of the global script, knowing I would want to use this int throughout the entire game in various rooms. The GUIs recognize this variable just fine, it would seem.

The problem came when I tried to change the int from a room script. I got an error saying "Uknown token" or something like that, indicating my variable. Apparently the room script is not aware of this variable created at the beginning of the global script.

Can someone help me out? I realize I am probably being a dunce here...

Ubel

I don't know much about this but you probably have to export the variable and import it to script header.

Gilbert

Somethign like:


In global script:
int blah;
export blah;

Then on top of the room's script where you want to reference that variable (or in the header, in case you want it accessible from all the rooms):
import int blah;

You can actually read a little bit deeper into the manual before asking, the information is located in:
Scripting --> Script Language Keywords

Akumayo

in the global script you must have:
Code: ags

int my_variable;

In the very bottom of the global script:
Code: ags

export my_variable;

In the Script Header:
Code: ags

import my variable;
"Power is not a means - it is an end."

Gilbert

Quote from: Akumayo on Sat 22/10/2005 03:02:23
In the very bottom of the global script:
Not necessarily very bottom, as long as it's below the variable's declaration it's fine. I find it more readible if it's just below the declarartion, and that's just personal preference.

Akumayo

Perhaps my version of the manual is out of date, or simply a minor correction is needed.Ã,  It states:
Quote
export
export variable [, variable ... ] ;
Declares that variable can be exported and accessed by other scripts. You must place this at the end of your global script. You can export many variables with one export line.

For example:


export my_variable;
export counter, strength;

This exports three variables - my_variable, counter and strength.

As you can see, it states to export at the end of the global script, agian, I am not saying your wrong, but that the manual maybe has a minor error.
"Power is not a means - it is an end."

Gilbert

This may not be an error, it's just a reminder to ensure things work, actually the export lines can be anywhere below the declarations.

Anyway, I had answered the post earlier already.

Akumayo

I saw that (your answering post) but only also replied because I was under the impression that export's HAD to be at the end of the script.  Thanks for sharing that extra bit about after the definitions though, that does make things easier  ;D
"Power is not a means - it is an end."

Ashen

Although I can't find the quote just now, I think Chris has said he put the 'at the bottom of the script' bit in to make sure it was done after the declaration. But, as Gilbot has said, not only does just after work, it also looks neater, and is a bit more convenient.
I know what you're thinking ... Don't think that.

Akumayo

Amen, the manual should still be updated to include this though I think.
"Power is not a means - it is an end."

Akumayo

I believe this is on topic, but a knew problem has arisin with ints for me.   In the global script I have declared an int, and exported it accordingly.  The problem is that when I attempt to compile, I get an error telling me that I cannot export an import.  This is the opposite of what the manual says, that to import a variable it must have been exported from the global script!   ???   ???  ???

I tried not exporting, and then the integer in the room is an undefined token by the way.

Ideas anyone?

-Regards, Akumayo
"Power is not a means - it is an end."

Gilbert


Akumayo

at the end of the global script (even though I do know it can be after the declarations...)

I'll but it after the declarations, just to see...

EDIT:  How strange, I've placed the exports after the declarations, rather than at the end of the script, and they work fine...Thanks Gilbot.
"Power is not a means - it is an end."

Gilbert

Maybe you put that inside a function as an accident? Like at the end of the global script:

blah bla bla...
export haha;
} <--- and you didn't notice this

In that case there should be an error.

Akumayo

I don't know, anyway, it's fixed now, whatever it was.
"Power is not a means - it is an end."

SSH

It would be nice as well if you could do it all in one line, actually, something like:

exporting int x,y;

rather than

int x,y;
export x,y;
12

KingMick

Wow, thanks for all this help, this really does make a big difference!  I am new to this export/import stuff...if I alter a variable that has been exported/imported (i.e. change its value), will it still have a global effect?  i.e. if I do this export/import thingy, will things I do to the "imported" variable have an effect on the variable at large?

The reason I am asking is, this is a variable that I will need to access from pretty much every room in the game.  As long as I put in the export in the global script and the import in each room script, will it work the way I am intending?

Ashen

You'll only need to import it once, in the Script Header, rather than into every room individually. Other than that, yes. ints that have been exported/imported this way can be accessed & changed globally.
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk