HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Problem with triggers and timer [JASS]

05-30-2006, 10:00 PM#1
StockBreak
Hi all, I have a great JASS problem; I want to create a new trigger which has the "timer elapses" event, BUT when fired it should destroy another trigger; however I cannot find any way to pass the local trigger tr as an argument to the timer trigger, can someone explain it to me? Thanks.
Here is a simple of the code:

Collapse JASS:
function NewTrigger takes unit u1, u2, timer t returns nothing
    local trigger tr = CreateTrigger(  )
    local trigger maxtime = CreateTrigger(  )
    call TriggerRegisterUnitEvent( tr, u1, EVENT_UNIT_DAMAGED)
    call TriggerAddCondition( tr, Condition( function Trig_NewTrigger_Conditions ) )
    call TriggerAddAction( tr, function Trig_NewTrigger_Actions )
    call TriggerRegisterTimerExpireEventBJ( maxtime, maxtimer )
    // here is the problem! I want to pass the "tr" local trigger to the
    // trigger "maxtime" in order to destroy "tr" when the timer elapses
    // call TriggerAddAction( maxtime, function Trig_MaxTime_Actions( tr ) )
    set tr = null
    set maxtime = null
endfunction
05-30-2006, 10:07 PM#2
Rising_Dusk
Use handle vars or tables or whatever you're most comfortable with and attach 'tr' to 'maxtime' so when 'maxtime' executes you can thus destroy 'tr'.

Collapse JASS:
call SetHandle(maxtime, "tr", tr)
...Then to get it in the code for maxtime...
Collapse JASS:
local trigger maxtime = GetTriggeringTrigger()
local trigger tr = GetTrigger(maxtime, "tr")
That's how it looks using the variations of handles I use for my maps.
It may or may not look different for you.
Don't forget to FlushLocals() and such when done!

Also, fyi, the variable 'maxtimer' is undeclared in...
Collapse JASS:
call TriggerRegisterTimerExpireEventBJ( maxtime, maxtimer )
05-31-2006, 09:02 AM#3
StockBreak
Thank you very much for your help. I know nothing about handle variables; do you know a good tutorial about those things?

P.S: just some more questions:
1) where do I have to put the code
Collapse JASS:
call SetHandle(maxtime, "tr", tr)
?
2) In this way, each time a create a new trigger tr, a COMPLETELY NEW trigger maxtime is created, right? I say that because that function NewTrigger is often called different times, almost contemporary. Thanks.
05-31-2006, 09:40 AM#4
The)TideHunter(
Im not totaly sure what you asked for but i think it was this:
This requires a gamecache called GC (or udg_GC in Jass terms)

Collapse JASS:
function GameCache takes nothing returns gamecache
    if(udg_GC == null) then
        call FlushGameCache(InitGameCache("GC"))
        set udg_GC = InitGameCache("GC")
    endif
    return udg_GC
endfunction

function H2I takes handle H returns integer
    return H
    return I
endfunction

function I2Trigger takes integer I returns trigger
    return H
    return null
endfunction

function DestroyTheOtherTrigger takes nothing returns nothing
    call DestroyTrigger(I2Trigger(GetStoredInteger(GameCache(), I2S(H2I(GetExpiredTimer())))
endfunction

function NewTrigger takes unit u1, u2, timer t returns nothing
    local trigger tr = CreateTrigger(  )
    local trigger maxtime = CreateTrigger(  )

    call StoreInteger(GameCache(), I2S(H2I(t)), "Timer_DestroyingTrigger", H2I(tr))

    call TriggerRegisterUnitEvent( tr, u1, EVENT_UNIT_DAMAGED)
    call TriggerAddCondition( tr, Condition( function Trig_NewTrigger_Conditions ) )
    call TriggerAddAction( tr, function Trig_NewTrigger_Actions )

    call TriggerAddAction( maxtime, function DestroyTheOtherTrigger )

    call TriggerRegisterTimerExpireEventBJ( maxtime, maxtimer )
    set tr = null
    set maxtime = null
endfunction

This will only work if the timer does expire, and you have to destroy the timer wherever you want, other it will leak

EDIT: I put a space inbetween the stuff i did to make it stand out
05-31-2006, 02:32 PM#5
StockBreak
Well, what I am really asking is: "how to pass locals from one trigger to another"? It's impossible for me because the required function for TriggerAddAction (and similars) can't take any parameters (it must be a "takes nothing returns nothing" func). Thanks
05-31-2006, 04:17 PM#6
Captain Griffen
Local handle variables, using game cache and return bug.
05-31-2006, 04:26 PM#7
TaintedReality
http://www.wc3jass.com/viewtopic.php?t=224
05-31-2006, 06:12 PM#8
Rising_Dusk
Collapse JASS:
function H2I takes handle h returns integer
    return h
    return 0
endfunction

//CREATE A GLOBAL VARIABLE LIKE SO...
//Name: cache
//Type: gamecache
function Cache takes nothing returns gamecache
    if udg_cache==null then
        call FlushGameCache(InitGameCache("GameCache.x"))
        set udg_cache=InitGameCache("GameCache.x")
    endif
    return udg_cache
endfunction

function SetInt takes handle h,string n,integer v returns nothing
    if v==0 then
        call FlushStoredInteger(Cache(),I2S(H2I(h)),n)
    else
        call StoreInteger(Cache(),I2S(H2I(h)),n,v)
    endif
endfunction

function SetReal takes handle subject,string name,real value returns nothing
    if value==0 then
        call FlushStoredReal(Cache(),I2S(H2I(subject)),name)
    else
        call StoreReal(Cache(),I2S(H2I(subject)),name,value)
    endif
endfunction

function SetHandle takes handle subject,string name,handle value returns nothing
    call SetInt(subject,name,H2I(value))
endfunction

function GetTrigger takes handle subject,string name returns trigger
    return GetStoredInteger(Cache(),I2S(H2I(subject)),name)
    return null
endfunction

function FlushLocals takes handle subject returns nothing
    call FlushStoredMission(Cache(),I2S(H2I(subject)))
endfunction

//***************************************************************************************************************
//*                                                                                                             *
//*                                          End Handle Stuff                                                   *
//*                                                                                                             *
//***************************************************************************************************************

function NewTrigger_Update takes nothing returns nothing
    local timer maxtime = GetExpiredTimer()
    local trigger tr = GetTrigger(maxtime, "tr")
    
    //Do other stuff here
    
    call DestroyTrigger(tr)
    call DestroyTimer(maxtime)
    set tr = null
endfunction

function NewTrigger takes unit u1, u2, real duration, timer t returns nothing
    local trigger tr = CreateTrigger(  )
    local timer maxtime = CreateTimer(  )
    call TriggerRegisterUnitEvent( tr, u1, EVENT_UNIT_DAMAGED)
    call TriggerAddCondition( tr, Condition( function Trig_NewTrigger_Conditions ) )
    call TriggerAddAction( tr, function Trig_NewTrigger_Actions )
    call SetHandle(maxtime, "tr", tr)
    call TimerStart(maxtime, duration, false, function NewTrigger_Update)
    set tr = null
    set maxtime = null
endfunction

That's how it would all look for me.

Oh and also.
This is my favorite tutorial on handles.
05-31-2006, 08:29 PM#9
StockBreak
I tried to used another method (since I'm still learing handle vars), but it seems to cause a fatal error after the ability it's used a couple of times... Sigh :( Do you know why does it crash? I attached my simple map. Thanks a lot.

EDIT: to make the map crash, just attack AND stop attacking before the attack is correctly finished, attack normally other times and it should crash.
Attached Files
File type: w3xCustom Critical Strike.w3x (24.3 KB)
05-31-2006, 08:45 PM#10
Rising_Dusk
Humm... I just played your map and it didn't crash at all.
And I used your custom crit the entire way through, appeared to work.
However, even if it didn't, I can't understand what the text that appears on the screen means. :P
All I understood was 'normale' and 'mortale'.

Anyways, it appeared to work properly.
I'll delve into the code and check it closer though, just for kicks.
05-31-2006, 08:55 PM#11
StockBreak
Quote:
Originally Posted by Rising_Dusk
Humm... I just played your map and it didn't crash at all.
And I used your custom crit the entire way through, appeared to work.
However, even if it didn't, I can't understand what the text that appears on the screen means. :P
All I understood was 'normale' and 'mortale'.

Anyways, it appeared to work properly.
I'll delve into the code and check it closer though, just for kicks.
Oh, sorry! I forgot to change the italian debug strings^^ Just attack, stop and attack again some times and, believe me, it will crash to windows screen! Thanks.

P.S: normale means that a normal attack was performed, mortale means that a critical strike was performed. You can use the Critical Strike also on allied units, so you don't have to hit creeps.