Page 1 of 1

Array

Posted: Tue Feb 12, 2013 3:21 pm
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

Posted: Tue Feb 12, 2013 9:49 pm
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.

Posted: Wed Feb 13, 2013 8:38 am
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.

Posted: Wed Feb 13, 2013 11:57 am
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

Posted: Wed Feb 13, 2013 12:16 pm
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}};

Posted: Wed Feb 13, 2013 12:24 pm
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

Posted: Fri Feb 15, 2013 7:22 am
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.