- Joined
- Oct 11, 2012
- Messages
- 711
Hi all. I am still learning vJass and what I am trying to do is to make a spell that can change the fly height of a group of units around the hero. It first elevates the group of units to a certain height, and then decrease their fly height until they hit the ground, nothing complicated. Here is my code:
The spell worked as intended when I tested it (only cast it a few times), but I would like to know:
1. Am I deallocating things correctly? Are there any leaks? Or other errors in terms of coding? (each time I cast this spell with two heroes standing among a group of units, the memory usage increases 20 to 40 k)
2. How to optimize the code?
3. There must be a better way of doing this. Any idea or thoughts that can help me? (I don't want to use other libraries, such as CTL, because I want to learn the basic of vJass first)
Thanks a lot.
Edit: I have made some improvement to the code, it's still ugly but better than the one above, please see post #8 .
JASS:
scope MoveZ
struct A
private static timer t = CreateTimer()
private static timer t1 = CreateTimer()
private thistype prev
private thistype next
private unit tri
private unit u
private group g
private real x
private real y
private method destroy takes nothing returns nothing
call this.deallocate()
set this.next.prev = this.prev
set this.prev.next = this.next
call DestroyGroup(this.g)
set this.tri = null
set this.u = null
set this.g = null
endmethod
private static method cb2_action takes nothing returns nothing
if GetUnitZ(GetEnumUnit()) > 0.01 then //Somehow, the unit can never reach the height of 0.000 after elevation, but 0.010
if UnitAlive(GetEnumUnit()) and GetEnumUnit() != gg_unit_Hpal_0000 then
call SetUnitFlyHeight( GetEnumUnit(), GetUnitZ(GetEnumUnit()) - 50,0 )
endif
endif
endmethod
private static method cb2 takes nothing returns nothing
local thistype this = thistype(0).next
loop
exitwhen this == 0
call ForGroup(this.g,function thistype.cb2_action)
set this.u = FirstOfGroup(this.g) // I use this to check if the group of units has reached the ground. I couldn't figure out a better way to check it.
if GetUnitZ(this.u) > 0.01 then
else
call PauseTimer(t1)
call this.destroy()
endif
set this = this.next
endloop
endmethod
private static method cb_action takes nothing returns nothing
if GetUnitZ(GetEnumUnit()) < 500 then //if the unit's height is lower than 500, then increase it.
if UnitAlive(GetEnumUnit()) then
call UnitAddAbility(GetEnumUnit(),'Amrf')
call UnitRemoveAbility(GetEnumUnit(),'Amrf')
call SetUnitFlyHeight( GetEnumUnit(), GetUnitZ(GetEnumUnit()) + 50,0 )
endif
else
call PauseTimer(t)
call TimerStart(t1,0.03,true,function thistype.cb2)
endif
endmethod
private static method cb takes nothing returns nothing
local thistype this = thistype(0).next
loop
exitwhen this == 0
set this.g = CreateGroup()
set this.x = GetUnitX(this.tri)
set this.y = GetUnitY(this.tri)
call GroupEnumUnitsInRange(this.g,this.x,this.y,600.,null)
call GroupRemoveUnit(this.g,this.tri) // I don't want the casting unit in the group.
call ForGroup(this.g,function thistype.cb_action)
set this = this.next
endloop
endmethod
private static method run takes nothing returns boolean
local thistype this = thistype.allocate()
set this.next = 0
set this.prev = thistype(0).prev
set thistype(0).prev.next = this
set thistype(0).prev = this
call TimerStart(t,0.03,true,function thistype.cb)
set this.tri = GetTriggerUnit()
return false
endmethod
//===========================================================================
private static method onInit takes nothing returns nothing
call RegisterSpellEffectEvent('A00U', function thistype.run)
endmethod
endstruct
endscope
The spell worked as intended when I tested it (only cast it a few times), but I would like to know:
1. Am I deallocating things correctly? Are there any leaks? Or other errors in terms of coding? (each time I cast this spell with two heroes standing among a group of units, the memory usage increases 20 to 40 k)
2. How to optimize the code?
3. There must be a better way of doing this. Any idea or thoughts that can help me? (I don't want to use other libraries, such as CTL, because I want to learn the basic of vJass first)
Thanks a lot.
Edit: I have made some improvement to the code, it's still ugly but better than the one above, please see post #8 .
Last edited: