structs can not access values of other structs?

Started by ollj, Mon 13/07/2015 11:00:48

Previous topic - Next topic

ollj

I have 2 different Structs, they contain (non-writeprotected) parameters/integers, getter and setter functions for them.
Now the second struct wants to access the value stored in one INSTANCE of the fist struct struct, that exists globally and that was instanced before the second struct is defined.

The second struct does that by not using "this." but instead directly addressing an instance of the first Struct (that exists before the second struct is defined). This however does create a new instance of that struct during runtime, and the addressed instance is NOT the same instance as the global instance of the first struct.
The instance referred to from the second Struct's function is instead a new instance that confusingly has the very same name/label in source code but (on runtime) has different memory addresses, without any warning about this during compilation. that second instance with the same label is of course  initialized with 0-vallues and null-pointers, and the (second) struct can use all its functions , change the values of the struct...

Intuitively you would think it would both point at the same single global instance of the struct.

Actually this can easily be confusing and make debugging unnecessarily complicated, since auto-complete does not care and no warning is given.

---

I tested this, and so far my only workaround to have one struct (and any of its instances) access the parameters/values/integers of another Struct- instances ... is to give Structs the ability to copy/load their data to a global array. I already made a struct for that (one for "int) and one for "Character*" (with v3.4alpha-dynamic array in the struct) and any other struct may "extend" on that struct to inherit the ability to copy its values to a global array, so that other Structs can read from it (and copy the data to their instances).

Crimson Wizard

#1
Some additional information would be useful to clarify the situation.
Are the structs declared in different script modules? How do you import/export struct instances (variables of struct type)?
Could you maybe provide a related excerpt from your script?

The "instances duplication" may be a result of incorrect variable import -- this is the most common mistake with AGS script, and the first idea that comes to mind.

EDIT:
Here, I found very similar case: http://www.adventuregamestudio.co.uk/forums/index.php?topic=52018
A related pages in Wiki:
http://www.adventuregamestudio.co.uk/wiki/The_script_header
http://www.adventuregamestudio.co.uk/wiki/Script_language_keywords#import


Khris

Somebody clearly declared variables in the header...

ollj

Quote from: Khris on Mon 13/07/2015 23:14:35
Somebody clearly declared variables in the header...

i (next to) never declare variables outside of structs. almost all of my variables are in various structures.
so i guess the same issue is with declaring an instance of a struct, and not exporting (and importing) that.
i didnt think about NOT declaring instances in scripts header (that also defines the instanced struct)

Crimson Wizard

#4
Quote from: ollj on Sun 19/07/2015 14:49:00
Quote from: Khris on Mon 13/07/2015 23:14:35
Somebody clearly declared variables in the header...

i (next to) never declare variables outside of structs. almost all of my variables are in various structures.
so i guess the same issue is with declaring an instance of a struct, and not exporting (and importing) that.
i didnt think about NOT declaring instances in scripts header (that also defines the instanced struct)
Global instance of a struct is a variable too, as well as pointer to managed struct. These are entities of same kind, therefore same rules apply.

This is very similar to how C/C++ declares global variables (including global instances of struct) with "extern" in headers.

SMF spam blocked by CleanTalk