Frictional Games Forum (read-only)
Array in class - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: Array in class (/thread-14892.html)

Pages: 1 2


Array in class - nemesis567 - 04-18-2012

I've been trying forever to have an array in a class. It just won't work and will give me an error message. Is there any way to get an array into a class?



RE: Array in class - Cranky Old Man - 04-18-2012

(04-18-2012, 01:00 PM)nemesis567 Wrote: will give me an error message
What error message? Always, always specify what error message pops up. Error messages are made to be read and understood, if not by you, then by the person helping you.




RE: Array in class - nemesis567 - 04-18-2012

It's not specific, and thus won't help that much: Error: Expected ;

The ; was expected where I put "HERE"

int[][] myArray"HERE"(5, int[](5));



RE: Array in class - Cranky Old Man - 04-18-2012

(04-18-2012, 01:47 PM)nemesis567 Wrote: It's not specific, and thus won't help that much: Error: Expected ;



The ; was expected where I put "HERE"



int[][] myArray"HERE"(5, int[](5));


I know very little about AngelScript arrays, but reading the AngelScript documentation, I see that arrays aren't necessarily supported by the HPL2 engine.
Your line looks okay - it should define a 5x5 array of ints with uninitialized values - but judging by the error, it seems that the compiler can only understand empty arrays. ( int[][] myArray; )




RE: Array in class - nemesis567 - 04-18-2012

Empty arrays work as long as I don't ever assign anything to them. Pointless.



RE: Array in class - Apjjm - 04-18-2012

The problem comes from the fact that angelscript treats the array initilisation as a function call, which it then doesn't permit in the global scope:
Code:
int[][] x(4,int[](5)); -> int[][] x(4, makeArrayInt(5));
One way to fix this is to just to take split the initialisation out so there are none of these function calls:
Code:
int[][] x(4,_x1); int[] _x1(5,0);

This works, as can be seen by running the following test:
Code:
int[][] x(3,y); int[] y(3,0);

void OnStart()
{
    x[0][0] = 1;
    x[0][1] = 2;
    x[0][2] = 3;
    x[1][0] = 10;
    x[1][1] = 20;
    x[1][2] = 30;
    x[2][2] = -3;
    x[2][1] = -2;
    x[2][0] = -1;
    printArray(x,"Test");
    printArray(y,"Test2");
}

void printArray(int[] ar, string name)
{
  AddDebugMessage(name,false);
  for(uint i =0; i<ar.length(); i++) AddDebugMessage("["+i+"]" + ar[i],false);
}

void printArray(int[][] ar, string name)
{
  AddDebugMessage(name,false);
  for(uint i =0; i<ar.length(); i++)
  for(uint j =0; j<ar[i].length(); j++)
  AddDebugMessage("["+i+","+j+"]" + ar[i][j],false);
}

The other way is just to state the bounds in a constructor (classes) or the OnStart event:
Code:
int[][] a;

void OnStart()
{
   a = int[][](4,int[](5,0));
}

Edit: Forum decided to mess up the code tags horribly Sad


RE: Array in class - nemesis567 - 04-18-2012

Thanks a lot!

If you initialize it with values it also seems to work correctly.

Unfortunetely it still gives the same error. Remember that the array is a member of the class.



RE: Array in class - Your Computer - 04-18-2012

(04-18-2012, 02:52 PM)nemesis567 Wrote: Unfortunetely it still gives the same error. Remember that the array is a member of the class.

Try declaring the array without a value, with the size of the array provided within the square brackets, then in the constructor of the class assign the default values.


RE: Array in class - nemesis567 - 04-18-2012

int myArray[nVal1][nVal2]; Does not work.
int[][] myArray = {{1,2},{1,2}}; Does not work
int[][] myArray(2, int[](2)); Does not work
int[][] x(4,_x1); int[] _x1(5,0); Does not work
int[] simpleArray(2); Does not work
int[][] myArray; Works but when I initialize values in the constructor it gives an error from the main function: main(8,2): Failed to initialize global variable myObject;



RE: Array in class - Apjjm - 04-18-2012

The last method should still work for classes:
Code:
void OnStart()
  {
    Test t;
    printArray(t.array,"T");
  }
  
class Test
{
    int[][] array;
    
    Test()
     {
        array = int[][](4,int[](5,0));
     }
}

void printArray(int[][] ar, string name)
{
  AddDebugMessage(name,false);
  for(uint i =0; i<ar.length(); i++)
  for(uint j =0; j<ar[i].length(); j++)
  AddDebugMessage("["+i+","+j+"]" + ar[i][j],false);
}