Eloni said: The code compiles error free, but there is no pause between sleeping and resting.
I'm splitting hairs here, but Lua isn't a compiled language. The Lua interpreter parses the script and turns it into bytecode for its own virtual machine, which it then executes. But it's never actually compiled into machine code.
Eloni said: I think a wait function is ideal in the regen() function, but I can't tell until I test it. I might be wrong, but: It bothers me that the bot could get attacked while sleeping, and it wouldn't be able to respond because the 'wait' function has paused the script.
Maybe a timer that runs separately from the 'waited' code that constantly checks to see if fighting = true, and if so 'unwaits' the code... somehow. I haven't experimented with timers yet, so I'm not sure how they work, but I assume they're ran from the mud, and not from inside the lua script.
Nick ninja'd me, but the reason your waiting doesn't work is because wait.make creates a coroutine and runs it. When you call wait.time() inside it, it yields that coroutine back to the original context (which would be within wait.make, which returns at that point). wait.time() creates a temporary timer within MUSHclient, and the timer's script resumes the waiting coroutine.
Also keep in mind that MUSHclient is not multithreaded. When control is within a script, nothing else happens (including triggers, aliases, timers, etc.) until the script returns back. Coroutines let you save your context within a multi-step task. In this case - as Nick showed - your first (and only) task is to wait some time and then wake up. But anything outside the wait.make() block will be executed as soon as your coroutine yields control, which you seem to send "wake" immediately.
Coroutines are really fun and highly useful, they just take some extra thought and practice to build up that essential mental model. |