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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Why It can't run in the order

Why It can't run in the order

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


Posted by Tianxz   (30 posts)  [Biography] bio
Date Sat 09 Jul 2011 02:33 PM (UTC)
Message
function a()
DoAfter(1,"e")
end

function b()
DoAfter(0.1,"hp")
a()
DoAfter(0.1,"l")
end
The result is
hp then l then e

But I just want to it can run in the order
first:hp
after 1s then: e
then:l

Of course, If I change the DoAfter(0.1,"l") to DoAfter(2,"l"), the result is same I want. If I don't know how long it need to run for function a, how can I correct it? Thank you.


[Go to top] top

Posted by Tianxz   (30 posts)  [Biography] bio
Date Reply #1 on Sat 09 Jul 2011 02:36 PM (UTC)
Message
If some Doafter is nested in function a, it will be more complex. I think the important thing is how can let the code run in the order.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #2 on Sat 09 Jul 2011 08:01 PM (UTC)
Message
DoAfter() sets a timer with the delay you specify, and immediately continues with the script. That means that in this script, "hi" is shown first:
DoAfter(0.1, "lol")
DoAfter(0.2, "rofl")
print("hi")

And then as time passes, MUSHclient runs the timers as they're fired, so "lol" would come after 0.1 seconds, and 0.1 seconds after that (i.e. 0.2 seconds from being created) "rofl" would show up.

I'd suggest doing this:
function a(delay)
  DoAfter(delay, "e")
end

function b()
  DoAfter(0.1, "hp")
  a(1.1)
  DoAter(1.2, "l")
end


You could also try using the "wait.lua" module, which uses coroutines to give you a pause mechanism closer to what you expect.

require "wait"

function a()
  Send("e")
end

wait.make(function()
  wait.time(0.1)
  Send("hp")
  wait.time(1)
  a()
  wait.time(0.1)
  Send("l")
end)


Template:post=4956 Please see the forum thread: http://gammon.com.au/forum/?id=4956.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Sat 09 Jul 2011 09:47 PM (UTC)
Message
Twisol is quite right, using the wait module is a much better way of doing it. In fact, you don't even need the second wait.time (0.1) because things will be executed in sequence.

http://www.gammon.com.au/forum/?id=4956

and:

http://www.gammon.com.au/forum/?id=4957

- 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.


14,761 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]