Array

If there is something important you think is missing in the current version of ZGameEditor then you can post a feature request here!

Moderator: Moderators

Post Reply
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Array

Post by Kjell »

:idea:

A variable type for Arrays ( similar to Camera, Bitmap etc. ) would add a great deal of flexibility to the scripting language. So for example ..

Code: Select all

// Demo function only for use with 1D arrays of type byte / int / float.

void emptyArray(Array myArray)
{
  for(int i=0; i<myArray.SizeDim1; i++)
  {
    myArray[i] = 0;
  }
}
K
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Post by Rado1 »

I agree with Kjell. Local arrays in scripts would be quite handy too. Current usage of global arrays for temporary, script-local computations breaks code encapsulation.
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Post by VilleK »

Yes I agree too, only (big) hurdle is that the compiler needs to more about of the array to verify syntax. Both the number of dimensions and type is needed.

Something like this (don't know how hard it would be to implement):
void emptyArray(Array<string>[][] myArray)

Here myArray is a 2-dimensional string array.
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

Hmm,
VilleK wrote:Yes I agree too, only (big) hurdle is that the compiler needs to more about of the array to verify syntax. Both the number of dimensions and type is needed.
Can't you do that in run-time ( in the editor ) against the information provided by the passed array component instead? Isn't this how buffer overflow checking is currently handled for arrays too?

I don't think it necessarily needs to be safe though. Could be treated similar to checking whether a model "reference" is null or not .. up to the developer. Plus that would allow you to write stuff like ..

Code: Select all

void emptyArray(Array myArray) 
{ 
  if(myArray.Dimensions)return; // This function is only for 1D arrays :-)

  switch(myArray.Type)
  {
    case 2: // String
      for(int i=0; i<myArray.SizeDim1; i++)
      {
        myArray[i] = "";
      }
      break;

    case 3: // Model
      for(int i=0; i<myArray.SizeDim1; i++)
      {
        myArray[i] = null;
      }
      break;

    default:
      for(int i=0; i<myArray.SizeDim1; i++) 
      { 
        myArray[i] = 0; 
      }
      break;
}
K
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Post by Rado1 »

Kjell, non-parametric (generic) Array type is an interesting idea, but how then to declare local arrays in expressions? Something like this?

Code: Select all

Array a;
a.Dimensions = 1;
a.SizeDim1 = 100;
a.Type = 0;

for(int i = 0; i<100;i++) a[i] = i;
Usage of Array properties would be cumbersome to declare arrays. Why not this?

Code: Select all

float a[100];

// in functions
void funct(int p1[][]){...}
Also static initialization of arrays would help... maybe.

Code: Select all

int a[] = {1,2,3,4,5};
float b[][] = {{1,2.3,5},{14,23.5,9.99}};
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

Hi Rado1,
Rado1 wrote:Kjell, non-parametric (generic) Array type is an interesting idea, but how then to declare local arrays in expressions? Something like this?
Local arrays could / should still be initialized using standard syntax. For instance .. "int myArray[8][8];"
Rado1 wrote:Also static initialization of arrays would help... maybe.
Use a persistent array for those situations. You don't want to allocate & assign & destroy that data locally each frame.

K
User avatar
Rado1
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Post by Rado1 »

Hi Kjell,
Kjell wrote:Local arrays could / should still be initialized using standard syntax. For instance .. "int myArray[8][8];"
Having two language constructs to declare arrays, one for function and another for local declaration, is maybe not very clear concept. Even if it brings some kind of flexibility.
Kjell wrote:Use a persistent array for those situations. You don't want to allocate & assign & destroy that data locally each frame.
I thought more about usage in OnLoaded, OnSpawn, BitmapExpression, and MeshExpression components. But this is not very import feature anyway.
Post Reply