I must be doing something wrong

Posted by Tcoffeep on Sat 10 Jan 2009 07:26 AM — 8 posts, 30,776 views.

Canada #0
Hello, I have been tinkering with lua for a little bit, and am still trying to get a grip on things. However, I can't seem to figure out why I keep making Mushclient crash whenever I run this function :


area = {"w", "w", "sw", "w", "w", "w", "s", "s", "sw", "s",
 "se", "s", "s", "se", "e", "ne", "e", "n", "n", "w", "w", "nw", "n", "ne", "e", "e", "s", "s", "se", "ne", "ne", 
"se", "se", "s", "s", "e", "e", "e", "e", "se", "e", "e", "ne", "ne", "n", "n", "ne", "ne", "n", "n", "nw", "nw", 
"s", "n", "nw", "nw", "w", "w", "s", "s", "s", "s", "se", "e", "s", "s", "sw", "s", "e", "se", "e", "e", "ne", "ne", 
"n", "n", "ne", "ne", "w", "n", "s", "e", "n", "n", "nw", "nw", "s", "n", "nw", "s", "ne", "sw", "s", "e", "e", "w", 
"w", "w", "n", "s",  "s", "n", "w", "n", "s", "s", "se", "e", "s", "n", "w", "sw", "ne", "nw", "n", "w", "w", "e", 
"e", "nw", "w", "sw", "w", "w", "e", "e", "ne", "e", "n", "ne", "e", "w", "sw", "s", "se", "sw", "w", "e", "sw", "w", 
"w", "w", "w", "n", "s", "s", "w", "n", "n", "w", "s", "s", "ne", "w", "w", "w", "nw", "n", "n", "ne", "n", "n", "nw", 
"nw", "w", "sw"}

function DoWalk ()
  if misc.name == "y" and stats.bal == "eb" and not mob.attack then
    for i=1,166 do
      CheckRoom()
      if mob.room then
        i = i + 1
        mob.room = false
        Send(area[i])
      else
       EnableTimer("basher", true)
      end
    end
  end
end

function CheckRoom ()
  mob.check = true
  Send("look")
  DoAfterSpecial (3, 'mob.check=false', sendto.script)
end


Where am I going wrong? Is it too large of a loop?
Amended on Sat 10 Jan 2009 07:34 AM by Tcoffeep
USA #1
What's the error that you get?
Amended on Sat 10 Jan 2009 07:34 AM by WillFa
Canada #2
None.

I run the function, and it stops responding, and it doesn't do anything until I close it via CTRL-ALT-DELETE.

I tried tracing, but, as I've said, it crashes, and it stops responding.
USA #3

The only thing I see right now is

Send(Area)

Should probably be
Send(Area[i])


Currently you're trying to send the entire table, which should get an error in the Send function (expected string, got table)

What does the CheckRoom() function do? Could you be getting in an infinite loop in there? Does CheckRoom call DoWalk which would also cause a loop... ?
Canada #4
It just checks the room in case anyone else is in there.

It is, but the forums removed them because it probably was assumed to be code or something.

edit : i edited a few things because of some mistakes. still crashes, though.
USA #5
Modifying i in the loop is 'undefined' behavior in Lua.


The way this is written, I don't think it'll do what you want... It looks like you're trying to write a stepper. The loop runs regardless, as fast as possible.

so you'll walk into a room, send look, and walk into the next room before the mu even sends output back.

To make a stepper, you'll need a little bit more complex code using a coroutine. Coroutines let you pause a loop or funtion, and then pick up where you left off.

Nick's wait.lua module shows how they can be used.

Basic logic of it would be:

f = coroutine.wrap(function ()
  if misc.name == "y" and stats.bal == "eb" and not mob.attack then
    for i=1,166 do
      CheckRoom()
      coroutine.yield()
      if mob.room then
        mob.room = false
        Send(area[i])
      else
       EnableTimer("basher", true)
      end
    end
  end
end )

Either your basher timer or prompt trigger would then call f() to resume, after the mud's gotten a chance to send you enough text to trigger on a mob being in the room.

Amended on Sat 10 Jan 2009 07:50 AM by WillFa
Canada #6
I'll try adding in


v = i

Send(area[v])


and see if that works?
Canada #7
I'll give it a shot. Thanks.

You were right. I have a lot to learn yet!