I want to execute a function from a timer after a certain amount of in-game time has passed, to do things like enable and disable status effects, trigger events, etc., using a unified system.
I have an array of timer objects which look something like this:
{ time: 10, func: "exampleFunction", args: [foo, bar] }
This function is called from the main time function (a macro named <<time>>) and progresses all the timers:
function timers(forward) {
"use strict";
let timers = State.getVar("$timers")
for (let i = 0; i < timers.length; i++) {
timers[i].time = timers[i].time - forward
if (timers[i].time <= 0) {
executeTimer(timers[i])
timers.deleteAt(i)
}
}
State.setVar("$timers", timers);
}
function executeTimer(timer) {
let func = timer.func
let args = timer.args
return window[func](args);
}
I want the code to execute exampleFunction(foo, bar) when the time property reaches 0. It works until the actual execution, where I get this error message:
Error: cannot execute macro <<time>>: window[func] is not a function
What am I doing wrong? Is there a better way to do this?