Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Bavolis on Mon 01/12/2014 01:08:03

Title: Patrol + Animation Question
Post by: Bavolis on Mon 01/12/2014 01:08:03
I have a *terrible* math and logic brain and failed basic programming in college, so bear with me. I've tried a couple of different angles on this one, but I have yet to get it working properly. I thought I was on the right track with a function and variables, but I'm having a hard time getting the script to wait for what I need. Here's the breakdown:

1. I have an engineer (not the player), who I would like to walk between three set "repair" spots and animate a "repairing" animation before moving on to the next, all done in the background and not interfering with the player. I'd like him to keep looping this cycle.
2. The player can interact with him, talk to him, etc., and afterwards, I'd like him to go back to that loop.

What I've tried:

1. I tried to repeatedly execute, but way points do not work because he doesn't care about the walk path and the walk function doesn't work because it keeps sending him the same command over and over.
2. I set up a variable for his patrol with 3 levels to the patrol.
3. I set up a function called "busywork" that I import into the room and then call - he goes to the first spot, but if I have him say "testing" when he reaches the coordinates, he says it before he walks. This would be solved by noEblock on the main character, but I don't want to halt the main script here. The idea was he'd get to the first point, I'd play the animation, then set the variable to the next and call "busywork" again, but I'm not sure how to make his own little loop wait for him to finish walking before executing the next part.

I'm guessing my approach is completely wrong, so feel free to scold me for bad scripting. I'm sure I'm guilty of much worse in my code so far, too :)

Any help would be tremendously appreciated. Thanks!
Title: Re: Patrol + Animation Question
Post by: Mandle on Mon 01/12/2014 15:09:48
With your repeatedy execute structure have you tried having the game detect the x,y location of the character and then shutting out/allowing the next way point command based on whether he has reached the last one before he can continue to the next?

I'm not sure if I completely understood the problem however...
Title: Re: Patrol + Animation Question
Post by: Dadalus on Mon 01/12/2014 22:57:13
The way I would tackle this is to break the problem into smaller chunks.

Forget about animating (just temporarily).

1.Set up two variables above all your functions x[3] and y[3] and a third one called counter.

2. Store the values of each of your walk points in the x,y variables e.g x[0]=100; y[0]=200;

3. Set your counter to zero.

4. Walk your NPC to x[counter],y[counter] (non blocking).

5. in repeatedly_execute test if NPC is moving using '.Moving' if its not moving then increase your counter by 1 (if its 3 or above set back to zero). Repeat step 4.

Try to do this first, then build upon it by adding in the animating (you can test if a character is animating using '.Animating').

I'd write the code for this if I had the time, but I think tackling it yourself would be the best approach. That way you will learn & progress.

Cheers,

Dadalus.

EDIT: In repeatedly_execute a general rule of thumb I use is to test for something before executing any commands. Otherwise you just end up sending the same command over & over & over. so in the example above

Code (ags) Select
if (cNPC.Moving)
{
    //do nothing as the character is walking
}
else
{
    //character has reached his destination

    //increase counter by 1
    counter++;
    if (counter>=3) counter=0;
    //walk to next point
    cNPC.Walk(x[counter],y[counter],eNoBlock);   
}



hope that helps (I've not tested this) :)


Title: Re: Patrol + Animation Question
Post by: Gurok on Mon 01/12/2014 23:39:39
Unfortunately, there's no really simple solution to this. I can tell you that your character talks right away because "eNoBlock" means continue running commands and walk in the background. It doesn't halt the script while it waits for the command to complete, nor does it make the script run in the background while other game cycles happen.

If you want to solve this for one particular room, the suggestions above will certainly help. The idea is that your starting script simply contains the Walk command, and subsequent commands get run when your background character reaches certain points (via repeatedly_execute).

But if you want to get really fancy...

I half-wrote a module for CassieBSG a while ago that makes it possible to program background operations sequentially. It might be of some use to you:

http://goo.gl/htUyhb

You use it like this:


cEngineer.OrderToWalk(100, 150); // Walk to the first repair point
cEngineer.OrderToAnimate(5, 0, cEngineer, eOnce); // Play the repair animation
cEngineer.OrderToWalk(200, 150); // Walk to the second repair point
cEngineer.OrderToAnimate(5, 0, cEngineer, eOnce); // Play the repair animation
cEngineer.OrderToWalk(300, 150); // Walk to the third repair point
cEngineer.OrderToAnimate(5, 0, cEngineer, eOnce); // Play the repair animation
cEngineer.FollowOrders(); // Tell the engineer to execute the above instructions in order


I haven't tested that and the values are arbitrary, but I hope you get the idea.

You might want to introduce a "paused" flag to stop the execution while the player talks to the character.

I don't know if that'd work exactly how you'd want it. You'd probably need to play around with it a bit.
Title: Re: Patrol + Animation Question
Post by: Bavolis on Tue 02/12/2014 01:04:41
These are some great replies - thank you everybody! I'm going to play around with each just as a learning experience and see what happens :)