array<type> arrayname[size] — what is this?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)

array<type> arrayname[size] — what is this?

Postby Jekyll Grim Payne » Wed Jun 22, 2022 9:52 am

ZScript has 3 types of declarations for what could be described as arrays in one form or another:

Code: Select allExpand view
string mystrings[3]; // a local array: fixed size, non-fixed values

array<string> mystrings; // a dynamic array: non-fixed size, non-fixed values

static const string mystrings[] = { "foo", "bar", "um" }; //a static constant array: fixed size, fixed values


However, just today I found out that this is a valid declaration:
Code: Select allExpand view
array<string> mystrings[3];


What is this? It's not local because you can't use mystrings[0] = "Foo" to set its value, but it also doesn't support Push() or anything like that.

Is this an oversight?
User avatar
Jekyll Grim Payne
 
 
 
Joined: 21 Jul 2008
Discord: Agent_Ash#4401
Github ID: jekyllgrim
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia (Modern GZDoom)

Re: array<type> arrayname[size] — what is this?

Postby phantombeta » Wed Jun 22, 2022 4:57 pm

It's simply a fixed-size array of dynamic arrays. It's the same as any other fixed-size array, except the element type is a dynamic array.
Code: Select allExpand view
class Test : Actor {
    override void PostBeginPlay () {
        Array<int> myStrings [3];

        for (int i = 0; i < 3; i++) {
            int accum = 0;
            for (int j = random (1, 5); j > 0; j--)
                myStrings [i].Push (accum++);
        }

        for (int i = 0; i < 3; i++) {
            let line = String.Format ("%d", myStrings [i] [0]);
            for (int j = 1; j < myStrings [i].Size (); j++)
                line = String.Format ("%s %d", line, myStrings [i] [j]);
            Console.Printf (line);
        }

        SetStateLabel ("Null");
    }
}
User avatar
phantombeta
Tired of being treated like trash by control freaks
 
Joined: 02 May 2013


Return to Scripting

Who is online

Users browsing this forum: TheAgauresAgain and 2 guests