Page 1 of 1
Script Help - Busy Wait and If Statements
Posted: Sun Apr 12, 2015 8:56 am
by RexInvictus
Never done Lua scripting before and I've tried with two separate MUD clients now to get this working.
Essentially, what I want to do is go through a 6 x 7 grid, do a command on each point on this grid, delay, do another command and then move. For the life of me, I cannot seem to do this. Every time I try to do this, it causes Mudlet to hang and then crash without an error message.
Some pseudocode of what I want to do:
while (always)
for (three times)
for (seven times)
send("command")
wait("x[2/3] seconds") -- command takes a short time to take effect, so needs this delay
send("other command")
send("go south")
end
send("go east")
for (seven times)
send("command")
wait("x[2/3] seconds") -- command takes a short time to take effect, so needs this delay
send("other command")
send("go north")
end
send("go east")
end
for (seven times)
send("go west") -- resets to starting point on grid
end
end
I am using Mudlet 3.0 delta.
Re: Script Help - Busy Wait and If Statements
Posted: Mon Apr 13, 2015 8:56 pm
by Vadi
while always will run forever and you don't seem to have any start conditions... so as soon as you start it, it runs, and nothing else gets done at all.
Re: Script Help - Busy Wait and If Statements
Posted: Tue Apr 14, 2015 6:41 am
by RexInvictus
Vadi wrote:while always will run forever and you don't seem to have any start conditions... so as soon as you start it, it runs, and nothing else gets done at all.
I mean, while (always) was an example. :p
How would you recommend I do the rest?
Re: Script Help - Busy Wait and If Statements
Posted: Tue Apr 14, 2015 4:08 pm
by Akaya
Here's the proper way to set up a for loop.
-- this will send go west 7 times
for i=1, 7 do
send("go west")
end
And here is a tempTimer, equivalent to the wait command.
-- this will send go west after a 7 second delay
tempTimer( 7, [[send("go west")]])
Re: Script Help - Busy Wait and If Statements
Posted: Thu Apr 16, 2015 7:29 am
by RexInvictus
Akaya wrote:Here's the proper way to set up a for loop.
-- this will send go west 7 times
for i=1, 7 do
send("go west")
end
And here is a tempTimer, equivalent to the wait command.
-- this will send go west after a 7 second delay
tempTimer( 7, [[send("go west")]])
It was pseudocode. :p I did eventually work out how to do for loops.
tempTimer doesn't really work with for loops. If I do:
-- this will send go west 7 times
for i=1, 7 do
-- this will send go west after a 7 second delay
tempTimer( 7, [[send("go west")]])
end
All that happens is that it sends "go west" 7 seconds afterwards (and then 6 times more at like 7.2 seconds, 7.4 seconds etc). Maybe I'm an idiot, but I can't manage wait functionality with it.
Re: Script Help - Busy Wait and If Statements
Posted: Thu Apr 16, 2015 3:48 pm
by Akaya
Here's a bit more info on the tempTimer function and how it works:
http://wiki.mudlet.org/w/Manual:Introduction#Timers
Re: Script Help - Busy Wait and If Statements
Posted: Mon Apr 20, 2015 2:30 pm
by Silvine
could try
tempTimer( 7*i, [[send("go west")]])
Re: Script Help - Busy Wait and If Statements
Posted: Fri May 01, 2015 1:39 pm
by Jor'Mox
This makes a generic function that you can use to send commands with waits in between them. The first time is optional (0 is inserted if none is provided), but after that, it needs a number (time to wait in seconds) followed by a string (command to send to the game). Might be useful for what you are looking to do.
Like so: sendQueue('w',0,'s',0,'open west',1,'w',1,'say hello')
local runQueue
function runQueue(tbl)
local info = table.remove(tbl,1)
if info then
local run = function()
send(info[2])
runQueue(tbl)
end
tempTimer(info[1], run)
end
end
function sendQueue(...)
local tbl = {}
if type(arg[1]) ~= "number" then
table.insert(arg,1,0)
end
for k = 1,#arg,2 do
tbl[(k + 1) / 2] = {arg[k],arg[k+1]}
end
runQueue(tbl)
end