Page 1 of 1

Assigning an Entry in an Array Causes an Error

Posted: Sat May 07, 2016 4:10 am
by mrtriscuit
I'm new to scripting, but I think I'm getting the hang of it. There's just one weird quirk I've ran into and I can't find a solution to it anywhere. For some reason, assigning an entry in an array to a variable causes an error.

For example, below works perfectly:

Code: Select all

script 2 OPEN
{
    int myArray[5] = {5, 10, 15, 20, 25}; // Works perfectly
}
but this causes my script to terminate:

Code: Select all

script 2 OPEN
{
    int myArray[5]; // Works perfectly
    myArray[3] = 15; // Breaks my script
}
Why is that?

I'm also running an updated version of ACC in Doom Builder (compared to what came preinstalled) because ACC didn't recognize UniqueTID()

edit: I almost forgot, here's a screenshot of the error I get
Image

Re: Assigning an Entry in an Array Causes an Error

Posted: Sat May 07, 2016 4:22 am
by SwordGrunt
"All arrays must have script (New from 2.8.1), map, global or world scope (the last two are special case arrays though)."

Script arrays are not supported by Zandronum so you'll get odd results attempting to use them. Why not just declare them as map arrays, outside your script?

Perhaps if you share a bit more on what you're using the array for, I could tell you whether or not you really want the script scope on your variable. Usually that's only needed for scripts with multiple instances called (ACS_ExecuteAlways and derivatives) where each instance can use the variable without affecting the others (while I've personally never used it this way, I remember seeing a script that did, I believe it was ZDoom Wars' health bar script).

Re: Assigning an Entry in an Array Causes an Error

Posted: Sat May 07, 2016 8:50 pm
by mrtriscuit
SwordGrunt wrote:"All arrays must have script (New from 2.8.1), map, global or world scope (the last two are special case arrays though)."

Script arrays are not supported by Zandronum so you'll get odd results attempting to use them. Why not just declare them as map arrays, outside your script?

Perhaps if you share a bit more on what you're using the array for, I could tell you whether or not you really want the script scope on your variable. Usually that's only needed for scripts with multiple instances called (ACS_ExecuteAlways and derivatives) where each instance can use the variable without affecting the others (while I've personally never used it this way, I remember seeing a script that did, I believe it was ZDoom Wars' health bar script).
Alright, here's my full code. This is my first project with ACS, so it's probably nausiating to look at for anyone who's experienced with ACS lol

Code: Select all

#include "zcommon.acs"
#define maxCount 350

script 1 ENTER
{
    Sector_SetFade(2, 25, 25, 25); // spooky fog
    Sector_SetDamage(2, 20, MOD_POISON);
}

script 2 ENTER
{
    int count = 0;
    int toSpawn = 1;
    while(true)
    {
        if(count < maxCount)
        {
            count++;
            HudMessage(s:"Time until next spawn:", d:(maxCount - count)/35; HUDMSG_PLAIN, 1, CR_BLUE, 0.5, 0.0, 0);
            Delay(1);
        }
        else // NOTE: Everything commented out is broken, fix it later/never
        {
            int spawned = 0;
            // log(s:"Setting up demons array");
            // int demons[10] = {UniqueTID(), UniqueTID(), UniqueTID(), UniqueTID(), UniqueTID(), UniqueTID(), UniqueTID(), UniqueTID(), UniqueTID(), UniqueTID()};
            while(spawned < toSpawn)
            {
                // log(s:"Setting up newID");
                int newID = UniqueTID();
                spawned += SpawnSpot("Demon", 1, newID);
                // TODO: Change demon stuff
                Delay(1);
            }
            toSpawn += 1;
            count = 0;
        }
    }
}
The idea behind my mod was this; every 10 seconds a wave of demons will spawn and attack the player, kind of like Nazi Zombies.

What I was trying to do, was create a bunch of demons and delay the next wave until they're all dead. I originally tried to do it with int demons[toSpawn], but ACC thinks I'm trying to create an array with 0 entries. Then I thought "well, I don't need more than 10 demons for now, I'll just do that for now." So, after reading your comment, it makes sense why Zandronum terminated my script when declaring my array.

Thanks for the help, by the way!

Re: Assigning an Entry in an Array Causes an Error

Posted: Sat May 07, 2016 10:18 pm
by SwordGrunt
The most important thing is to change those ENTER scripts to OPEN, since enter is executed every time a player joins the game with that player as the activator, and these scripts are clearly only supposed to be called once at game start with the activator being the world (exactly what open scripts are).

So you're delaying the waves by putting 10 seconds between each, then attempting to spawn a Demon every tic on the spot of ID 1 with an unique TID (which is a ZDoom 2.7.1 function if I'm not mistaken, meaning it's not supported by Zandronum 2.1!)

Here's my version (hopefully simple to understand) of your script:

Code: Select all

#include "zcommon.acs"

#define maxcount 10                // in seconds
#define maxdemoncount 10      // number of demons to spawn
#define baseid 25                     // Demons will have TIDs of baseid, baseid+1, baseid+2... baseid+(maxdemoncount - 1)

int demoncount; // if you do not initialize a variable, it defaults to 0. This includes arrays: every value is initialized to 0

script 2 OPEN
{
    for(int count = 0; count < maxcount; count++)
    {
        HudMessage(s:"Time until next spawn: \cd", i:(maxCount - count); HUDMSG_PLAIN, 1, CR_BLUE, 0.5, 0.0, 0);
        Delay(35);
    }

    while(demoncount < maxdemoncount)
    {
        if(SpawnSpot("Demon",1,baseid + demoncount))
            demoncount++;

        Delay(10);
    }

    demoncount = 0;

    restart;
}

Re: Assigning an Entry in an Array Causes an Error

Posted: Sat May 07, 2016 11:13 pm
by mrtriscuit
SwordGrunt wrote:The most important thing is to change those ENTER scripts to OPEN, since enter is executed every time a player joins the game with that player as the activator, and these scripts are clearly only supposed to be called once at game start with the activator being the world (exactly what open scripts are).

So you're delaying the waves by putting 10 seconds between each, then attempting to spawn a Demon every tic on the spot of ID 1 with an unique TID (which is a ZDoom 2.7.1 function if I'm not mistaken, meaning it's not supported by Zandronum 2.1!)

Here's my version (hopefully simple to understand) of your script:

Code: Select all

#include "zcommon.acs"

#define maxcount 10                // in seconds
#define maxdemoncount 10      // number of demons to spawn
#define baseid 25                     // Demons will have TIDs of baseid, baseid+1, baseid+2... baseid+(maxdemoncount - 1)

int demoncount; // if you do not initialize a variable, it defaults to 0. This includes arrays: every value is initialized to 0

script 2 OPEN
{
    for(int count = 0; count < maxcount; count++)
    {
        HudMessage(s:"Time until next spawn: \cd", i:(maxCount - count); HUDMSG_PLAIN, 1, CR_BLUE, 0.5, 0.0, 0);
        Delay(35);
    }

    while(demoncount < maxdemoncount)
    {
        if(SpawnSpot("Demon",1,baseid + demoncount))
            demoncount++;

        Delay(10);
    }

    demoncount = 0;

    restart;
}
Wow, that's so much better than my script. I'll go change it from ENTER to OPEN right now. Thank you so much for the help!

Is there a reference for functions that work in Zandronum? I've been using ZDoom's wiki, but there doesn't seem to be a clear way of telling which are and aren't compatible with Zandro.

edit: also UniqueTID() seems to work when I use it

Re: Assigning an Entry in an Array Causes an Error

Posted: Sun May 08, 2016 2:52 am
by SwordGrunt
Does it return proper values in-game? I thought it was a ZDoom 2.7 function but I could be wrong.

There isn't really a reference but new ACS functions are pretty scarce so don't worry about it.

I'd recommend using fixed TID values regardless, that way you can adapt your script later to account for monsters on the field before spawning more via ThingCount, if you want to - a good example would be spawning a Cyberdemon only after all demons have been killed, instead of delaying it by a fixed time frame.

Re: Assigning an Entry in an Array Causes an Error

Posted: Mon May 09, 2016 2:43 pm
by mrtriscuit
SwordGrunt wrote:Does it return proper values in-game? I thought it was a ZDoom 2.7 function but I could be wrong.

There isn't really a reference but new ACS functions are pretty scarce so don't worry about it.

I'd recommend using fixed TID values regardless, that way you can adapt your script later to account for monsters on the field before spawning more via ThingCount, if you want to - a good example would be spawning a Cyberdemon only after all demons have been killed, instead of delaying it by a fixed time frame.
That makes sense. I probably said this like 20 times now, but thanks for the help.

Re: Assigning an Entry in an Array Causes an Error

Posted: Fri May 13, 2016 12:36 pm
by Edward-san
I'd like to clarify that UniqueTID was backported out of sequence, so it is available since 2.1.

Re: Assigning an Entry in an Array Causes an Error

Posted: Fri May 13, 2016 3:28 pm
by SwordGrunt
Edward-san wrote:I'd like to clarify that UniqueTID was backported out of sequence, so it is available since 2.1.
Thank you for clearing that up, that's really handy to know.