Handle Vars

Frizz.Meister

New Member
Reaction score
0
A simple question really but i cant seem to work out how to actually use handle vars. I know what they do and how they do it but they never work for me.

Would it be possible for someone to post a trigger moving a unit from initial function to a timer function.

JASS:
unction Timer_Func takes nothing returns nothing
    call KillUnit( tu )
endfunction
        
function Init takes nothing returns nothing
    local timer t=CreateTimer()
    local unit tu=GetSpellTargetUnit()
    call TimerStart(t, 0.05, true, function Timer_Func)
    call TriggerSleepAction(1)
    call DestroyTimer(t)
    set t=null
endfunction


I know its not the best example but i want to move tu to the function Timer_Func

any help?
 

Romek

Super Moderator
Reaction score
964
I recommend Timer Utils.
I agree that Tom Jones' method is a bit difficult for a complete beginner. :p

Though, I don't use any attachment with high frequency timers.
 

Frizz.Meister

New Member
Reaction score
0
Actually Tom Jones got it perfect thanks a lot man you saved me a lot of time. However just out of curiosity how would a timer attach system help any more than a standard timer if i wasnt going to recycle it?
 

quraji

zap
Reaction score
144
It's not overkill, but some explanation would be handy..

With Tom's snippet (which assumes you use vJass):
JASS:

// every handle (unit, effect, timer, etc) gets a unique id when it is created,
// this will return that id
function H2I takes handle h returns integer
    return h
    return 0
endfunction

// an array for storing the struct ids
globals
    integer array S
endglobals

// structs are an easy way to hold lots of data for attaching, since all you
// need to access the data is the struct id
struct data
    unit u
    // you could have more things here (more units, groups, whatever)
endstruct

// read below first
function Callback ...
    local data s = S[H2I(GetExpiredTimer())-0x100000] // retrieve the struct
                                                                         // from the array using
                                                                         // the timer's id

    call KillUnit(s.u)
endfunction

function ...
    local data s = data.create()   // create the struct
    local timer t = CreateTimer()

    set s.u = ...
    set S[H2I(t)-0x100000] = s // save the struct id into the array
                                          // the index of the array is the id of the timer
                                          // minus the hex value 0x100000 because
                                          // that is the hex number at which handle ids
                                          // start (so that they'll fit into the array)
                                          // if you don't understand this part say so

    call TimerStart(t,0.01,false,function Callback) // start the timer
    set t = null
endfunction
 

Joker(Div)

Always Here..
Reaction score
86
Actually Tom Jones got it perfect thanks a lot man you saved me a lot of time. However just out of curiosity how would a timer attach system help any more than a standard timer if i wasnt going to recycle it?
It recycles for you. You just need to change the function names.

CreateTimer() -> NewTimer()
DestroyTimer() -> ReleaseTimer()

The the Romek linked uses a timer stack and resues them as needed.

Also, the attachment systems save you quite a bit of time.
 

Frizz.Meister

New Member
Reaction score
0
Although what you said makes sense to me. The trigger doesnt work. I looked around the web and from what i can gather i need to set a game cache, is this correct or is my mistake something else?

anyway here is my entire trigger
JASS:
globals
    data array KBD
endglobals

struct data
    unit u
endstruct

function H2I takes handle h returns integer
    return h
    return 0
endfunction

function Kill_Func takes nothing returns nothing
    local data s = KBD[H2I(GetExpiredTimer())-0x100000]
     call KillUnit(s.u)
endfunction
        
function Trig_trig_dummy_Actions takes nothing returns nothing
    local data s = data.create()
    local timer t = CreateTimer()
    set s.u =  (GroupPickRandomUnit(udg_TempGroup))
    set KBD[H2I(t)-0x100000] = s
    call TimerStart(t,0.05,true,function Kill_Func)
    call TriggerSleepAction(1)
    set t = null
endfunction

//===========================================================================
function InitTrig_trig_dummy takes nothing returns nothing
    set gg_trg_trig_dummy = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_trig_dummy, 400.00 )
    call TriggerAddAction( gg_trg_trig_dummy, function Trig_trig_dummy_Actions )
endfunction


If it helps the event is after 400 seconds have passed.

Edit: Also it ment to be a repeating action with the timer so more like
JASS:
    call TimerStart(t, 0.05, true, function My_Func)


Sorry for not making this clear earlier
 

Viikuna

No Marlo no game.
Reaction score
265
A bit overkill for a beginninger, No? Why not link to some timer attach system?

I hate when people say that.

Anyways, if H2I( timer ) - 0x100000 is greater than 8191 ( Jass array size ), you need to use vJass´s bigger arrays ( which are slower ).

Recycling timers is also a good idea.

edit. I actually wrote a tutorial about timer attaching. link
 

Frizz.Meister

New Member
Reaction score
0
the tutorial helped but it doesnt explain how to attach units only locations and integers. Im looking at the attaching data to timers section, just above the timer system section. Or can units simply be attached in the same way?

eg.
JASS:
local unit u=GetTriggerUnit()
      local integer handleID=H2I(u)


or would this not work?
 

Viikuna

No Marlo no game.
Reaction score
265
You should only attach integers ( structs ) to handles.

JASS:
struct Data
   unit u // you can have all data you need inside your struct
   location loc
   player p

endstruct


You can use H2I to unit too, but units also have UnitUserData so you should use it to index them.

edit. Yes, unit is a handle. You should check common.j and see which things extend handle.
 

Akolyt0r

New Member
Reaction score
33
TimerUtils (Red) is the best attachment system for timers ever ...

H2I(t)-OFFSET (without recycling timers) will break if you spawn many units and/or have leaks in your map ...
If you use a Damage Detection system there is no way around leaks anyway ..so you would hit that limit of 8190 handles pretty fast...

TimerUtils (red) use H2I(t)-OFFSET aswell ....but the timers for TU (red) are all created at map init ..so its ensured that they will be in the 8190 range...
 

Viikuna

No Marlo no game.
Reaction score
265
IMO the best way is to use struct arrays for periodic stuff

( fast & has no limits, but is practical only for periodic timers. )

and TimerUtils(Red) ( fast or some other TimerStack script for the rest.

( fast but has only limited amout of timers. )
 

Akolyt0r

New Member
Reaction score
33
IMO the best way is to use struct arrays for periodic stuff

True, but only for stuff which is used a lot (= its pretty likely you have more than one instance of "it).
Means ...its suitable for systems and maybe even spells which are used a lot (= spells of normal units) but not for spells which arent used a lot ( hero spells ?!)
 

Viikuna

No Marlo no game.
Reaction score
265
I dont really see why it would be non suitable for spells that are not used a lot.

Well, there is nothing wrong with using TimerUtils, just make sure that you dont run out of timers.
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top