Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ 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 page


Posted by Azcporian   (2 posts)  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.
Top

Posted by Nick Gammon   Australia  (23,070 posts)  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
Top

Posted by Nick Gammon   Australia  (23,070 posts)  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
Top

Posted by Nick Gammon   Australia  (23,070 posts)  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
Top

Posted by Nick Gammon   Australia  (23,070 posts)  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
Top

Posted by Azcporian   (2 posts)  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.
Top

Posted by Nick Gammon   Australia  (23,070 posts)  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
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.


13,621 views.

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

Go to topic:           Search the forum


[Go to top] top

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