Using variables in local commands in a timer

Posted by Azcporian on Sun 21 Feb 2021 03:25 PM — 7 posts, 21,717 views.

#0
I currently have a timer set up to interact with a pet every 10 minutes. They recently added items expand how you do so that I would like to add to the timer. Currently I have the timer set up with:


local commands = { "train ", "play ", "pat " }

Send ( commands [ math.random (#commands) ] .. GetVariable("Pet") )


They added a ball, stuffed toy, and a frisbee. The commands I would need to use is "train @Pet with ball|toy|frisbee" but I the other two commands play and pat would need to remain the same. I tried changing the local commands to:


local commands = { "train (GetVariable("Pet") with frisbee", "play (GetVariable("Pet")", "pat (GetVariable("Pet")" }


But I would get the error:


Compile error
World: Primary
Immediate execution
[string "Timer: "]:1: '}' expected near 'Pet'


I'm not sure how I would go about changing the timer to accomplish this.
Australia Forum Administrator #1
How about:


local commands = { "train ", "play ", "pat " }
local toys = { "ball", "stuffed toy", "frisbee" }

Send ( commands [ math.random (#commands) ] .. GetVariable("Pet") 
       .. " with " .. toys [  math.random (#toys) ] )
Australia Forum Administrator #2
Or, to do it your way:


 local commands = { "train " .. GetVariable("Pet") .. " with frisbee", 
                    "play " .. GetVariable("Pet"), 
                    "pat " .. GetVariable("Pet") }

Send ( commands [ math.random (#commands) ] )
Australia Forum Administrator #3
And to vary the toys:



local toys = { "ball", "stuffed toy", "frisbee" }

local commands = { "train " .. GetVariable("Pet") .. " with " .. toys [ math.random (#toys) ] , 
                   "play " .. GetVariable("Pet"), 
                   "pat " .. GetVariable("Pet") }

Send ( commands [ math.random (#commands) ] )
Australia Forum Administrator #4
To make it neater:


local pet = GetVariable("Pet") 
local toys = { "ball", "stuffed toy", "frisbee" }

local commands = { "train " .. pet .. " with " .. toys [ math.random (#toys) ] , 
                   "play " .. pet, 
                   "pat " .. pet }

Send ( commands [ math.random (#commands) ] )
#5
Thank you so much.

To be honest I never really knew what the .. before GetVariable did, so I never thought putting them after it would fix the problem.
Amended on Mon 22 Feb 2021 05:06 AM by Azcporian
Australia Forum Administrator #6
In Lua .. just concatenates strings.