What's the point of GlobalInts?

Started by Snarky, Thu 24/06/2004 21:11:41

Previous topic - Next topic

Snarky

Is there any reason at all why I should use GetGlobalInt() and SetGlobalInt() instead of just defining and importing ints in the global script and script header?

With GlobalInts it seems like it would be a huge hassle to keep track of what each one represented, and make sure not to use the same one for two different things.

Are they just a legacy feature since before AGS supported importing ints from the global script, or do they offer something more?


Moox

I use global ints, I just make a text file in notepad of what each one means tho. Everyone here uses global ints pretty much.

Scorpiorus

#2
Yeah, formerly, there was no way to export variables, so the global ints were the only way to share data between the global and room scripts.

Well, global ints is an array and may be used accordingly. We can, of course, declare our own and set functions to read/write but, nevertheless, there is one ready to be used. Furthermore, having AGS user arrays is unofficial feature.

EDIT:
To keep track of what global int stands for we can put #defined constans in the script header:

Script header:
#define GI_MONEY 0
#define GI_HEALTH 1
#define GI_ROOM_LEVER 2
..
..

Thus in script:

SetGlobalInt(GI_HEALTH, 100);
int x = GetGlobalInt(GI_MONEY);

etc.

Snarky

How do I access the GlobalInts as an array?

Or do you just mean that it's accessed by an index, so it works like an array?

Yeah, it's possible to use hashdefines to name the indices, but isn't it cleaner to just define and import your ints?

int money;
int health;
int room_lever;

Then you can write:

health = 100;
x = money;

And elegantly do stuff like:

money+=10;


Scorpiorus

#4
Quote from: Snarky on Thu 24/06/2004 22:09:05
How do I access the GlobalInts as an array?

Or do you just mean that it's accessed by an index, so it works like an array?
Yep, that's what I mean, [EDIT] it [/EDIT] comes in handy sometimes.

QuoteYeah, it's possible to use hashdefines to name the indices, but isn't it cleaner to just define and import your ints?
Sure, it's possible to do it in either way. On the other hand, with globalInts one needn't do an extra work - to export and then to import a variable. That's, of course, a little bit of work but people used to globalInts and also a person has to correctly import a variable, i.e. specify the same type as it was declared with - it may be intricate for a newbie.
GlobalInts remain not only because of the backward compatibility but also because they are used where it's impossible to access variables declared in the script. For example, you can write a global message and put @GI1@ so the token will be replaced with a value of gobalInt1, or when you want to set a variable within the dialog script: set-globalint GI VAL. It's easier than calling a dialog_request function and setting a value there.

All in all, if you are an experienced AGS scripter you would usually export all the variables you want. But still, there are situations (like with global messages) when using a globalInt is the only way out.

Goot

I haven't experimented with this but I think importing room intergers will create a separate interger for each room, with the same name. Therefore it won't really work.

Wolfgang Abenteuer

As long as you define the integer in the global script, then export it, and import it into the script header (not the header for each room), it should work just fine.Ã,  Any script, whether local or global, will be able to read from and modify that variable.

~Wolfgang

Snarky

Cheers, Scorpiorus. I guess I'll find GlobalInts come in handy when I get around to doing some global messages and dialogues.

Scorpiorus

#8
Quote from: Scorpiorus on Thu 24/06/2004 23:05:17
Quote from: Snarky on Thu 24/06/2004 22:09:05
How do I access the GlobalInts as an array?

Or do you just mean that it's accessed by an index, so it works like an array?
Yep, that's what I mean, I comes in handy sometimes.
Blimey, it's meant to be "it comes in handy"... :)

Quote from: Wolfgang Abenteuer on Fri 25/06/2004 01:34:37
As long as you define the integer in the global script, then export it, and import it into the script header (not the header for each room), it should work just fine.Ã,  Any script, whether local or global, will be able to read from and modify that variable.
Yeah, you can declare a variable with the same name in each room thus it will give you different variables or declare it in the global script and then share by exporting from the global and importing into the room script what will make them be the same variables.

Quote from: Snarky on Fri 25/06/2004 02:49:36Cheers, Scorpiorus. I guess I'll find GlobalInts come in handy when I get around to doing some global messages and dialogues.
You are welcome :)

strazer

Quoteyou can declare a variable with the same name in each room thus it will give you different variables

The same happens when you declare a variable in the script header.

Scorpiorus

Yeah, since the content of the script header is added at top of all the room scripts and the main global script, you get it like if you would put an import declaration in every room.

Another thing I forgot to mention about is when you want to pass an array of integers (by value) to a function. AGS doesn't support passing of arrays as parameters, but what you can do is fill globalInt cells with the values you want to pass in and call a function, so it can then read them etc. Some sort of a temporary buffer we have here. I, though, usually use gs_globals[] for that purpose. There are other ways possible but that may come in handy if you, for example, don't want to reveal an array (by exporting it) to room scripts or you'd like to pass an array which was declared locally to the calling function.
It is also the only way (apart from gs_globals[] or Get/SetGraphicalVariable) to access script data from a plugin due to the fact that a plugin can't access AGS script variables directly.

Hollister Man Unplugged

Thanks for the tip about @GI1@, that is another feature I'd have liked to have known about before. :)  I'll put it in the demo. ;)

SMF spam blocked by CleanTalk