- Joined
- Jun 1, 2007
- Messages
- 98
As the title says, I have a struct with an onDestroy method and everything works up until I want to remove the struct. The onDestroy method doesn't get called. I filled it with BJDebugMsgs to check.
Here is the code. It's just a little test I was doing to make projectiles using structs.
The trigger which creates the struct:
The map is also attached, if that is easier.
Here is the code. It's just a little test I was doing to make projectiles using structs.
JASS:
//************************************************************
//
// For more JASS scripts, visit us at [url=http://www.wc3JASS.com]WC3Jass.com :: Index[/url]
//
//============================================================
// ===========================
function H2I takes handle h returns integer
return h
return 0
endfunction
// ===========================
function LocalVars takes nothing returns gamecache
return InitGameCache("jasslocalvars.w3v")
endfunction
function SetHandleHandle takes handle subject, string name, handle value returns nothing
if value==null then
call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)
else
call StoreInteger(LocalVars(), I2S(H2I(subject)), name, H2I(value))
endif
endfunction
function SetHandleInt takes handle subject, string name, integer value returns nothing
if value==0 then
call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)
else
call StoreInteger(LocalVars(), I2S(H2I(subject)), name, value)
endif
endfunction
function SetHandleReal takes handle subject, string name, real value returns nothing
if value==0 then
call FlushStoredReal(LocalVars(), I2S(H2I(subject)), name)
else
call StoreReal(LocalVars(), I2S(H2I(subject)), name, value)
endif
endfunction
function SetHandleString takes handle subject, string name, string value returns nothing
if value==null then
call FlushStoredString(LocalVars(), I2S(H2I(subject)), name)
else
call StoreString(LocalVars(), I2S(H2I(subject)), name, value)
endif
endfunction
function GetHandleHandle takes handle subject, string name returns handle
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
return null
endfunction
function GetHandleInt takes handle subject, string name returns integer
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
endfunction
function GetHandleReal takes handle subject, string name returns real
return GetStoredReal(LocalVars(), I2S(H2I(subject)), name)
endfunction
function GetHandleString takes handle subject, string name returns string
return GetStoredString(LocalVars(), I2S(H2I(subject)), name)
endfunction
function GetHandleUnit takes handle subject, string name returns unit
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
return null
endfunction
function GetHandleTimer takes handle subject, string name returns timer
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
return null
endfunction
function GetHandleTrigger takes handle subject, string name returns trigger
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
return null
endfunction
function GetHandleEffect takes handle subject, string name returns effect
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
return null
endfunction
function FlushHandleLocals takes handle subject returns nothing
call FlushStoredMission(LocalVars(), I2S(H2I(subject)) )
endfunction
//============================================================================
globals
real array STRENGTH
real array AGILITY
constant real ARROW_BASESPEED = 10.0
endglobals
struct arrow
unit owner
unit u
real speed
trigger onHit
timer t
static method create takes unit owner returns arrow
local arrow data = arrow.allocate()
set data.owner = owner
set data.u = CreateUnit( GetOwningPlayer( owner ), 'arro', GetUnitX( owner ), GetUnitY( owner ), GetUnitFacing( owner ) )
set data.speed = ARROW_BASESPEED //TO EDIT
set data.onHit = CreateTrigger()
call TriggerRegisterUnitInRange( data.onHit, data.u, 64, null )
call TriggerAddAction( data.onHit, function arrow.onHitFunc )
call SetHandleInt( data.onHit, "arrowstruct", data )
set data.t = CreateTimer()
call TimerStart( data.t, 0.03, true, function arrow.doArrowMove )
call SetHandleInt( data.t, "arrowstruct", data )
return data
endmethod
method onDestroy takes nothing returns nothing
call KillUnit( .u )
debug call BJDebugMsg( "Arrow killed" )
call RemoveUnit( .u )
debug call BJDebugMsg( "Arrow Removed" )
call PauseTimer( .t )
debug call BJDebugMsg( "Move timer paused" )
call FlushHandleLocals( .t )
debug call BJDebugMsg( "Timer Locals flushed" )
call FlushHandleLocals( .onHit )
debug call BJDebugMsg( "onHit Trigger Locals Flushed" )
call DestroyTrigger( .onHit )
debug call BJDebugMsg( "Trigger Destroyed" )
call DestroyTimer( .t )
debug call BJDebugMsg( "Timer Destroyed" )
set .u = null
set .t = null
set .onHit = null
endmethod
static method onHitFunc takes nothing returns nothing
local unit p = GetTriggerUnit()
local arrow ar = GetHandleInt( GetTriggeringTrigger(), "arrowstruct" )
debug call BJDebugMsg( "An arrow hit!" )
if not ( GetOwningPlayer( ar.u ) == GetOwningPlayer( p ) ) then
call KillUnit( p )
debug call BJDebugMsg( "Unit killed" )
call ar.destroy()
debug call BJDebugMsg( "Destroy function called" )
endif
set ar = 0
set p = null
endmethod
static method doArrowMove takes nothing returns nothing
local arrow a = GetHandleInt( GetExpiredTimer(), "arrowstruct" )
local real x = GetUnitX( a.u ) + a.speed * Cos( GetUnitFacing( a.u ) * bj_DEGTORAD )
local real y = GetUnitY( a.u ) + a.speed * Sin( GetUnitFacing( a.u ) * bj_DEGTORAD )
call SetUnitX( a.u, x )
call SetUnitY( a.u, y )
endmethod
endstruct
The trigger which creates the struct:
JASS:
function Trig_CreateArrow_Actions takes nothing returns nothing
local arrow a
if GetSpellAbilityId() == 'fire' then
set a = arrow.create( GetSpellAbilityUnit() )
endif
endfunction
//===========================================================================
function InitTrig_CreateArrow takes nothing returns nothing
set gg_trg_CreateArrow = CreateTrigger( )
call TriggerRegisterAnyUnitEventBJ( gg_trg_CreateArrow, EVENT_PLAYER_UNIT_SPELL_EFFECT )
call TriggerAddAction( gg_trg_CreateArrow, function Trig_CreateArrow_Actions )
endfunction
The map is also attached, if that is easier.