timer problems in loops

Post Reply
Sani
Posts: 15
Joined: Thu Jan 21, 2010 6:35 pm

timer problems in loops

Post by Sani »

I have some code that basically looks like this...
Code: [show] | [select all] lua
for k, v in pairs(table) do
              tempTimer([[table[k] = true]])
       end
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.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: timer problems in loops

Post by Heiko »

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

Sani
Posts: 15
Joined: Thu Jan 21, 2010 6:35 pm

Re: timer problems in loops

Post by Sani »

Heiko wrote:Thats 2 syntax errors:
tempTimer( 1.5, table[k] .. "=true" )
well, good point. Was but an example though. What do you do when you have multiple commands that need encasing in []'s?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: timer problems in loops

Post by Heiko »

such as?

Sani
Posts: 15
Joined: Thu Jan 21, 2010 6:35 pm

Re: timer problems in loops

Post by Sani »

Code: [show] | [select all] lua
if string.find(v.special, "timer [%d]+") then
	local delay = tonumber(string.find(v.special, "timer ([%d]+)"))
	tempTimer(delay,defense_list[k].balance = 1:Cure_Balances["Salve"].used = false:send("stand"))
else
	tempTimer(1,defense_list[k].balance = 1)
end
Seperated the multiple commands with colons...how would you get that working correctly Master Heiko?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: timer problems in loops

Post by Heiko »

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.

Iocun
Posts: 174
Joined: Wed Dec 02, 2009 1:45 am

Re: timer problems in loops

Post by Iocun »

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")]])

Sani
Posts: 15
Joined: Thu Jan 21, 2010 6:35 pm

Re: timer problems in loops

Post by Sani »

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.

Post Reply