Nested function calls

Posted by Winddancer on Wed 02 Nov 2022 09:59 PM — 3 posts, 11,425 views.

#0
In my mud, there is a delay when you read a page in a book before you can continue. Naturally I wanted to circumvent this with an alias and 2 functions.

What I want to do:
Step 1 Open the book
Step 2.1 read a page
Step 2.2 wait 5 seconds
Step 2.3 turn the page
Step 3 repeat Step 2 as often as I indicated in the alias call.

What I did:
I created the following script functions:

function readPage()
        Send ("read book")
        DoAfterSpecial (5, "turn page", sendto.world)
end --readPage

function readBook(x)
	local i=1
	while i <= x
	do
		readPage()
		i=i+1
	end -- do
end --readBook

  <alias
   match="readBook *"
   enabled="y"
   group="books"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>Send("open book")
readBook(%1)
</send>


What happens when I call the alias with "readBook 4":
I open the book
I send 4 times read book to the world
The first pages gets read.
I get 3 messages from the mud that I am reading the book. (because the delay is not yet over)
I get a message that I am finished reading the page
I send 4 times turn page to the world.
I turn a page 4 times

Where is my error? What did I do wrong?
When I call the readPage() function on its own, it works as intended:
I read the page.
After 4 seconds I get the message that I am finished reading the page.
I send turn page to the world.
The page gets turned.
Amended on Wed 02 Nov 2022 10:22 PM by Winddancer
Australia Forum Administrator #1
Check out my page about building pauses into scripts: http://www.gammon.com.au/forum/?id=4956

In your case your alias could look like this:


<aliases>
  <alias
   match="readBook *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>

if not tonumber ("%1") then
  print "Number of pages not numeric"
  return
end -- if

require "wait"
wait.make (function ()

  Send("open book")

  -- read each page
  for i = 1, %1 do

    Send ("read book")
    wait.time (5)
    Send ("turn page")
  end -- for

  print "Book reading done"

end) 

</send>
  </alias>
</aliases>



Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.
#2
Thanks. Must have overlooked that post of yours.