Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Furwerkstudio on Sat 04/01/2020 19:01:59

Title: Using animation as a timer
Post by: Furwerkstudio on Sat 04/01/2020 19:01:59
Hey, I was trying to figure out what the code would look like to use an object that is animated as a "timer". That when it reaches a certain frame it triggers a situation, like say when the animation is done it triggers goes to another room unless a different object is clicked.

I did some mock coding, and wonding if this would work or is there any better way to code this.
Code (ags) Select

ObjectX.Animate(1, 1, eOnce, eNoblockk, eForward);

if (OtherObject.AnyClick) {
       player.gotoroom(x,y);
      }
else {
     game.GetViewFrame(1, 80);
     player.gotoroom(y,x);

Title: Re: Using animation as a timer
Post by: Crimson Wizard on Sat 04/01/2020 19:16:18
Generally, you wait in a loop and check for Object.Loop, Object.Frame, and perhaps Object.Animating.

How to do this depends on whether you allow some other actions while the object is animated. From your description it seems to be the second situation. But I will post example solutions for both, just in case.

Variant 1. Animate and blocking wait:
Code (ags) Select

ObjectX.Animate(1, 1, eOnce, eNoBlock);
while (ObjectX.Animating && ObjectX.Frame < 80) {
     Wait(1); // to keep game redrawn
}
player.ChangeRoom(...);


Variant 2. Animate and non-blocking wait.

In this case you need to start animation and let it go, but keep checking for an object in room's rep-exec function.

Code (ags) Select

function Room_RepExec() { // assuming you connected this function to corresponding room's event
    if (ObjectX.Animating && ObjectX.Frame == 80) {
         // object was animating and reached certain frame
         player.ChangeRoom(...);
    }
}


But since you also allow interruption, then you need to handle other object clicking somehow, for instance make a function for "Any click on object" event, and somehow cancel the timer there (stop or reset animation, or set some variable to tell that it should be ignored).