Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Break used to break a loop chunk, how to break a script?

Break used to break a loop chunk, how to break a script?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by porridge cat   (16 posts)  Bio
Date Wed 09 May 2007 11:50 AM (UTC)
Message
if "%2" == "" or "%2" ~= "" then
break the whole script!!
end

for i, %1 do -- Because the whole script was stopped,
something -- so the script of the left won't be executed!!
end -- and won't prompt an error message either.

To sum up, is there a command to break the whole script?

Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Wed 09 May 2007 12:08 PM (UTC)
Message
You can just use return to leave the current code chunk, as opposed to a loop block.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by porridge cat   (16 posts)  Bio
Date Reply #2 on Wed 09 May 2007 01:00 PM (UTC)
Message
I've tried to use return.
But using return still had some error messages appeared.

Is there an easier command to jump out of the script?
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #3 on Wed 09 May 2007 01:23 PM (UTC)

Amended on Wed 09 May 2007 01:24 PM (UTC) by Worstje

Message
What do you mean by 'out of the script'?

return will stop the execution of the trigger, assuming you are editing the particular function hooked up to it.

If you want to cease your entire scripts execution, you could either have a global flag in your plugin which you check before you try to do something, or you could turn all triggers and aliases off using a loop, effectively stopping its execution.

If you can't figure it out, paste some other code including the error message you are getting. That'll help us to help you find the problem.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #4 on Wed 09 May 2007 04:18 PM (UTC)
Message
Quote:

But using return still had some error messages appeared.


It always helps to give the error message.

For deeply nested functions doing a return will not exit the whole script, just the function it is in.

If this is what is happening, using a protected call (pcall) will do the trick. Here is an example:


function foo (a)
  if a == "stop" then
    error "exit_function"
  end -- some error condition
end -- foo

function bar (x)
  foo (x)
end -- bar

local ok, err = pcall (bar, "stop")

if not ok then  -- error
 if string.match (err, "exit_function") then
   return
 end -- expected error
 error (err, 2) -- some other error
end -- error


In this example, I am using pcall to call function bar, which then calls foo, and in foo if the argument is "stop" then it raises an error. This immediately jumps it out of all the nested functions back to the line after the pcall.

Then we test for the word "exit_function" in the error message. If we get that, we simply exit altogether. Otherwise it might be some other sort of error, so we raise the error again.

However this is more complex than you need if you are not using nested functions. Simply return from the script as David suggested:


if "%2" == "" or "%2" ~= "" then
  return
end


Although in this particular case the test looks a bit strange. That condition would always be true wouldn't it?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #5 on Wed 09 May 2007 05:22 PM (UTC)
Message
Quote:
if "%2" == "" or "%2" ~= "" then
break the whole script!!
end

for i, %1 do -- Because the whole script was stopped,
something -- so the script of the left won't be executed!!
end -- and won't prompt an error message either.


Are these two pseudo code snippets together, or separate examples? If you want the script to break and then restart, you could just break the if statement, then do some bounds checking afterwards, or set a flag that says that it broke whatever control you had previous.

Having some specific examples might also help, post your code within [code] [\code] flags and copy down your error message. If it's something you don't want to show, just make a quick example rather than using pseudo code so we can see exactly what is going on.

It is much easier to fight for one's ideals than to live up to them.
Top

Posted by porridge cat   (16 posts)  Bio
Date Reply #6 on Sun 13 May 2007 06:29 AM (UTC)
Message

if "hello" == "hello" then
print ("hello")
-- then use the function Nick gave me to jump out of the entire script!!
function foo (a)
  if a == "stop" then
    error "exit_function"
  end -- some error condition
end -- foo

function bar (x)
  foo (x)
end -- bar

local ok, err = pcall (bar, "stop")

if not ok then  -- error
 if string.match (err, "exit_function") then
   return
 end -- expected error
 error (err, 2) -- some other error
end -- error

end

for i, %1 do
something
end
[\code]

Compile error
World: mud
Immediate execution
[string "Alias: "]:25: '<name>' expected near 'do'


If there is a wrong syntax followed, it won't work.

What else stuff I must add to skip wrong syntax and jump out of the script in the mean time? 

Finally, for the Nick's kind heart of offering functions, thank you ever so much!




Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #7 on Sun 13 May 2007 06:49 AM (UTC)
Message
Can you show us the whole alias? It is hard to suggest things when you take snippets of what I have suggested and plug stuff around it.


http://mushclient.com/copying

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by porridge cat   (16 posts)  Bio
Date Reply #8 on Sun 13 May 2007 07:35 AM (UTC)
Message
I just think it will be very convenient to have a funcion like 'jump', but now I give up the idea. Maybe it's an impossible task. Thank you anyway! Thank you!!!

Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #9 on Sun 13 May 2007 01:05 PM (UTC)

Amended on Sun 13 May 2007 01:07 PM (UTC) by Shaun Biggs

Message
[quote[
for i, %1 do
something
end
[\code]

Compile error
World: mud
Immediate execution
[string "Alias: "]:25: '<name>' expected near 'do'

This for loop doesn't have all the correct declarations in it. In a for loop, you need something to go to and from, where here you just have one argument passed. That's causing the error in line 25, if that's the whole script part of the alias.

for i = 1,"%1" do
  -- something
end

would be a valid call, although it might not do what you're looking to do. Copying and pasting the whole alias like Nick suggested is going to give a lot more information to track down what you're trying to do.

It is much easier to fight for one's ideals than to live up to them.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


29,804 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.