Page 1 of 1

Problem with array definition in script

Posted: Tue Jun 24, 2025 4:28 pm
by Ats
Hi.
I'm currently simplifying the exported text from my Sheet to my ZGE project. So I'm trying to declare arrays in script.

Instead of doing this:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZLibrary Comment="Constants" HasInitializer="1">
      <Source>
<![CDATA[
{
  Alphabet.SizeDim1 = 39;
  Alphabet[0] = "A"; Alphabet[1] = "B"; Alphabet[2] = "C"; Alphabet[3] = "D"; Alphabet[4] = "E"; Alphabet[5] = "F"; Alphabet[6] = "G"; Alphabet[7] = "H"; Alphabet[8] = "I"; Alphabet[9] = "J"; Alphabet[10] = "K"; Alphabet[11] = "L"; Alphabet[12] = "M"; Alphabet[13] = "N"; Alphabet[14] = "O"; Alphabet[15] = "P"; Alphabet[16] = "Q"; Alphabet[17] = "R"; Alphabet[18] = "S"; Alphabet[19] = "T"; Alphabet[20] = "U"; Alphabet[21] = "V"; Alphabet[22] = "W"; Alphabet[23] = "X"; Alphabet[24] = "Y"; Alphabet[25] = "Z"; Alphabet[26] = "0"; Alphabet[27] = "1"; Alphabet[28] = "2"; Alphabet[29] = "3"; Alphabet[30] = "4"; Alphabet[31] = "5"; Alphabet[32] = "6"; Alphabet[33] = "7"; Alphabet[34] = "8"; Alphabet[35] = "9"; Alphabet[36] = "-"; Alphabet[37] = "."; Alphabet[38] = " ";
}]]>
      </Source>
    </ZLibrary>
  </OnLoaded>
  <Content>
    <Array Name="Alphabet" Type="2" SizeDim1="39"/>
  </Content>
</ZApplication>
I'd like a simpler :
string[39] Alphabet = {"A","B","C",...};

But I get 'Assignment destination must be variable or array: Alphabet'

The problem is that even the example in the Help file of ZGameEditor is not working:
byte[] data = {1,2,3,4}; // initialization of 1D array of bytes

Re: Problem with array definition in script

Posted: Tue Jun 24, 2025 4:45 pm
by Kjell
Hi Ats,

That syntax can only be used to declare and initialize local arrays .. like this:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[//

int[] code = {90210, 33162};

for(int i=0; i<code.SizeDim1; i++)
{
  trace(intToStr(code[i]));
}

//

string[] name = {"Beverly Hills", "Miami"};

for(int i=0; i<name.SizeDim1; i++)
{
  trace(name[i]);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
</ZApplication>
For global arrays you might as well use the "Persistent" feature.

K

Re: Problem with array definition in script

Posted: Wed Jun 25, 2025 8:13 am
by Ats
Thanks Kjell. Now that I read your response, I feel that I already asked or saw that somewhere in the forum... :lol:
So I'll keep my array as is, since "Persistent arrays only supported for numeric data types".

Reading the Help file again, it's not clear that only the last line of the examples can't be declared in a ZLibrary, and only in a ZExpression.

Re: Problem with array definition in script

Posted: Wed Jun 25, 2025 8:48 am
by Kjell
Hi Ats,
Ats wrote: Wed Jun 25, 2025 8:13 amSo I'll keep my array as is, since "Persistent arrays only supported for numeric data types".
Perhaps your "Alphabet" example is just an example .. but in case it's a map for a font or name-entry dialog or something, you might as well store the ASCII codes instead. However, binary data in .zgeproj files is compressed using a not-so-straight-forward algorithm before being converted to a hexadecimal string :(
Ats wrote: Wed Jun 25, 2025 8:13 amReading the Help file again, it's not clear that only the last line of the examples can't be declared in a ZLibrary, and only in a ZExpression.
You mean as a global array defined in a ZLibrary, right? Since it works just fine inside a function:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZLibrary>
      <Source>
<![CDATA[//

void call()
{
  string[] names = {"Peter", "Ray", "Egon"};

  for(int i=0; i<names.SizeDim1; i++)
  {
    trace(names[i]);
  }
}]]>
      </Source>
    </ZLibrary>
    <ZExpression>
      <Expression>
<![CDATA[//

call();]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
</ZApplication>
K