• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJass] onDestroy & deallocate()

Status
Not open for further replies.

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,468
Just a question, since sometimes I see this.deallocate()in the onDestroy method, sometimes I don't: does the onDestroy method need to call the generated destructor?

In the JassHelper manual, I read that the onDestroy method needs to call .deallocate(), but then directly after that they give an example script of an onDestroy method that does not use .deallocate().

I would imagine omitting something like the native destructor would be detrimental to the stability of the struct?
 
I think a manual this.destroy() should be enough. The onDestroy method is fired, well "on the destroyal". As Anachron said, .deallocate() is the same as .destroy(), but .deallocate() doesn't fire the onDestroy method. I don't think it is necessary to deallocate() in the onDestroy method though, unless you want to control at what point it is destroyed or something weird like that. Especially since so far, I haven't had any problems using merely .destroy().

It is essentially the same reason we use .allocate() in static method create methods.

EDIT: Perhaps you misinterpreted this line?
Since 0.9.Z.1 you may also override the destroy method by declaring your own one. Then use deallocate to call the normal destroy method.

By "calling deallocate to call the normal destroy method", it means that deallocate will do the normal destroying of the struct without calling the onDestroy method. I guess it is a bit confusing since they use the word "method" when they should use something like "process". =)
 
No it doesn't. It means that you can write your own destroy() function similar to how you can write your own create() function; when you do, you have to free the memory manually with deallocate() similarly to how you would have to get it with allocate().
What is the idea to be able to write the own onDestroy and destroy method? Shouldn't one be enough?
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
I have had reasons in which I have had to add faux-destroy methods (before this feature) because you couldn't override destroy.

Here's an example taken from some random map code:

JASS:
    method free takes nothing returns nothing
        set this.units = this.units - 1
        if this.units == 0 and this.maxunits > 10 then
            if this==trig then
                set trig = 0
            endif
            call DestroyTrigger(this.source)
            call this.destroy()
        endif
    endmethod
 
Status
Not open for further replies.
Top