Script Help - Busy Wait and If Statements

Post Reply
RexInvictus
Posts: 3
Joined: Sun Apr 12, 2015 8:47 am

Script Help - Busy Wait and If Statements

Post 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:
Code: [show] | [select all] lua
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.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Script Help - Busy Wait and If Statements

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

RexInvictus
Posts: 3
Joined: Sun Apr 12, 2015 8:47 am

Re: Script Help - Busy Wait and If Statements

Post 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?

User avatar
Akaya
Posts: 414
Joined: Thu Apr 19, 2012 1:36 am

Re: Script Help - Busy Wait and If Statements

Post by Akaya »

Here's the proper way to set up a for loop.
Code: [show] | [select all] lua
-- 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.
Code: [show] | [select all] lua
-- this will send go west after a 7 second delay
tempTimer( 7, [[send("go west")]])

RexInvictus
Posts: 3
Joined: Sun Apr 12, 2015 8:47 am

Re: Script Help - Busy Wait and If Statements

Post by RexInvictus »

Akaya wrote:Here's the proper way to set up a for loop.
Code: [show] | [select all] lua
-- 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.
Code: [show] | [select all] lua
-- 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:
Code: [show] | [select all] lua
-- 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.

User avatar
Akaya
Posts: 414
Joined: Thu Apr 19, 2012 1:36 am

Re: Script Help - Busy Wait and If Statements

Post by Akaya »

Here's a bit more info on the tempTimer function and how it works: http://wiki.mudlet.org/w/Manual:Introduction#Timers

Silvine
Posts: 142
Joined: Sat Oct 23, 2010 2:36 pm

Re: Script Help - Busy Wait and If Statements

Post by Silvine »

could try
Code: [show] | [select all] lua
tempTimer( 7*i, [[send("go west")]])
Mapper of Bedlam

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: Script Help - Busy Wait and If Statements

Post 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')
Code: [show] | [select all] lua
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

Post Reply