Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: jamesreg on Tue 24/12/2013 16:03:36

Title: Help with Global variables
Post by: jamesreg on Tue 24/12/2013 16:03:36
Ok, I have a global variable set called LibraryMode set by default to 0

I have a button called LibraryControls each time I click on the button I want it to change the number of the Global varable to something else.

I have done something wrong along the way because the first time you click it, It does indeed change from 0 to 1 like I want but each time I click it after that it does not change the numbers accordingly could someone help me figure out what im doing wrong.

Here is my code.
function LibraryMode_OnClick(GUIControl *control, MouseButton button)
{
if (LibraryControls  == 0) SetGlobalInt(LibraryControls, 1);//(sets to 1 library mode)
else
if (LibraryControls == 1) SetGlobalInt(LibraryControls, 2);  //(sets to 2 Exterior Scanner mode)
else
if (LibraryControls == 2) SetGlobalInt(LibraryControls, 3);  //(Sets to 3 Interior Scanner mode)
else
if (LibraryControls == 3) SetGlobalInt(LibraryControls, 4); //(Sets to 4 Science Reports)
else
if (LibraryControls == 4) SetGlobalInt(LibraryControls,  0); //(sets back to 0 select mode
}
Title: Re: Help with Global variables
Post by: GlaDOSik on Tue 24/12/2013 16:42:06
Too complitaded. Try this in your button code:

if (LibraryControls >= 4)
LibraryControls  = 0;
else
LibraryControls++;
Title: Re: Help with Global variables
Post by: jamesreg on Tue 24/12/2013 17:10:07
Just tried that and it did not do anything at all did not change the numbers at all
I dont know whats going on it seems that no matter what coding I use it only wants to process the first mouse click on the button
Title: Re: Help with Global variables
Post by: monkey0506 on Tue 24/12/2013 18:37:09
We really need to remove SetGlobalInt from the autocomplete list. You don't use GetGlobalInt/SetGlobalInt when working with global variables. Despite the similar naming, they're completely different things. GladOSik's code should work fine. Why are you saying that it doesn't? What results are you getting or not getting vs. what results are you expecting?
Title: Re: Help with Global variables
Post by: GlaDOSik on Tue 24/12/2013 19:13:10
Ups. Sorry, my mistake. I messed up the name of variable. It should be this way:

if (LibraryMode >= 4)
LibraryMode = 0;
else
LibraryMode ++;

Now it has to work. I recommend not to use SetGlobalInt. If you want to use global variable, just put the declaration in your script header (.ash), like this:

int myGlobalVariable;

Then, you can in your room script change the value in normal way like this:

myGlobalVariable = 5;
Title: Re: Help with Global variables
Post by: Crimson Wizard on Tue 24/12/2013 19:25:59
Quote from: GlaDOSik on Tue 24/12/2013 19:13:10
If you want to use global variable, just put the declaration in your script header (.ash), like this:

int myGlobalVariable;

No, no, no, this is not how it should be done ;). By doing this you will put DIFFERENT variable with same name into EVERY script (which won't have any connection).

Instead,
1) In header (GlobalScript.ash):
Code (ags) Select

import int myGlobalVariable;

Notice import word.
2) In actual script (GlobalScript.asc):
Code (ags) Select

int myGlobalVariable;
export myGlobalVariable;


This way your myGlobalVariable (or whichever else) variable will exist uniquely in GlobalScript, but will be seen to any other script.
Title: Re: Help with Global variables
Post by: GlaDOSik on Tue 24/12/2013 20:04:55
Oh. It looks I have a really bad habit.
Title: Re: Help with Global variables
Post by: Khris on Wed 25/12/2013 11:08:56
That's not just a bad habit, that's game-breaking.

But, I'm already having a hard time trying to understand this:

QuoteI have a button called LibraryControls

Code (ags) Select
function LibraryMode_OnClick


Also, primitive types and even pointers can be made global in the Global Variables pane, that's what it's there for.

You only need the import/export business when you want a global struct and/or array.

So:
In the Global Variables pane, create an int called LibraryMode, initial value 0.

In the button's on click function, whatever it is called, use the code GlaDOSik posted last.
Title: Re: Help with Global variables
Post by: jamesreg on Thu 26/12/2013 15:29:35
Ok got it working now, Sorry been busy with holiday stuff and couldnt get on here.
I thought you had to goto that global varibles menu and create stuff like this there.
Still confused what that section is for now. But I Got this working the way I want now.
Thanks to all of your input.
Title: Re: Help with Global variables
Post by: Khris on Thu 26/12/2013 23:30:37
Yes, you use the Global variables pane.
You did it almost right, only to set a variable, you just assign the new value directly using =, you don't use SetGlobalInt().