How to run code after you stop your script? #383
|
Let's say you have a py script: def Run():
API.SysMsg("Test script")
while not API.StopRequested:
Run()
API.Pause(1)
API.SysMsg("Script stopped!")If you were to stop this script using the script manager Stop button, the last line "Script stopped" would never run. So how can you get that last line to run after a script is stopped? We need to use a try/catch or try/finally, this is because the python engine we use ( Putting your code inside a try statement will "catch" that error and allow the script to continue to run depending on how you handle it. For our case here we just want some sort of code to run after the stop button was hit: import API
def Run():
API.SysMsg("Test script")
try:
while not API.StopRequested:
Run()
API.Pause(1)
finally:
API.SysMsg("Script stopped!") |
Replies: 2 comments 2 replies
|
I've got a chunk highlighter script, which turns the 8x8 chunk you're in yellow. It's neat for mining in caves. I'd like it to remove the highlight when stopped. Previously, I had it save a persistent variable, and when run a second time, it read the persistent variable and cleared the chunk highlight. I'd updated to the Since the v5.0.0 update, this doesn't work for me any more. Is there a chance we could get an |
|
We've now implemented API.OnStop: https://tazuo.org/legion/legionapi/#onstop |
We've now implemented API.OnStop: https://tazuo.org/legion/legionapi/#onstop