• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] Struct instance destruction

Status
Not open for further replies.
Level 20
Joined
Jul 6, 2009
Messages
1,885
I ran into problem while i was using structs.

I have a trigger that loops through a struct instance array and to avoid destroyed structs, i used comparison 'If struct_instance != 0 then', but it didn't seem to work as i thought since even when an instance is destroyed, it still doesn't return 0. So i thought of checking if a random member of the destroyed instance is null, but it always returned a normal value.

So my questions are - what's the purpose of the destroy() method if it doesn't null instance's members?
And how to filter out destroyed instances in a loop?

EDIT: For second question, i thought of making a boolean member of a struct and setting it to true upon destruction, but is there another way?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Of course, it does not return 0 then, that's not even the case for built-in types. The variables won't be automatically reset, still holding an address unequal to null. The purpose of destroy()/deallocate() is to put the struct index back into choice, so it may be used for the next instance.

You might not pick the destroyed structs in the first place or what you mentioned.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
No, doing so in general would not be effective. You should not use an uninitialized variable in the first place. But you can write "<type> <name> = <value>" as declaration, so the variable would be set to <value> on each allocation as a default value. However, you could as well do this yourself. Just put a set variable-line in the create-method. You should always customize create/destroy. Handling it from outside like resetting the values manually each time is absurd.
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
I think you didn't understand me, though i don't know how to rephrase...
The purpose of destroy()/deallocate() is to put the struct index back into choice, so it may be used for the next instance.
I meant this. Does it mean that once an instance is deallocated, the next instance allocated will take the same index and will replace all values of old one?
The question isn't related to filtering out destroyed instances, just asking out of curiosity.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
I was targeting this question. For the normal allocation method, destroyed structs are placed onto a stack, last-in-first-out. So a new instance obtains the last freed index, in case the stack is not empty. It inherits the old values of the previous instance with the same index. It is practically the same one, just reused. Struct instances are only integers, which are placed into global arrays as an index to access its coherent data.

Just look into the output of your vJass scripts logs/outputwar3map.j.
 
I was targeting this question. For the normal allocation method, destroyed structs are placed onto a stack, last-in-first-out. So a new instance obtains the last freed index, in case the stack is not empty. It inherits the old values of the previous instance with the same index. It is practically the same one, just reused. Struct instances are only integers, which are placed into global arrays as an index to access its coherent data.

Just look into the output of your vJass scripts logs/outputwar3map.j.

To add to this:

JASS:
struct myStruct
    integer data
    static method onInit takes nothing returns nothing
        local thistype this = thistype.allocate()
        set this.data = 5
        call this.destroy()
        set this = thistype.allocate()
        call BJDebugMsg(I2S(this.data)) //Displays 5
        set this = thistype.allocate()
        call BJDebugMsg(I2S(this.data)) //Displays 0
    endmethod
endstruct

You can see here the effects of the last in/first out process.
 
Status
Not open for further replies.
Top