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 » Sun Apr 12, 2015 8:56 am

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: 4702
Joined: Sat Mar 14, 2009 3:13 pm

Re: Script Help - Busy Wait and If Statements

Post by Vadi » Mon Apr 13, 2015 8:56 pm

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 » Tue Apr 14, 2015 6:41 am

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: 412
Joined: Thu Apr 19, 2012 1:36 am

Re: Script Help - Busy Wait and If Statements

Post by Akaya » Tue Apr 14, 2015 4:08 pm

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 » Thu Apr 16, 2015 7:29 am

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: 412
Joined: Thu Apr 19, 2012 1:36 am

Re: Script Help - Busy Wait and If Statements

Post by Akaya » Thu Apr 16, 2015 3:48 pm

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 » Mon Apr 20, 2015 2:30 pm

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

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

Re: Script Help - Busy Wait and If Statements

Post by Jor'Mox » Fri May 01, 2015 1:39 pm

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

Who is online

Users browsing this forum: No registered users and 12 guests