[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Using variables in local commands in a timer

Using variables in local commands in a timer

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Azcporian   (2 posts)  [Biography] bio
Date Sun 21 Feb 2021 03:25 PM (UTC)
Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,928 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sun 21 Feb 2021 09:21 PM (UTC)
Message
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) ] )

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,928 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sun 21 Feb 2021 09:23 PM (UTC)
Message
Or, to do it your way:


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

Send ( commands [ math.random (#commands) ] )

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,928 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Sun 21 Feb 2021 09:25 PM (UTC)
Message
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) ] )

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,928 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sun 21 Feb 2021 09:27 PM (UTC)
Message
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) ] )

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Azcporian   (2 posts)  [Biography] bio
Date Reply #5 on Mon 22 Feb 2021 05:05 AM (UTC)

Amended on Mon 22 Feb 2021 05:06 AM (UTC) by Azcporian

Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,928 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Mon 22 Feb 2021 05:07 AM (UTC)
Message
In Lua .. just concatenates strings.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


10,762 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]