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, confirm your email, resolve issues, 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 ➜ Call a function after AddTimer has fired

Call a function after AddTimer has fired

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


Posted by Solamain   (2 posts)  Bio
Date Sat 05 Sep 2015 07:31 PM (UTC)

Amended on Sat 05 Sep 2015 08:15 PM (UTC) by Solamain

Message
I am currently working on a plugin (Lua) to make idle time feel more organic when I leave my character sitting in a pub while I go afk for a smoke or to handle household chores. The problem I am having is figuring out how to call a function after the timer sends the action to the game. Pertinent code posted below, please not I am new to LUA but not coding, and any input on streamlining this code is welcome.

--------------------------

havedrink = 0
havesmoke = 0

function Callit ()
        RndAct ()
end

function RndAct ()
        math.randomseed(os.time())
        tmrstg = math.random(15, 150)
        tmpact = math.random(1, 25)
        if tmpact == 1 then tmract = "gaze"
        elseif tmpact == 2 then tmract = "act grabs some peanuts and begins snacking on them"
        elseif tmpact == 3 then tmract = "act stretches and adjusts his position on his stool"
        elseif tmpact == 4 then tmract = "act taps one of his feet impatiently as a nearby conversation rises into an argument"
        elseif tmpact == 5 then tmract = "act cracks his neck as he looks around cautiously"
        elseif tmpact == 6 then tmract = "act pushes hat back to scratch his head"
        elseif tmpact == 7 then tmract = "act begins humming a few bars of a popular pub song"
        elseif tmpact == 8 then tmract = "act inspect the bottles lining the shelves behind the bar"
        elseif tmpact == 9 then tmract = "act taps out a rhythm with his fingers on the bar"
        elseif tmpact == 10 then 
                if havedrink = 0 then
                        tmract = "act points to a random bottle on the shelves and buys a drink"
                        havedrink = math.random(3, 5)
                else
                        tmract = "act takes a sip of his drink, sighing with satisfaction"
                        havedrink = havedrink - 1
                end
        elseif tmpact == 11 then tmract = "act checks to make sure his coin purse, as well as his weapons, are still in place"
        elseif tmpact == 12 then 
                if havesmoke = 0 then
                        tmract = "act takes a smoke out of his case before returning it to his cloak, and then lights his cigarette"
                        havesmoke = math.random(8, 14)
                elseif havesmoke = 1 then
                        tmract = "act stubs out his smoke in the ashtray"
                        havesmoke = havesmoke - 1
                else
                        tmract = "act takes a long drag off of his smoke"
                        havesmoke = havesmoke - 1
                end
        elseif tmpact == 13 then tmract = "act rubs his eyes, pulling his hands down his face afterwards in a distracted manner"
        elseif tmpact == 14 then tmract = "sigh"
        elseif tmpact == 15 then tmract = "act pulls out a coin and rolls it across his knuckles a few times before returning it to his coin pouch"
        elseif tmpact == 16 then tmract = "act stands up and walks over to look at the jobs board, sighs at finding nothing of interest, then returns to his stool"
        elseif tmpact == 17 then tmract = "act checks his watch, taps the face a couple times and then winds it up"
        elseif tmpact == 18 then tmract = "act tries to draw a picture on the bar with some of the spilled drinks"
        elseif tmpact == 19 then tmract = "act pulls out his cigarette case and distractedly plays with it for a while before putting it back"
        elseif tmpact == 20 then tmract = "stretch"
        elseif tmpact == 21 then tmract = "act attempts to clean one of his boots with a bit of his shirt, somehow making both dirtier for his effort"
        elseif tmpact == 22 then tmract = "act mutters something about wattered down spirits under his breath"
        elseif tmpact == 23 then tmract = "squint"
        elseif tmpact == 24 then tmract = "act flips a couple of coins to the barkeep to cover his tab"
        elseif tmpact == 25 then tmract = "act rummages through his numerous pockets looking for something, then gives up and shrugs"
        end	 --if
        tmin = math.floor(tmrstg/60)
        tsec = tmrstg - (60*tmin)
        AddTimer ("Alive", 0, tmin, tsec, tmract, timer_flag.Enabled + timer_flag.OneShot, "")
end


What I want to happen is Callit () to be called after the timer added by AddTimer triggers from within the plugin.
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 06 Sep 2015 (UTC)
Message
You can have a timer "send to script" so it can both send something to the MUD, and do something else.

However it looks like you want to do something repeatedly, so just make a timer that fires repeatedly and not just once. Inside the timer you have a script (not just a single "send") that chooses a message at random and sends it.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Solamain   (2 posts)  Bio
Date Reply #2 on Sun 06 Sep 2015 10:39 AM (UTC)
Message
And how would I set up the timer to fire on random intervals?
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #3 on Sun 06 Sep 2015 08:12 PM (UTC)
Message
One approach would be, when the timer fired, to use SetTimerOption to change the timer interval (eg. option "minute"). You may need to do a ResetTimer afterwards to have it recalculate the finish time.


Template:function=SetTimerOption SetTimerOption

The documentation for the SetTimerOption script function is available online. It is also in the MUSHclient help file.




Template:function=ResetTimer ResetTimer

The documentation for the ResetTimer script function is available online. It is also in the MUSHclient help file.




Another approach would be to have the timer fire every (say) 5 seconds. When it fires, make a decision about whether to show the emote, or do nothing, based on a random roll. That saves fiddling with the timer values.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Donecce   (16 posts)  Bio
Date Reply #4 on Sat 12 Sep 2015 06:02 PM (UTC)

Amended on Sat 12 Sep 2015 06:03 PM (UTC) by Donecce

Message
Solamain said:

and any input on streamlining this code is welcome.


tmract =
{ "gaze",
"act grabs some peanuts and begins snacking on them",
"act stretches and adjusts his position on his stool",
"etc",
"etc etc",
}

...

AddTimer ("Alive", 0, tmin, tsec, tmract[math.random(1, 25)], timer_flag.Enabled + timer_flag.OneShot, "")

Would require a slightly different approach to handle some of those special cases in
your example. But the idea is to look up stuff from a table instead of using
an if-then expression.
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.


15,569 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.