Struct

Structure objects allow saving data of different types.

Once the structure is defined, an instance of it must be created in order to access its elements.

  1. void OnStart() {
  2. struct Structure {
  3. double arr[];
  4. int x;
  5. };
  6.  
  7. Structure a; // structure
  8. ArrayResize(a.arr, ArraySize(a.arr) + 1);
  9. a.arr[0] = 2;
  10. a.x = 1;
  11. Print(" arr[0] = ", a.arr[0], "; x = ", a.x);
  12.  
  13. Structure b[]; // array
  14. ArrayResize(b, ArraySize(b) + 1);
  15. b[0] = a; // copy the structure because each b[n] is empty
  16. ArrayResize(b, ArraySize(b) + 1);
  17. b[1] = a;
  18. b[1].arr[0] = 4;
  19. b[1].x = 2;
  20. Print(" b[0].arr[0] = ", b[0].arr[0], "; b[0].x = ", b[0].x,
  21. " b[1].arr[0] = ", b[1].arr[0], "; b[1].x = ", b[1].x);
  22. }

Structures can be used as structure of arrays (SoA) or as array of structures (AoS):

  1. struct Colour {
  2. double c[];
  3. };
  4. Colour colour; // structure of array (SoA)
  5.  
  6.  
  7. struct Shape {
  8. double s;
  9. };
  10. Shape shape[]; // array of structure (AoS)
  11.  
  12.  
  13. void OnStart() {
  14. soa();
  15. aos();
  16. }
  17.  
  18.  
  19. void soa() { // better performances
  20. ArrayResize(colour.c, ArraySize(colour.c) + 1);
  21. colour.c[ArraySize(colour.c) - 1] = 10;
  22. ArrayPrint(colour.c);
  23. }
  24.  
  25.  
  26. void aos() { // better clearness
  27. ArrayResize(shape, ArraySize(shape) + 1);
  28. shape[ArraySize(shape) - 1].s = 20;
  29. ArrayPrint(shape);
  30. }

Important: it's not possible to declare a structure within a function and pass its instance to another one.