Global arrays?

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.
User avatar
Cutmanmike
Posts: 11353
Joined: Mon Oct 06, 2003 3:41 pm
Operating System Version (Optional): Windows 10
Location: United Kingdom
Contact:

Global arrays?

Post by Cutmanmike »

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?
User avatar
Slasher
Posts: 1160
Joined: Sun Mar 16, 2008 11:17 pm
Location: California

Re: Global arrays?

Post by Slasher »

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);
    }
}
They can be used like any normal variable, in any script, in any map, in any hub. They will hold their values across the entire game. There is a limit of 64 global variables, which are 0 - 63, unless this limit has increased at some point.
User avatar
Cutmanmike
Posts: 11353
Joined: Mon Oct 06, 2003 3:41 pm
Operating System Version (Optional): Windows 10
Location: United Kingdom
Contact:

Re: Global arrays?

Post by Cutmanmike »

So assuming I was using that script, "variable" would be the same as using int variable[57]?
User avatar
Slasher
Posts: 1160
Joined: Sun Mar 16, 2008 11:17 pm
Location: California

Re: Global arrays?

Post by Slasher »

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:

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.
User avatar
Cutmanmike
Posts: 11353
Joined: Mon Oct 06, 2003 3:41 pm
Operating System Version (Optional): Windows 10
Location: United Kingdom
Contact:

Re: Global arrays?

Post by Cutmanmike »

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?
User avatar
Slasher
Posts: 1160
Joined: Sun Mar 16, 2008 11:17 pm
Location: California

Re: Global arrays?

Post by Slasher »

Cutmanmike wrote: where do you set how many elements you want? Or is that not required?
The edit in my above post answers that question perfectly.
Gez
 
 
Posts: 17946
Joined: Fri Jul 06, 2007 3:22 pm

Re: Global arrays?

Post by Gez »

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
carlcyber
Posts: 163
Joined: Thu Jan 27, 2005 1:04 am

Re: Global arrays?

Post by carlcyber »

Cutmanmike wrote:where do you set how many elements you want? Or is that not required?
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.

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;
    }
} 
User avatar
Slasher
Posts: 1160
Joined: Sun Mar 16, 2008 11:17 pm
Location: California

Re: Global arrays?

Post by Slasher »

AFAIK:
1. G/Zdoom only supports up to 8 players.
2. All variables are automatically initialized to 0.
User avatar
Cutmanmike
Posts: 11353
Joined: Mon Oct 06, 2003 3:41 pm
Operating System Version (Optional): Windows 10
Location: United Kingdom
Contact:

Re: Global arrays?

Post by Cutmanmike »

Thanks slasher, that absurd script answered my questions :P

I'll be needing 18 (probably more in the future) global arrays with 32 elements (max skulltag players).
User avatar
Slasher
Posts: 1160
Joined: Sun Mar 16, 2008 11:17 pm
Location: California

Re: Global arrays?

Post by Slasher »

I just checked, global arrays cannot be more than 1 dimension.

So,

Code: Select all

global int 0: variable[][];
doesn't work.
Gez
 
 
Posts: 17946
Joined: Fri Jul 06, 2007 3:22 pm

Re: Global arrays?

Post by Gez »

Cutmanmike wrote:Thanks slasher, that absurd script answered my questions :P

I'll be needing 18 (probably more in the future) global arrays with 32 elements (max skulltag players).
One array per achievement, I suppose... You could also use one array per player.

Is that what you were wanting to use bitflags for?
User avatar
Cutmanmike
Posts: 11353
Joined: Mon Oct 06, 2003 3:41 pm
Operating System Version (Optional): Windows 10
Location: United Kingdom
Contact:

Re: Global arrays?

Post by Cutmanmike »

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 ;)
User avatar
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?

Post by Macil »

carlcyber wrote:
Cutmanmike wrote:where do you set how many elements you want? Or is that not required?
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.
Spoiler: code
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:

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();  }
}
Also, there's a bug with the above script (and the original): if a player joins in-game, then they don't have their PlayerLevel[] set. (This isn't just a Skulltag problem - in-game joining is possible in zdoom by quitting, and having more players than originally start a netgame resuming from a savegame - I've done this somewhat often when LANing and skulltag didn't quite cut it.)
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;
}
Note that the Initialized variable isn't even needed here.
carlcyber
Posts: 163
Joined: Thu Jan 27, 2005 1:04 am

Re: Global arrays?

Post by carlcyber »

@Agent ME: Thanks for pointing this out.
Locked

Return to “Editing (Archive)”