Variable changing for no obvious reason

Started by WHAM, Mon 16/11/2009 15:00:37

Previous topic - Next topic

WHAM

Below is a code

Code: ags
function oChair_Interact()
{
  
 if (GetGlobalInt(demostatus) == 1) { 
   cEgo.Say("I don't think that's a good idea at the moment");
 } 
 
 if (GetGlobalInt(demostatus) >= 2 || GetGlobalInt(demostatus) == 0) { 
    cEgo.Walk(390, 313, eBlock, eWalkableAreas);
    cEgo.FaceLocation(cEgo.x, cEgo.y - 10, eBlock);
    cEgo.Transparency = 100;
    oChair.SetView(CHAIRS, 0, 3);
    oChair.Move(274, 331, 1, eBlock, eAnywhere); // End of animation
    
if (GetGlobalInt(democomputer) >= 2) {
    SetGlobalInt(democomputer, 3);
    cEgo.ChangeRoom(275);
} else if (GetGlobalInt(democomputer) <= 1) {
    oObject7.SetView(MONITORS, 0, 1);
    cEgo.Say("It needs a password!                    ");
    SetGlobalInt(democomputer, 1); // THIS LINE CAUSES THE PROBLEM
    Wait(40);
    oObject7.SetView(MONITORS, 0, 0);
    Wait(15);
    oChair.Move(364, 331, 1, eBlock, eAnywhere);
    oChair.SetView(CHAIRS, 0, 2);
    cEgo.Transparency = 0;
}
}
}


When I execute the script, the global variable "demostatus" is 0. When the script finishes, it has become 1.

WHY!? Is it that I cannot use global variables like this or what?
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

NsMn

I'm not sure what you mean... if the variable is defined/declare like:
int variablename;
You can just write variablename=value or if(variablename==value).

WHAM

Before I run the script, both global variables: "demostatus" and "democomputer" are zero.
As the script is run, it checks if "demostatus" is 1. If it is, cEgo says that "this is not a good idea at the moment". If it is zero, then the rest of the script runs normally. As it is run, if the "democomputer" global variable is zero, it is set to 1 (via the line: SetGlobalInt(democomputer, 1).

For some reason, when I first run the script, it runs normally, but when I try to run it again later, when the "demostatus" should still be zero, it has changed to 1 and it is done by the line commented in the code to be the cause of the problem.

I cannot understand why.
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

Gilbert

#3
Since you're still using the (obsolete by some people's standard) Global Ints, note that they're numbered. So, if demostatus and democomputer are two integer variables having the value, GlobalInt(demostatus) and GlobalInt(democomputer) refer to the same entry.
Make sure you know that GlobalInts take on an integer index.
My bet is that you just declared int demostatus; and int democomputer; for the scripts to compare, which prompted that these two variables were both initialised as zero and thus, GlobalInt(demostatus) and GlobalInt(democomputer) both point to the same entry, GlobalInt(0).

I now see your second post, so I'll elaborate more on this. Here, GlobalInt(blah) refers to a variable entry with index blah. It is NOT the variable blah. A more "modern" way to use global variables is to first do this in the global script:
Code: ags
int blah;
export blah;

and then in the script header:
Code: ags
import int blah;


Then you can access the variable blah directly by doing stuff like blah=4 and if (blah ==3) {... like NsMn mentioned. There is no need to use the lagacy GlobalInt() unless you really need them for some reasons.

WHAM

Quote from: Gilbet V7000a on Mon 16/11/2009 15:51:55
Since you're still using the (obsolete by some people's standard) Global Ints, note that they're numbered. So, if demostatus and democomputer are two integer variables having the value, GlobalInt(demostatus) and GlobalInt(democomputer) refer to the same entry.
Make sure you know that GlobalInts take on an integer index.
My bet is that you just declared int demostatus; and int democomputer; for the scripts to compare, which prompted that these two variables were both initialised as zero and thus, GlobalInt(demostatus) and GlobalInt(democomputer) both point to the same entry, GlobalInt(0).

I now see your second post, so I'll elaborate more on this. Here, GlobalInt(blah) refers to a variable entry with index blah. It is NOT the variable blah. A more "modern" way to use global variables is to first do this in the global script:
Code: ags
int blah;
export blah;

and then in the script header:
Code: ags
import int blah;


Then you can access the variable blah directly by doing stuff like blah=4 and if (blah ==3) {... like NsMn mentioned. There is no need to use the lagacy GlobalInt() unless you really need them for some reasons.

So if I do this:

Code: ags

int blah = 0;
export blah;

In the global script and then do this:

Code: ags

import int blah;
blah = 5;

in the script for room 1, it will work just fine and dandy? If I now do the import int blah in room 5 (for example) will it be 1 or 5 in that room (will the value carry from room to room without any further work)
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

NsMn

No, because the value is the one exported from the global script, so you could actually say that they are completely different integers. Though I'm not even sure you can give it a value in the global script....

RickJ

MsMn is misinformed or has noty correctly understood your question.   If you define a static variable (declaration is outside the bounds of any functions)  and export it can  be used in the Global script or any Room Script in which it has been imported.  The common practices is to put the import statement in the Script header so that it is imported in to every room.

You will be accessing the same variable so it will have the same value no matter where you access it from.

WHAM

So the only way to get an integer in room 1, alter it there, use it again in another room and then check the new value in the first room is still the global integers.

And when I create a global integer in the global variables menu, instead of giving it an "Initial value" of "0" i give it an index number of "0".

Is this somewhat correct?
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

Gilbert

Don't mix up with the "yet more modern" global variables in the editor with the legacy GlobalInt()'s. The GlobalInts are actually an old workaround to make some variables accessible from any script and they're used like how I mentioned.
I don't have time to get into the new global variable editor yet, but from the manual it seems that after you declare one such variable in that editor you can just refer to it by name directly in the scripts (like blah =4, if (blah==3){..., etc.) and you don't even need to use import and export there (I think).

Khris

#9
Exactly, the ability to create global variables in the editor was introduced to avoid the somewhat tedious import/export business.

If you declare a variable and export it (in the gs), then import it only in a few room scripts instead of all via the header, only those rooms can "see" i.e. read/change the variable. But it's still just one.

However, if you declare a variable in the header, every script will have its own instance of it (not recommended).

WHAM

Thank god I didn't have more than 1000 lines of script. I've now adopted the new global variables and removed all of the GetGlobalInts and SetGlobalInts.

Thanks for the quick answers again, people!
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

Pumaman

I just noticed that the Scripting Tutorial still uses GetGlobalInt, which is probably what is confusing some people into thinking they need to use it. I've now changed the tutorial so it doesn't do that any more.

WHAM

Quote from: Pumaman on Mon 16/11/2009 22:42:05
I just noticed that the Scripting Tutorial still uses GetGlobalInt, which is probably what is confusing some people into thinking they need to use it. I've now changed the tutorial so it doesn't do that any more.


That and obeying the forum rules. As I searched the forum for tips on how to use global variables, I seem to have forgotten to check the dates on the posts. Trying to use old style script and new style script together results in... well, this!
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

SMF spam blocked by CleanTalk