but the timer fails to correctly insert the value of k, would anyone know a way around this? Sohl suggested encasing the command in --'s but that didn't help.
timer problems in loops
timer problems in loops
I have some code that basically looks like this...
Re: timer problems in loops
Thats 2 syntax errors:
If table[k] holds the global variable that you want to set true when the timer fires you can write:
tempTimer( 1.5, table[k] .. "=true" )
You can also reference the global variable directly by using _G["variableName"]=value
If table[k] holds the global variable that you want to set true when the timer fires you can write:
tempTimer( 1.5, table[k] .. "=true" )
You can also reference the global variable directly by using _G["variableName"]=value
Re: timer problems in loops
well, good point. Was but an example though. What do you do when you have multiple commands that need encasing in []'s?Heiko wrote:Thats 2 syntax errors:
tempTimer( 1.5, table[k] .. "=true" )
Re: timer problems in loops
such as?
Re: timer problems in loops
Seperated the multiple commands with colons...how would you get that working correctly Master Heiko?
Re: timer problems in loops
1. command seperator in Lua is ; not :
2. You can reference a global variable directly with _G["variableName"]=value. _G is a table that holds the entire Lua variables and functions.
3. tempTimer() expects the code parameter to be a string that. This code will be run when the timer fires. A typical newbie error is to forget the difference between variable name and value and the different evaluation times. Use string concats to assemble a runnable string. It's really not that difficult.
2. You can reference a global variable directly with _G["variableName"]=value. _G is a table that holds the entire Lua variables and functions.
3. tempTimer() expects the code parameter to be a string that. This code will be run when the timer fires. A typical newbie error is to forget the difference between variable name and value and the different evaluation times. Use string concats to assemble a runnable string. It's really not that difficult.
Re: timer problems in loops
Technically, you can also just use whitespace as a command separator (in other words: you don't really need a "command separator" at all in Lua). The following tempTimer should work fine, for instance:
tempTimer(1, [[echo("Hello ") echo("World!")]])
But semicolons can certainly help to make things look clearer, so I'll use them in your example:
tempTimer(delay,string.format([[defence_list["%s"].balance=1; Cure_Balances["Salve"].used=false; send("stand")]],k))
Here we created the string with string.format (which would be especially useful if you included lots of variables like "k" in that timer), but we could as well have used concatenation with double dots:
tempTimer(delay,[[defence_list["]]..k..[["].balance=1; Cure_Balances["Salve"].used=false; send("stand")]])
tempTimer(1, [[echo("Hello ") echo("World!")]])
But semicolons can certainly help to make things look clearer, so I'll use them in your example:
tempTimer(delay,string.format([[defence_list["%s"].balance=1; Cure_Balances["Salve"].used=false; send("stand")]],k))
Here we created the string with string.format (which would be especially useful if you included lots of variables like "k" in that timer), but we could as well have used concatenation with double dots:
tempTimer(delay,[[defence_list["]]..k..[["].balance=1; Cure_Balances["Salve"].used=false; send("stand")]])
Re: timer problems in loops
Just to make it clear, i'm not using : or ; as a seperator...that was just for illustrative purposes
And nice try Iocun! Timer actually fired, but returned... Lua error:[string "defence_list["Fitness"].balance=1"]:1: attempt to index global 'defence_list' (a nil value).
It is a global so...hmm :/
EDIT: Haha, bloody spelling differences. I've been americanised and have been spelling it defense, doh! Works though. You're a star Iocun.
And nice try Iocun! Timer actually fired, but returned... Lua error:[string "defence_list["Fitness"].balance=1"]:1: attempt to index global 'defence_list' (a nil value).
It is a global so...hmm :/
EDIT: Haha, bloody spelling differences. I've been americanised and have been spelling it defense, doh! Works though. You're a star Iocun.