• 🏆 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!

Add ability for x seconds

Status
Not open for further replies.
Level 17
Joined
Mar 21, 2011
Messages
1,597
[SOLVED]Add ability for x seconds

Hi,
i want to make a simple trigger in Jass which adds an ability to a unit for x seconds. I use TimerUtils, but i dont know how to catch the unit in the called function at the end of the duration.

JASS:
function RemoveDetector takes nothing returns nothing
    call UnitRemoveAbility(?UNIT?, 'A00S')
    call ReleaseTimer(GetExpiredTimer())
endfunction

function Trig_Detector_Conditions takes nothing returns boolean
    local timer t
    if (GetSpellAbilityId() == 'A00T')) then
        set t = NewTimer()
        call TimerStart(t, 30.00, false, function RemoveDetector)
        call UnitAddAbility(GetTriggerUnit(), 'A00S')
        set t = null
    endif
    return false
endfunction

//===========================================================================
function InitTrig_Detector takes nothing returns nothing
    local integer index
    set index = 0
    set gg_trg_Detector = CreateTrigger()
    loop
        call TriggerRegisterPlayerUnitEvent(gg_trg_Detector, Player(index), EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
        set index = index + 1
        exitwhen index == bj_MAX_PLAYER_SLOTS
    endloop
    call TriggerAddCondition(gg_trg_Detector, Condition(function Trig_Detector_Conditions))
endfunction
 
Last edited:
For this particular situation, you're talking about attaching data to a unit. There are multiple ways to do this, as you can see, but using the unit's custom value is the fastest.

A unit indexer also gives you access to a unit the instant it exists and when it is removed from the game forever. For simplicity, GUI Unit Indexer doesn't give you access to the removed unit the instant it's removed as then you'd need to get into Object Editor which would make it's import process more than just copy&paste the trigger. GUI Unit Event covers the instant a unit is removed from the game.
 
That would be the case if you use structs and data attachment, but would not be the case if you use hashtables or Table.

If you're looking to make life easier, you could make this use Spell System.

JASS:
function RemoveDetector takes nothing returns boolean
    call UnitRemoveAbility(udg_Spell__Caster, 'A00S')
    return false
endfunction

function Trig_Detector_Conditions takes nothing returns boolean
    set udg_Spell__Time = 30.00
    call UnitAddAbility(udg_Spell__Caster, 'A00S')
    return false
endfunction

//===========================================================================
function InitTrig_Detector takes nothing returns nothing
    set udg_Spell__Trigger_OnEffect = CreateTrigger()
    call TriggerAddCondition(udg_Spell__Trigger_OnEffect, Condition(function Trig_Detector_Conditions))
    set udg_Spell__Trigger_OnLoop = CreateTrigger()
    call TriggerAddCondition(udg_Spell__Trigger_OnLoop, Condition(function RemoveDetector))
    call TriggerExecute(gg_trg_Spell_System)
endfunction
 
Level 17
Joined
Mar 21, 2011
Messages
1,597
If you're looking to make life easier, you could make this use Spell System.
There used to be a system in the spell section for this but it was removed for whatever reason.

maybe i'll use this later, but for now i want to learn new stuff :D



JASS:
struct Variables
     unit caster
 endstruct

 function AfterTimer takes nothing returns nothing
     local timer t=GetExpiredTimer()
     local Variables var=GetTimerData(t)
     call SetUnitX(var.caster)
     call DestroyTimer(t)
     set t=null
 endfunction

 function test takes nothing returns nothing
     local timer t=CreateTimer()
     local Variables var=Data.create()
     set var.caster=CreateUnit(...)
     call SetTimerData(t, var)
     set t=null
 endfunction
JASS:
globals
     unit array caster[]
endglobals

 function AfterTimer takes nothing returns nothing
     local timer t=GetExpiredTimer()
     local unit u = caster[GetTimerData(t)]
     call SetUnitX(u)
     call DestroyTimer(t)
     set t=null
 endfunction

 function test takes nothing returns nothing
     local timer t=CreateTimer()
     set SomeUnit=CreateUnit(...)
     call SetTimerData(t, GetUnitUserData(SomeUnit))
     set caster[GetUnitUserData(SomeUnit)] = SomeUnit
     set t=null
 endfunction

which of those 2 is the better way? I'm not even sure if it works correctly tbh.
 
Status
Not open for further replies.
Top