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

Posted by porridge cat on Wed 09 May 2007 11:50 AM — 10 posts, 41,614 views.

#0
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?

USA #1
You can just use return to leave the current code chunk, as opposed to a loop block.
#2
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?
Netherlands #3
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.
Amended on Wed 09 May 2007 01:24 PM by Worstje
Australia Forum Administrator #4
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?
USA #5
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.
#6

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!





Australia Forum Administrator #7
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
#8
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!!!

USA #9
[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.