• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] Making a triggered mine/trap

Status
Not open for further replies.
Level 2
Joined
Nov 29, 2006
Messages
6
This is what I've got so far, it kind of bugs sometimes though (like killing nearby allied units when many enemies stand on it) and I don't know how to remove memory leaks in this special case.

Code:
function Trig_Glyph_Conditions takes nothing returns boolean
return GetSpellAbilityId()=='A04O'
endfunction

function triggermine takes nothing returns nothing
local trigger t=GetTriggeringTrigger()
local unit u=GetTriggerUnit()
local player p=GetOwningPlayer(u)
local string t_table = I2S(CS_H2I(t))
local unit mine=GetTableUnit(t_table,"mine")
if IsUnitEnemy(mine,p) then
call KillUnit(u)
call RemoveUnit(mine)
call FlushStoredMission(CSCache(), t_table)
set u=null
set mine=null
set p=null
call DestroyTrigger(t)
set t=null
endif
endfunction

function Trig_Glyph_Actions takes nothing returns nothing
local unit caster=GetTriggerUnit()
local player p=GetOwningPlayer(caster)
local real x=GetUnitX(caster)
local real y=GetUnitY(caster)
local unit mine=CreateUnit(p,'h028',x,y,0)
local trigger t=CreateTrigger()
local triggeraction ta
local string t_table=I2S(CS_H2I(t))
call SetTableObject(t_table,"mine",mine)
call TriggerRegisterUnitInRangeSimple(t,100,mine)
set ta=TriggerAddAction(t,function triggermine)
set caster=null
set p=null
set mine=null
endfunction

//===========================================================================
function InitTrig_Glyph takes nothing returns nothing
    set gg_trg_Glyph = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Glyph, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( gg_trg_Glyph, Condition( function Trig_Glyph_Conditions ) )
    call TriggerAddAction( gg_trg_Glyph, function Trig_Glyph_Actions )
endfunction
 
Level 11
Joined
Jul 12, 2005
Messages
764
You have a bit overcomplicated this...

function triggermine takes nothing returns nothing
local unit u=GetTriggerUnit()
local unit mine=GetTableUnit(I2S(CS_H2I(t)),"mine")
if IsUnitEnemy(u,GetOwningPlayer(mine)) then
call KillUnit(u)
call RemoveUnit(mine)
call FlushStoredMission(CSCache(), t_table)
call DestroyTrigger(GetTriggeringTrigger())
endif
set u=null
set mine=null
endfunction

function Trig_Glyph_Actions takes nothing returns nothing
local unit caster=GetTriggerUnit()
local unit mine = CreateUnit(GetOwningPlayer(caster),'h028',GetUnitX(caster),GetUnitY(caster),0
local trigger t=CreateTrigger()
call SetTableObject(I2S(CS_H2I(t)),"mine",mine)
call TriggerRegisterUnitInRangeSimple(t,100,mine)
call TriggerAddAction(t,function triggermine)
set caster=null
set mine=null
endfunction

I think the problem was here:
if IsUnitEnemy(mine,p) then
this should be:
if IsUnitEnemy(u,p) then
 
Level 2
Joined
Nov 29, 2006
Messages
6
Heh, maybe I did overcomplicated, gonna try your suggestion.
However, doesn't your TriggerAddAction cause a leak?
 
Status
Not open for further replies.
Top