Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: lafouine88 on Wed 20/01/2021 20:55:42

Title: Waiting for the animation to end
Post by: lafouine88 on Wed 20/01/2021 20:55:42
Hey guys.
Do you know if there is a way to wait for an animation to end without blocking the game?
For instance:
Code (ags) Select

Player.LockView(2);
Player.Animate(1,5,eOnce,eNoBlock);
"Someway to wait for it to end"
Player.UnlockView();


It's been bugging me for a while now and I kept avoiding the problem but there is maybe an easier way:)

Thanks in advance
Title: Re: Waiting for the animation to end
Post by: Crimson Wizard on Thu 21/01/2021 07:54:28
If you just need game update running, but keep player unable to progress the game, you could try following solution:

Code (ags) Select

Player.LockView(2);
Player.Animate(1,5,eOnce,eNoBlock);
while (Player.Animating)
{
     Wait(1); // wait minimal time to update the game in background and keep checking if it's still animating
};
Player.UnlockView();


This runs the Wait(1) in the loop until animation finishes.


If on another hand you want to simply run the animation for some time while player can continue to play the game, for that case there's repeatedly_execute (https://adventuregamestudio.github.io/ags-manual/RepExec.html) and similar functions, where you test when something ended or happened. These functions runs periodically, and eventually the condition will be met.

Something like that:
Code (ags) Select

function repeatedly_execute()
{
    // if player is locked in View 2 and not animating anymore, then unlock the view
    if (!Player.Animating && player.View == 2)
    {
         Player.UnlockView();
    }
}


Note that in the room script "repeatedly_execute" does not work, and you have to instead use room's event.
Title: Re: Waiting for the animation to end
Post by: lafouine88 on Thu 21/01/2021 11:04:09
Hey crimson, thanks for the tip. That is more or less the kind of trick I used but yours seems quite easier. It's the second one I was looking for, I don't want the player to be blocked while the animation plays. So I'll try it.

Although I did'nt understand the last part of your message. You mean that I can't do this with the repeatedly execute from the room? I don' t get why:/ Then (sorry I still suck at scripting) I must create a "repeatedly execute" function but how to apply it ?

Thanks again
Title: Re: Waiting for the animation to end
Post by: Crimson Wizard on Thu 21/01/2021 11:13:23
Quote from: lafouine88 on Thu 21/01/2021 11:04:09
Although I did'nt understand the last part of your message. You mean that I can't do this with the repeatedly execute from the room? I don' t get why:/ Then (sorry I still suck at scripting) I must create a "repeatedly execute" function but how to apply it ?

The "repeatedly_execute" and similar "repeatedly_" functions are called automatically by the engine when you put them in any script module, but not in the room script.
Rooms must have the function to be connected to event in the events table (this is same as you connect functions for "after fade in" event, or hotspot interactions, for example).

You may still call it "repeatedly_execute" if you prefer, but default name created by editor is something like "room_RepExec", don't really know why.
Title: Re: Waiting for the animation to end
Post by: lafouine88 on Thu 21/01/2021 22:00:31
OK thanks a lot for the tip. I'll try this :)