Array-extending Structs with non-static arrays

Status
Not open for further replies.
Level 7
Joined
Jan 30, 2011
Messages
267
JASS:
struct XY extends array
    integer array xy[10]
endstruct
this makes the compiler piss
is there a way to create a custom allocator for the arrays in order to be able to use them in array structs?

EDIT:
would solve it like this, but is there a way to use [] syntax without creating another struct?
JASS:
struct XY extends array
    private static integer array xy_p

    method get_xy takes integer i returns integer //method operator xy[] sadly cant be declared
        return thistype.xy_p[i + 10*this]
    endmethod

    method set_xy takes integer i, integer x returns nothing
        set thistype.xy_p[i + 10*this] = x
    endmethod
endstruct
 
I don't see any way to do so with Vexorian's jasshelper. You can suggest it here, though.

Personally, I had the same problem and I simply didn't use "extends array" and added this kind of line in my custom allocator :

JASS:
set s__XY_xy[this]=this*ARRAYSIZE

That's dirty and it creates the unneeded allocators but it works. You use the array with "this.xy" normally after that. It compiles into "s___XY_xy[s__XY_xy[this]+i]".
 
Level 7
Joined
Jan 30, 2011
Messages
267
if you didnt use "extends array" why did you do that custom lane at all? when ur not extending array then you can create non-static arrays and they work properly
 
They work properly only if you use the structure's allocator (.allocate()).
The point of using "extends array" is to create your own allocator/deallocator, it has no other purpose.

My allocator is more complicated, but it could have ressemble to that :
JASS:
struct MyStruct
    real array x[10]

    static integer AllocIndex = 1

    static method create takes nothing returns MyStruct
        local MyStruct this = MyStruct.AllocIndex
        set MyStruct.AllocIndex = MyStruct.AllocIndex+1
        set s__MyStruct_x[this] = this*10
        return this
    endmethod

endstruct
There, if I didn't add the line, the array wouldn't work because I never ever call MyStruct.allocate().
Btw, if you don't declare a static method "create", the .create() and .allocate() are precompiled into exactly the same call.
 
Status
Not open for further replies.
Top