• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

How to pass parameters to code variable in TimerStart() method?

Status
Not open for further replies.
Level 3
Joined
Jan 29, 2021
Messages
34
Hello guys, the question is pretty simple

Somewhere in the function I start the timer via
call TimerStart(myTimer, interval, false, function Actions)

I want to pass parameters to Actions() function, like

call TimerStart(myTimer, interval, false, function Action(100, 500))

How to do that?

If I cannot pass parameters how to solve the problem?
 
Last edited:
Level 3
Joined
Jan 29, 2021
Messages
34
Dont understand.
Lets make an example using pseudo-code:

I want to
call TimerStart(myTimer, interval, false, function Action(2))

and Actions() for example is

function Actions takes integer type returns nothing
// some other logic
if type == 1
Print("Hello")
if type == 2
Print("Goodbye")
if type == 3
Print(Fuck you")
endfunction
 
You cannot pass arguments to code paramaters.

JASS:
globals
    integer type = 0
endglobals

set type = 2
call TimerStart(myTimer, interval, false, function Action)

function Actions takes nothing returns nothing
// some other logic
if type == 1
Print("Hello")
if type == 2
Print("Goodbye")
endfunction

This is where things like TimerUtils or hashtables come in to play. If you were using Lua or TypeScript you could simply use lambdas.

JavaScript:
new Timer().start(interval, false, () => { actions(2) })
 
Level 3
Joined
Jan 29, 2021
Messages
34
Reasonable, using global variable, though not familiar with this style of coding xD

The next question, related to this topic(passing parameter to code handler) will be:

I want to make command, using Enter input, in format CommandName_Number, for example music_5

I have function that checks user input and creates/shows dialog with buttons to chose

Code:
function CheckInput() {
    GetUserMessage()
    CheckMessage() / ParseString()
    CreateAndShowDialog()
}

I have initialization function, like

Code:
function Init() {
    //do some initialization
    createTrigger()
    TriggerRegisterEvent(dialog button was pressed)
    TriggerAddActions(... function Actions())
}

and some logic in Actions()

Code:
function Actions() {
    DoSomeLogic(number)
}

dialog and its buttons are allready stored in memory via global variables
So, here I want to pass parameter 5 from my command to the function Actions in that trigger to make it work in function DoSomeLogic()

How to do that?
If again I make global variable number, where I reset it after reading from user input? In the end of the function that creates Dialog Menu?
 
Last edited:
Level 15
Joined
Sep 29, 2008
Messages
362
As TH says, you could use handle ids from the objects to link values stored into a table - hashtable.

use the function GetHandleId() to get id from any object.
To get Timer handle use GetExpiredTimer()

So your code would looks like the following pseudocode
JASS:
globals

 hashtable ht = null

endglobals


At map init functions you must initialize the hashtable
JASS:
 set ht = InitHashtable()

then back to your function
JASS:
function TestTimer ...
  local integer var = 1000
  local integer pos = 1
  local timer t =  new timer  //...check JASS libraries to choose a new timer function

  call SaveInteger(ht,GetHandleId(t), pos, var)

  call TimerStart(t, interval, false, function Action)


 set t = null

endfunction


then go to Action function

JASS:
function Action ...

 local timer t = GetExpiredTimer()
 local integer pos = 1
 local integer var = LoadInteger(ht,GetHandleId(t),pos)

call DebugMsg(var)

/*

 actions to check if you need to pause or stop the timer

and nullyfy it

*/


endfunction

ofc all this in a very basic approach
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,534
If you use Lua you don't have to deal with this crap.
Lua:
function Example()
    local a = 1
    local b = 2
    local c = 3
    local t = CreateTimer()
    TimerStart(t, 1.00, false, function ()
        print(a, b, c)
        DestroyTimer(t)
    end)
end
The timer's function can be created right there on the spot and it will have reference to the local variables.
 
Last edited:
Status
Not open for further replies.
Top