Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20

Thread: Calling function upon interruption of ASH script

  1. #1
    Senior Member
    Join Date
    Apr 2018
    Posts
    133

    Default Calling function upon interruption of ASH script

    Is it possible to have an ASH script catch an “abort” instruction and run a function to clean up, rather than simply stopping immediately?

    Alternatively, is it possible to accept user input without halting unless it is received? Like a version of wait() where it is listening for instructions (e.g. “cancel”) until the duration is up?

    I know it is possible to use user_confirm() with a timeout, but that creates a dialog box rather than accepting CLI input. I want my program to run on a headless system occasionally.

  2. #2

  3. #3
    Senior Member
    Join Date
    Apr 2018
    Posts
    133

    Default

    What would I put in ‘try’ to get this behavior?

  4. #4
    Senior Member
    Join Date
    Jun 2016
    Posts
    260

    Default

    You put everything you want to 'try' in the try block, and then when it aborts the script will continue with the finally block (and everything after that).

    For example:
    Code:
    try {
    	set_property("restUsingChateau","false");
    	if ( my_mp()>my_maxmp()-10000 )
    		cli_execute("maximize mp, equip brimstone bracelet, equip pantsgiving, -10000 stackable mana cost, -equip bjorn, -equip crown of thrones, -familiar");
    	else if (equipped_item($slot[pants]) != $item[pantsgiving])
    		equip($item[pantsgiving]);
    	while ( get_property("timesRested").to_int() < total_free_rests() )
    	{						
    		cli_execute("rest free");
    		libramBurn();
    	}
    }
    finally set_property("restUsingChateau","true");
    Last edited by the dictator; 07-09-2018 at 01:54 PM.

  5. #5
    Senior Member
    Join Date
    Apr 2018
    Posts
    133

    Default

    That’s the issue: I can’t get CLI input and have it abort in the absence of said input.

    Or do you mean a user-given abort command will cause it to proceed to “finally”?
    Last edited by Saklad5; 07-10-2018 at 08:13 PM.

  6. #6
    Senior Member
    Join Date
    Apr 2018
    Posts
    133

    Default

    I just thought of an incredibly stupid hack to at least stop a script gracefully: have it call another ASH script that is hard-coded to return true. I can later change this other script to return false during execution.

    This is obviously not a good idea for anything being used by others, of course.


    Edit: This doesn’t appear to be working. Does the ASH interpreter import all the scripts at launch, rather than as they are called? Because that would sink this plan entirely.
    Last edited by Saklad5; 07-10-2018 at 08:18 PM.

  7. #7
    Developer fronobulax's Avatar
    Join Date
    Feb 2009
    Location
    Central Virginia, USA
    Posts
    4,202

    Default

    Edit: This doesn’t appear to be working. Does the ASH interpreter import all the scripts at launch, rather than as they are called? Because that would sink this plan entirely.
    Originally Posted by Saklad5 View Post
    I think it does although if anyone says I am wrong, then I am.

    Perhaps reading and writing a map?

    I'm confused about your application. You can avoid the dialog prompt by entering parameters on the command line. If you are trying to run something headless and somehow make it stop and exit cleanly then your idea of having it periodically poll an external file and exit based upon that file is probably your best bet. But you obviously need access to the underlying filesystem from outside of mafia.
    You just vehemently agreed with me
    Originally Posted by Veracity View Post
    I agree with frono.
    Originally Posted by Veracity View Post

  8. #8
    Senior Member
    Join Date
    Jun 2016
    Posts
    260

    Default

    That’s the issue: I can’t get CLI input and have it abort in the absence of said input.

    Or do you mean a user-given abort command will cause it to proceed to “finally”?
    Originally Posted by Saklad5 View Post
    Yes, a user given abort command (such as hitting escape) is treated as any other, and as such will only break you out of the try block.

  9. #9
    Developer fronobulax's Avatar
    Join Date
    Feb 2009
    Location
    Central Virginia, USA
    Posts
    4,202

    Default

    Yes, a user given abort command (such as hitting escape) is treated as any other, and as such will only break you out of the try block.
    Originally Posted by the dictator View Post
    What are the user given abort commands that can be entered via the gCLI when a script is running and running headless? I didn't recall any but that is probably the easiest solution.
    You just vehemently agreed with me
    Originally Posted by Veracity View Post
    I agree with frono.
    Originally Posted by Veracity View Post

  10. #10
    Developer
    Join Date
    Aug 2009
    Posts
    2,822

    Default

    I think it does although if anyone says I am wrong, then I am.
    Originally Posted by fronobulax View Post
    That doesn't sound right. My understanding of the situation is that scriptA imports scriptB; you then modify scriptB while scriptA is still running. The script (and its imports) is parsed when you execute the script, and cached (I think? Could be wrong) until the script is modified.

    Why doesn't user_confirm with a timeout work for your use case?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •