Global Vars - How do they work?

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
Tormentor667
Posts: 13556
Joined: Wed Jul 16, 2003 3:52 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 11
Graphics Processor: nVidia (Modern GZDoom)
Location: Germany
Contact:

Global Vars - How do they work?

Post by Tormentor667 »

I have a few questions concerning global variables. Here an example:

Code: Select all

global int 2:STR02; 
What is the "int 2" part all about?
User avatar
Ryan Cordell
Posts: 4349
Joined: Sun Feb 06, 2005 6:39 am
Preferred Pronouns: No Preference
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia (Modern GZDoom)
Location: Capital of Explodistan

Re: Global Vars - How do they work?

Post by Ryan Cordell »

It's just the second global integer. Calm down. =P
User avatar
Tormentor667
Posts: 13556
Joined: Wed Jul 16, 2003 3:52 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 11
Graphics Processor: nVidia (Modern GZDoom)
Location: Germany
Contact:

Re: Global Vars - How do they work?

Post by Tormentor667 »

So if I use more than one global integer, I need also a unique number for each? Beyond, if these global integers are defined in more than one map, do the numbers have to be the same? For example:

Code: Select all

global int 2:STR02;
in MAP01 and

Code: Select all

global int 2:STR02;
in MAP02?
User avatar
HotWax
Posts: 10002
Joined: Fri Jul 18, 2003 6:18 pm
Location: Idaho Falls, ID

Re: Global Vars - How do they work?

Post by HotWax »

Tormentor667 wrote:So if I use more than one global integer, I need also a unique number for each?
Yes. Since global integers have to remain persistent from one script to another, the ACS system doesn't remember the variable name -- instead, it remembers a number. Therefore, each global integer needs its own unique number to represent it.
Beyond, if these global integers are defined in more than one map, do the numbers have to be the same?
Yes. Again, this is how ACS keeps track of which global variable is which. The numbers must be consistent for it to work properly.

In fact, technically speaking the name doesn't matter at all (except within the current script). You could have "int 1:MyVar" in one script and "int 1:YourVar" in another and they would both represent the same variable. For consistency's sake, it's probably better to use the same name though...
User avatar
randi
Site Admin
Posts: 7749
Joined: Wed Jul 09, 2003 10:30 pm
Contact:

Re: Global Vars - How do they work?

Post by randi »

To emphasize HotWax's point, if you have a script like this:

Code: Select all

global int 1:ThisIsGlobal;
global int 1:ThisIsGlobal2;

script 1 open
{
    ThisIsGlobal = 1;
    ThisIsGlobal2 = 2;
    printbold(s:"ThisIsGlobal = ", i:ThisIsGlobal);
}    
(Disclaimer: I have not tried this. The compiler might prevent you from defining multiple global variables with the same number.)

This script will output the number 2, not 1, because ThisIsGlobal and ThisIsGlobal2 are the same variable. The "name" of a global variable is actually the number, but you attach a more human-readable name to it to make it easier to work with.
User avatar
Tormentor667
Posts: 13556
Joined: Wed Jul 16, 2003 3:52 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 11
Graphics Processor: nVidia (Modern GZDoom)
Location: Germany
Contact:

Re: Global Vars - How do they work?

Post by Tormentor667 »

Thx for the explanation, now I got the point :) Though, I'd suggest someone puts this in the wiki, it's quite helpful for ACS noobs like me and I am sure something like this can'T be found there yet ;)
User avatar
HotWax
Posts: 10002
Joined: Fri Jul 18, 2003 6:18 pm
Location: Idaho Falls, ID

Re: Global Vars - How do they work?

Post by HotWax »

Tormentor667 wrote:I'd suggest someone puts this in the wiki, it's quite helpful for ACS noobs like me and I am sure something like this can'T be found there yet ;)
Wow, you're right. You'd think such basic information would already be there in some form or another...

Anyway, I added a description to the [wiki]Scope[/wiki] (Badly named?) wiki page. Hopefully that's enough to avoid further confusion.
User avatar
Project Shadowcat
Posts: 9369
Joined: Thu Jul 14, 2005 8:33 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Windows 11
Graphics Processor: nVidia with Vulkan support
Location: Blacksburg, SC USA
Contact:

Re: Global Vars - How do they work?

Post by Project Shadowcat »

HotWax wrote:
Tormentor667 wrote:I'd suggest someone puts this in the wiki, it's quite helpful for ACS noobs like me and I am sure something like this can'T be found there yet ;)
Wow, you're right. You'd think such basic information would already be there in some form or another...

Anyway, I added a description to the [wiki]Scope[/wiki] (Badly named?) wiki page. Hopefully that's enough to avoid further confusion.
I had to read the older ACS Primer from a totally different source to actually get the information earlier. It really wasn't available before in the Wiki. Thanks a bunch, HotWax. :D
Locked

Return to “Editing (Archive)”