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

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Why It can't run in the order
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Why It can't run in the order
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please)
Maximum of 6000 characters. Text only please, no HTML.
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Posted by Nick Gammon   Australia  (19,383 posts)  [Biography] bio   Forum Administrator
Date Sat 09 Jul 2011 09:47 PM (UTC)  quote  ]
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

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Sat 09 Jul 2011 08:01 PM (UTC)  quote  ]
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 Tianxz   (30 posts)  [Biography] bio
Date Sat 09 Jul 2011 02:36 PM (UTC)  quote  ]
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 Tianxz   (30 posts)  [Biography] bio
Date Sat 09 Jul 2011 02:33 PM (UTC)  quote  ]
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

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.


1,718 views.

[New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]