Global arrays?
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.
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.
- Cutmanmike
- Posts: 11353
- Joined: Mon Oct 06, 2003 3:41 pm
- Operating System Version (Optional): Windows 10
- Location: United Kingdom
- Contact:
Global arrays?
Can someone tell me more about global arrays? The wiki mentions them in the [wiki]Arrays[/wiki] page but only mentions that they exist. How do I use them?
Re: Global arrays?
Code: Select all
#include "zcommon.acs"
global int 0: variable[]; // <-- declare them like this. Notice that they do not need an array size, because they are global variables.
global int 1: another_var[];
global int 63: the_last_global_var[];
script 1 (void) //this will set the first 8 elements of global variable 0 to 57, and global variable 1 to a random value 32 - 99
{
for(int i = 0; i < 8; i++)
{
variable[i] = 57;
another_var[i] = Random(32,99);
}
}
- Cutmanmike
- Posts: 11353
- Joined: Mon Oct 06, 2003 3:41 pm
- Operating System Version (Optional): Windows 10
- Location: United Kingdom
- Contact:
Re: Global arrays?
So assuming I was using that script, "variable" would be the same as using int variable[57]?
Re: Global arrays?
Well only map variables can be arrays, meaning you can't declare an array within a script. "variable" as used in that script, is like a normal map array. I'm setting each element of it to the value 57...
So when that script is done, you'd have:
variable[0] = 57;
variable[1] = 57;
variable[2] = 57;
variable[3] = 57;
variable[4] = 57;
variable[5] = 57;
variable[6] = 57;
variable[7] = 57;
The idea of using a global variable is that it holds it's values across the entire game. The only time it will be destroyed is if the player starts an entirely new game.
EDIT: Unless you're wondering about why you don't have to declare the array size for a global variable. I don't know why that is, but it would seem that you can have as many elements as you want.
As stupid as this looks, it actually compiles and works:
So when that script is done, you'd have:
variable[0] = 57;
variable[1] = 57;
variable[2] = 57;
variable[3] = 57;
variable[4] = 57;
variable[5] = 57;
variable[6] = 57;
variable[7] = 57;
The idea of using a global variable is that it holds it's values across the entire game. The only time it will be destroyed is if the player starts an entirely new game.
EDIT: Unless you're wondering about why you don't have to declare the array size for a global variable. I don't know why that is, but it would seem that you can have as many elements as you want.
As stupid as this looks, it actually compiles and works:
Code: Select all
#include "zcommon.acs"
global int 0: variable[];
script 1 OPEN
{
variable[320000000000000000000000000000000000000000000000000000000000000000000000000000000000] = 5;
Print(d:variable[320000000000000000000000000000000000000000000000000000000000000000000000000000000000]);
}
Last edited by Slasher on Tue May 26, 2009 4:15 am, edited 1 time in total.
- Cutmanmike
- Posts: 11353
- Joined: Mon Oct 06, 2003 3:41 pm
- Operating System Version (Optional): Windows 10
- Location: United Kingdom
- Contact:
Re: Global arrays?
Aha, this makes sense to me
. Excuse my severe lack of programming knowledge but one more question, where do you set how many elements you want? Or is that not required?

Re: Global arrays?
The edit in my above post answers that question perfectly.Cutmanmike wrote: where do you set how many elements you want? Or is that not required?
Re: Global arrays?
Map array counts toward the map variable limit, but the world and global arrays are entirely separate from the world and global variables, so they're not limited. The reason for the discrepancy is "foolishness". However, there is no limit to the size of an array.
http://forum.zdoom.org/viewtopic.php?f=3&t=20412
The limits are here:
http://mancubus.net/svn/hosted/zdoom/acc/trunk/common.h
http://forum.zdoom.org/viewtopic.php?f=3&t=20412
The limits are here:
http://mancubus.net/svn/hosted/zdoom/acc/trunk/common.h
Re: Global arrays?
I guess he might feel unsafe that global arrays cannot be declared with a specified size. If I were you, I will initialized all the needed variables first. And in fact, I did this two years ago and I had no problem with this method.Cutmanmike wrote:where do you set how many elements you want? Or is that not required?
Code: Select all
#library "Globals"
#libdefine MAXPLAYERSUPPORT 16
global int 0: Initialized;
global int 1: PlayerLevel[];
global int 2: PlayerExperience[];
Script 1 ENTER
{
if(!Initialized)
{
for(int i = 0; i < MAXPLAYERSUPPORT; i++)
{
PlayerLevel[i] = 1;
PlayerExperience[i] = 0;
}
Initialized = true;
}
}
Re: Global arrays?
AFAIK:
1. G/Zdoom only supports up to 8 players.
2. All variables are automatically initialized to 0.
1. G/Zdoom only supports up to 8 players.
2. All variables are automatically initialized to 0.
- Cutmanmike
- Posts: 11353
- Joined: Mon Oct 06, 2003 3:41 pm
- Operating System Version (Optional): Windows 10
- Location: United Kingdom
- Contact:
Re: Global arrays?
Thanks slasher, that absurd script answered my questions
I'll be needing 18 (probably more in the future) global arrays with 32 elements (max skulltag players).

I'll be needing 18 (probably more in the future) global arrays with 32 elements (max skulltag players).
Re: Global arrays?
I just checked, global arrays cannot be more than 1 dimension.
So,
doesn't work.
So,
Code: Select all
global int 0: variable[][];
Re: Global arrays?
One array per achievement, I suppose... You could also use one array per player.Cutmanmike wrote:Thanks slasher, that absurd script answered my questions![]()
I'll be needing 18 (probably more in the future) global arrays with 32 elements (max skulltag players).
Is that what you were wanting to use bitflags for?
- Cutmanmike
- Posts: 11353
- Joined: Mon Oct 06, 2003 3:41 pm
- Operating System Version (Optional): Windows 10
- Location: United Kingdom
- Contact:
Re: Global arrays?
Nice work detective. Okay I'll spill the beans. I was going to use bitflags to store the achievements one had achieved. How? Consolecommand + some random unused Cvar like ChatMacro0. Cheeky, I know. However after a bit of debating I decided against it simply because it's nice knowing when you or someone else actually pulls one of the achievements off. So you CAN collect them all in a single run (if you're a GVH God), but if you close skulltag they reset so you can collect them all again 

- Macil
- Posts: 2529
- Joined: Mon Mar 22, 2004 7:00 pm
- Preferred Pronouns: He/Him
- Location: California, USA. Previously known as "Agent ME".
- Contact:
Re: Global arrays?
Setting a max number of players isn't a good idea. Skulltag can support up to 32 or 64 players last I checked. And if a player leaves the game, and someone joins to replace them, I think they get a new higher player number (so the script would break if just a single person just rejoined enough times). Instead of defining a specific maximum number of players, here's a solution I'm using in a multiplayer-centric project:carlcyber wrote:I guess he might feel unsafe that global arrays cannot be declared with a specified size. If I were you, I will initialized all the needed variables first. And in fact, I did this two years ago and I had no problem with this method.Cutmanmike wrote:where do you set how many elements you want? Or is that not required?Spoiler: code
Code: Select all
#library "Globals"
int highestPlayerNum = 0;
global int 0: Initialized;
global int 1: PlayerLevel[];
global int 2: PlayerExperience[];
Script 1 ENTER
{
// Give script 2 a moment to run first
delay(1);
if(!Initialized)
{
for(int i = 0; i < highestPlayerNum; i++)
{
PlayerLevel[i] = 1;
PlayerExperience[i] = 0;
}
Initialized = true;
}
}
script 2 ENTER
{
if(PlayerNumber() > highestPlayerNum)
{ highestPlayerNum = PlayerNumber(); }
}
Instead of making a single script process all players, and stop, ignoring later players, it makes sense to process each player as they enter:
Code: Select all
#library "Globals"
global int 1: PlayerLevel[];
global int 2: PlayerExperience[];
Script 1 ENTER
{
PlayerLevel[PlayerNumber()] = 1;
PlayerExperience[PlayerNumber()] = 0;
}
Re: Global arrays?
@Agent ME: Thanks for pointing this out.