Help with Global variables

Started by jamesreg, Tue 24/12/2013 16:03:36

Previous topic - Next topic

jamesreg

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

GlaDOSik

#1
Too complitaded. Try this in your button code:

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

jamesreg

#2
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

monkey0506

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?

GlaDOSik

#4
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;

Crimson Wizard

#5
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

import int myGlobalVariable;

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

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.

GlaDOSik

Oh. It looks I have a really bad habit.

Khris

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
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.

jamesreg

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.

Khris

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().

SMF spam blocked by CleanTalk