Cancel cEgo.Walk(x,y,eBlock);

Started by Barn Owl, Wed 19/03/2025 03:22:33

Previous topic - Next topic

Barn Owl

Hi all, thanks again for helpful responses lately! You guys are awesome. Again, my bad if these questions are rudimentary stuff from the manual.

Today's question is: Let's say cEgo is on the lower left of the screen, and the player clicks a command for him to walk to the upper right... Now, while cEgo is in mid-stride, the player decides they want to cancel the action... How would that be added to the script? So if the player clicks while cEgo is en route, cEgo will stop walking and not complete the function that he would have, had he reached his x,y destination?

Thanks again, & feel free to tell me to look in the manual, I know I'm swiss cheese 101 over here with my level of AGS script knowledge. All help is a huge help!


Crimson Wizard

#1
I presume that this is about a interaction sequence and not a regular walk, and you have other commands that depend on the walk's outcome.

The idea in a nutshell is this: you need a custom walk function that will start a non-blocking walk and run a waiting loop while the character is moving, and also repeatedly check for the key or mouse presses using IsKeyDown and/or Mouse.IsButtonDown. This function should also return a boolean value, telling whether the walk was complete or interrupted by player.

In a very primitive sense, it may look like this:
Code: ags
bool MyWalk(Character *c, int x, int y, WalkWhere where)
{
     c.Walk(x, y, eNoBlock, where);
     bool was_interrupted = false;
     while (c.Moving && !was_interrupted )
     {
           Wait(1);
           if (Mouse.IsButtonDown(eMouseLeft))
                was_interrupted = true;
     }
     return was_interrupted;
}
and then used like this:
Code: ags
if (!MyWalk(player, .....))
    return; // was interrupted, don't continue the sequence

There are already couple of modules that do this sort of interruption and report result. You may try using them, if you don't want to write your own code for this:
https://www.adventuregamestudio.co.uk/forums/modules-plugins-tools/module-cancellablewalk-1-0/
https://www.adventuregamestudio.co.uk/forums/modules-plugins-tools/module-gotthere-0-5/

EDIT: Above is the easiest solution to do, but it has its limits. The biggest one is that the player won't be able to interact with anything else, nor open a menu, or save/restore the game, etc. If that's not needed, then above solution will work fine.
On another hand, if you need a way to sequence some actions, and run them non-blocking, so that player could still work with GUIs at the same time, then the solution has to be largerly different. But I will skip that until this question is cleared.

Barn Owl

#2
Hey Crimson Wizard, your response is much appreciated once again.

Your presumption is correct, my question is regarding an interaction sequence that includes a walk across the room.

What I want to avoid is, the player clicks on a door with a function of
{cEgo.walk(400,300, eBlock);
cEgo.ChangeRoom(X, X, Y):}

And for this example, we'll say the walk to (400,300) is a slow arduous journey, and the player didn't even intend to click the sequence. The player would rather click-to-stop the function than proceed. Something like SkipAnyKeyOrMouse, yet for the function in eBlock progress. Is there any quick script I'm missing to stop a function during an eBlock?

Much appreciation & respect for taking the time to read, and for your work and knowledge.

I'll take a look at those mods as well, thanks for that recommendation, that may be the solution.


Edit: That script is a big help, thank you. I am a complete beginner with AGS script, yet I see how to potentially implement that.

Crimson Wizard

#3
Quote from: Barn Owl on Wed 19/03/2025 05:03:07The player would rather click-to-stop the function than proceed. Something like SkipAnyKeyOrMouse, yet for the function in eBlock progress. Is there any quick script I'm missing to stop a function during an eBlock?

You would have to write your own interruption handling, because not only you need to be able to stop a blocking action, but also:
1. You need to distinguish when this is allowed and when not;
2. You need to check the result after the walk is complete and proceed or cancel the sequence depending on that result.

I added the script example to my post, but that's essentially a simplified version of what is inside of these modules.

SMF spam blocked by CleanTalk