Managed Struct properties access issue via Array in AGS 4.0

Started by MrGreen, Tue 28/01/2025 12:52:24

Previous topic - Next topic

MrGreen

Hi!
I cannot access Struct properties via array in AGS 4.0
I only see the length property of the array.

When I try to run it it throws a "Null pointer referenced" error on the first line when I try to access the struct property. (Error line: testStruct_Array[0].int1 = 1;)
However, it works perfectly in 3.6

Any idea what could be the problem?  ???

Header:
Code: ags
managed struct TestStruct {
  int int1;
  int int2;
  int int3;
  bool flag;
};

Script:
Code: ags
TestStruct* testStruct_Array[];

function test_func() {
  testStruct_Array = new TestStruct[10];
  
  testStruct_Array[0].int1 = 1;
  testStruct_Array[0].int2 = 2;
  testStruct_Array[0].int3 = 3;
  testStruct_Array[0].flag = true;
}

Crimson Wizard

#1
An array of managed structs is in effect an array of pointers. When you create an array, you have a list of pointers, each initialized as "null", but no actual objects yet. So you need to create objects for each element of array separately:

Code: ags
testStruct_Array = new TestStruct[10];
for (int i = 0; i < testStruct_Array.Length; i++)
    testStruct_Array[i] = new TestStruct;

If you had a dynamic array of regular (non-managed) structs, then creating array would be enough.

EDIT:
The fact that Length property is shown in a hint list after "testStruct_Array[0].", instead of struct members - that's a autocomplete bug, and must be fixed in the editor.

MrGreen

Ooh, I'm stupid.  (roll)  That's how I did it before, but when I tried to get autocomplete to work, I gave up on the wrong version.
Thanks!

Crimson Wizard

#3
@MrGreen , if you like, here's a temporary AGS 4 build with few fixes to autocomplete:
https://cirrus-ci.com/task/6692180024098816

I found there were several mistakes in it, even prior to addition of array.Length property. For example, if you had array "Struct myarray[10];" and typed "myarray.", the autocomplete would show Struct's members, although you cannot access it without providing element index first.
I tried to fix mistakes with array autocomplete which I found, to make it work depending on whether you have a [n] index or not, whether it's a indexed property which is not a real array, and make sure that it works in chained access expressions too (like "myarray[1].member.memberarray[5].").

SMF spam blocked by CleanTalk