• 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] Am I using allocate and deallocate in the right way?

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi all. I am learning allocate and deallocate... Since I cannot find a decent tutorial, I need help from you guys. Thanks. :)
JASS:
struct TESTER

    static unit FOOTMAN = null
    static timer t 
    static boolean b = false
    private static TESTER data
    
    static method onCollide takes Missile new, unit justHit returns boolean
            if IsUnitEnemy(justHit, GetOwningPlayer(new.source)) then
                call UnitDamageTarget(new.source, justHit, 10., true, false, ATTACK_TYPE_CHAOS, DAMAGE_TYPE_DEMOLITION, null)
                //call DestroyEffect(AddSpecialEffectTarget(TERTIARY_COLLIDEFX, justHit, TERTIARY_ATTACHPOINT))
            endif
            
            return false
     endmethod
        
    implement MissileStruct
    
    static method onExpire takes nothing returns nothing
        local thistype this=thistype(TESTER.data)
        local real x = GetUnitX(FOOTMAN)
        local real y = GetUnitY(FOOTMAN)
        local real a = GetRandomReal(-bj_PI,bj_PI)
        local Missile new 
        if GetUnitState(FOOTMAN,UNIT_STATE_LIFE)>0.405 then
            set new= Missile.create(x,y,65,x+1000*Cos(a),y+1000*Sin(a),0)
            set new.speed=10
            set new.model="Abilities\\Weapons\\VengeanceMissile\\VengeanceMissile.mdl"
            set new.height=100
            set new.source=FOOTMAN
            set new.collision=80
            call launch(new)
        else
            call PauseTimer(TESTER.t)
            call this.deallocate() //deallocating here
            set TESTER.t=null
            set TESTER.data=null
        endif
    endmethod
    
    static method esc takes nothing returns boolean
        local thistype this=thistype.allocate() //allocating here

        if b then
            call PauseTimer(TESTER.t)
            call this.deallocate() //deallocating here
            set TESTER.t=null
            set TESTER.data=null
        else
            set TESTER.t=CreateTimer()
            set TESTER.data=this
            call TimerStart(t,0.05,true,function thistype.onExpire)
        endif
        set b = not b
        return false
    endmethod
    
    static method onInit takes nothing returns nothing
        local trigger trig=CreateTrigger()
        call TriggerRegisterPlayerEvent(trig,Player(0),EVENT_PLAYER_END_CINEMATIC)
        call TriggerAddCondition(trig,Condition(function thistype.esc))
        set FOOTMAN=CreateUnit(Player(0),'hfoo',0,0,270)
    endmethod
    
endstruct
 
Level 11
Joined
Oct 11, 2012
Messages
711
About your question, why are you even using .allocate() and .deallocate()? You should just use .create() and .destroy() instead.
The .(de)allocate() methods are intended for writing custom constructors.

Do you mean I should only do this?
JASS:
struct TESTER

    static unit FOOTMAN = null
    static timer t 
    static boolean b = false
    private static TESTER data
    
    static method onCollide takes Missile new, unit justHit returns boolean
            if IsUnitEnemy(justHit, GetOwningPlayer(new.source)) then
                call UnitDamageTarget(new.source, justHit, 10., true, false, ATTACK_TYPE_CHAOS, DAMAGE_TYPE_DEMOLITION, null)
                //call DestroyEffect(AddSpecialEffectTarget(TERTIARY_COLLIDEFX, justHit, TERTIARY_ATTACHPOINT))
            endif
            
            return false
        endmethod
        
    implement MissileStruct
    
    static method onExpire takes nothing returns nothing
        local thistype this=thistype(TESTER.data)
        local real x = GetUnitX(FOOTMAN)
        local real y = GetUnitY(FOOTMAN)
        local real a = GetRandomReal(-bj_PI,bj_PI)
        local Missile new 
        if GetUnitState(FOOTMAN,UNIT_STATE_LIFE)>0.405 then
            set new= Missile.create(x,y,65,x+1000*Cos(a),y+1000*Sin(a),0)
            set new.speed=10
            set new.model="Abilities\\Weapons\\VengeanceMissile\\VengeanceMissile.mdl"
            set new.height=100
            set new.source=FOOTMAN
            set new.collision=80
            call launch(new)
        else
            call PauseTimer(this.t)
            call this.destroy()
        endif
    endmethod
    
    static method esc takes nothing returns boolean
        local thistype this=thistype.create()

        if b then
            call PauseTimer(this.t)
            call this.destroy()
        else
            set this.t=CreateTimer()
            set TESTER.data=this
            call TimerStart(t,0.05,true,function thistype.onExpire)
        endif
        set b = not b
        return false
    endmethod
    
    static method onInit takes nothing returns nothing
        local trigger trig=CreateTrigger()
        call TriggerRegisterPlayerEvent(trig,Player(0),EVENT_PLAYER_END_CINEMATIC)
        call TriggerAddCondition(trig,Condition(function thistype.esc))
        set FOOTMAN=CreateUnit(Player(0),'hfoo',0,0,270)
    endmethod
    
endstruct

So, under what circumstances should I use allocate and deallocate? :/

Edit: Should I use allocate and deallocate when I use "extends array"?
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
In case you want to use struct extends array you have to write the create and destroy method on your own
or you use the Alloc module.

JASS:
struct MyStruct extends array
    private static integer instanceCount = 0
    private static thistype recycle = 0
    private thistype recycleNext

    static method create takes nothing returns thistype
        local thistype this

        //first check to see if there are any structs waiting to be recycled
        if (recycle == 0) then
            //if recycle is 0, there are no structs, so increase instance count
            set instanceCount = instanceCount + 1
            set this = instanceCount
        else
            //a struct is waiting to be recycled, so use it
            set this = recycle
            set recycle = recycle.recycleNext
        endif

        //perform creation code

        return this
    endmethod

    method destroy takes nothing returns nothing
        //add to recycle stack
        set recycleNext = recycle
        set recycle = this
    endmethod
endstruct

You can find the whole tutorial here: Coding efficient vJass structs
 
Level 11
Joined
Oct 11, 2012
Messages
711
when you are inside of create/destroy

when you use extends array with an Alloc module

In case you want to use struct extends array you have to write the create and destroy method on your own
or you use the Alloc module.

JASS:
struct MyStruct extends array
    private static integer instanceCount = 0
    private static thistype recycle = 0
    private thistype recycleNext

    static method create takes nothing returns thistype
        local thistype this

        //first check to see if there are any structs waiting to be recycled
        if (recycle == 0) then
            //if recycle is 0, there are no structs, so increase instance count
            set instanceCount = instanceCount + 1
            set this = instanceCount
        else
            //a struct is waiting to be recycled, so use it
            set this = recycle
            set recycle = recycle.recycleNext
        endif

        //perform creation code

        return this
    endmethod

    method destroy takes nothing returns nothing
        //add to recycle stack
        set recycleNext = recycle
        set recycle = this
    endmethod
endstruct

You can find the whole tutorial here: Coding efficient vJass structs
Thanks for both of you, +Rep if I can.
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
Jasshelpermanual is not some kind of fancy tutorial for expert knowledge, its the official manual of the vJass language and comes with every JNGP copy. Its standard literature for everyone who uses vJass.
If the thread owner read it he knew how to use .create() and .allocate().

A significant amount of vJass libraries here on THW is based on abuse of the vJass language which is expert knowledge and should only be used when you know normal vJass. Which is why reading/recoding resources is only a good idea if you already know the language features.

If you want to learn vJass just read the damn manual.
 
Level 11
Joined
Oct 11, 2012
Messages
711
Jasshelpermanual is not some kind of fancy tutorial for expert knowledge, its the official manual of the vJass language and comes with every JNGP copy. Its standard literature for everyone who uses vJass.
If the thread owner read it he knew how to use .create() and .allocate().

A significant amount of vJass libraries here on THW is based on abuse of the vJass language which is expert knowledge and should only be used when you know normal vJass. Which is why reading/recoding resources is only a good idea if you already know the language features.

If you want to learn vJass just read the damn manual.

Yea, I did read the manual but some parts are difficult for me to understand and that's why I hope you guys can help me with it. :)

Anyway, thanks.
 
Status
Not open for further replies.
Top