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.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ 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 page


Posted by Tianxz   (30 posts)  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.


Top

Posted by Tianxz   (30 posts)  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.
Top

Posted by Twisol   USA  (2,257 posts)  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
Top

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


16,952 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.