[Tutorial][Demo] Custom Animation Sequence and Fps

You must understand the Game Editor concepts, before post here.

[Tutorial][Demo] Custom Animation Sequence and Fps

Postby Bee-Ant » Wed Jul 16, 2014 5:40 pm

Hello everyone :D

I got a request a little while ago about playing animation in custom fps with pure code, so I came here to share it.
It took time to be finished since it was quite a headache :P
So, here we go...

First of all, we need to define the variables.
Write this on global code.
Code: Select all
#define FrameRate 30 //Store game's frame rate
//Check Config -> Game Properties -> Frame rate
//We use this manual configuration due to unstable "real_fps" value
//Unstable "real_fps" value will affect the animation smoothness

//Animation List
int AnimationList[5][5]=
{
    //animation1, animation2, animation3, animation4, fps
    2,8,2,8,3, //Stop Left
    3,9,3,9,3, //Stop Right
    0,4,0,6,10, //Run Left
    1,5,1,7,10, //Run Right
    //add other animation sequence here
};
//The number of the animation sequence is taken from the "animpos" of the player's animation
//And make sure the player has only one big animation, which contain all the animation it needs

//In correlation to the animation set, we need to define the facing direction as well
int direct=0; //To store Player's facing direction
int walk=0; //To store Player's walking state
int AnimationIndex=0; //To store Player's animation index
int AnimationFps=0; //To store Player's animation fps


And the PlayAnimation() function.
Code: Select all
void PlayAnimation(char Name[256], int animIndex, int fps)
{
    Actor *check=getclone(Name); //Get actor by it's name
    int i,anim1,anim2,currentframe; //Define temporary variables
    float PlayEachFrame,tmp_framerate; //Define temporary variables
    int FrameList[FrameRate]; //Use to store the frame playing list
 
    fps=max(1,min(fps,FrameRate)); //Limit the "fps" to avoid illegal operation
 
    tmp_framerate=FrameRate; //Copy the global "FrameRate" value to a decimal form
    PlayEachFrame=max(1,tmp_framerate/fps); //Get the frame count to play animation
 
    for(i=0;i<fps;i++)
    {
        FrameList[i]=round(i*PlayEachFrame); //Store the playing sequence into array
    }
    for(i=0;i<fps;i++)
    {
        currentframe=frame%FrameRate; //Get the current frame and loop it
        if(FrameList[i]==currentframe) //Check if any of the frame list value matches "currentframe"
        {
            anim2=anim1%4; //Loop the "anim1" value from 0 to 3 forever and store it to "anim2"
            check->animpos=AnimationList[animIndex][anim2]; //Set the actor animation
            anim1++; //Add the "anim1" value to play the next animation sequence
        }
    }
}



Now, let's code the Player actor.

Player -> Create Actor -> Script Editor:
Code: Select all
//Prevent the animation from auto playing
ChangeAnimationDirection("Event Actor", STOPPED);


Player -> Draw Actor -> Script Editor:
Code: Select all
AnimationIndex=direct+walk*2; //Get the animation index
AnimationFps=AnimationList[AnimationIndex][4]; //Get the animation fps
//"direct+walk*2" is the animation index formula (check the AnimationList[5][4])
//if "direct" is 0 and "walk" is 0, you will get 0 as index, which is "Stop Left"
//if "direct" is 1 and "walk" is 0, you will get 1 as index, which is "Stop Right"
//if "direct" is 0 and "walk" is 1, you will get 2 as index, which is "Run Left"
//if "direct" is 1 and "walk" is 1, you will get 3 as index, which is "Run Right"

//PlayAnimation("actor_name", animation_index, animation_fps);
PlayAnimation("player", AnimationIndex, AnimationFps); //Play the animation


That's the main code you need.
The following code are just custom configuration.

Player -> Keydown "Right" -> Script Editor:
Code: Select all
walk=1; //walk
direct=1; //face right


Player -> Keydown "Left" -> Script Editor:
Code: Select all
walk=1; //walk
direct=0; //face left


Player -> Keyup "Any key" -> Script Editor:
Code: Select all
walk=0; //stop


That's all.

The main idea about the custom fps is we play the animation by a certain duration.
Say the game has 30 of frame rate, and we give the animation 15 fps, that means we have to play / change the animation once for each 2 frames.
Say the game has 30 of frame rate, and we give the animation 10 fps, that means we have to play / change the animation once for each 3 frames.
Etc etc...

If we have to play / change the animation once for each 2 frames, that means we have to change the animation at the following frame:
0 - 2 - 4 - 6 - 8 - 10 - etc
We store this frame sequence to an array.

While playing the animation, we check the current frame to the array.
If any of the value matches, play the next animation. Otherwise, don't play it.
And this process is looping over time.

To see the final result, here's the demo :)

P.S.: Advantage of using this method is that you don't need to load a long list of animations with their own fps to your actor.
You just need a single animation sheet, which you can configure the sequence as well as it's fps with pure code. So that you can change them anytime you want :D

The disadvantage probably it requires even number of animation to display properly and easy to manage. Also, it's only for looping animation.
Needs more workaround to play animation only for once. Other than that, you tell me...
Attachments
CustomAnimationFPS.zip
(8.88 KiB) Downloaded 490 times
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: [Tutorial][Demo] Custom Animation Sequence and Fps

Postby digiot » Wed Jul 16, 2014 7:48 pm

First, thanks for your work.

I'm new in GEing, so I might miss common practices.

I don't know, how stable the fps, selected in the game properties, is at all.
But how can the "real_fps" be unstable, it just tells the current real fps,
or doesn't it detect the correct fps ?

Having possibly changing fps rates in mind, I would use timers for the animations.

What are the pros and cons of the both approaches ?


Cheers !
User avatar
digiot
 
Posts: 211
Joined: Sat Mar 17, 2012 2:29 pm
Score: 7 Give a positive score

Re: [Tutorial][Demo] Custom Animation Sequence and Fps

Postby Bee-Ant » Wed Jul 16, 2014 8:16 pm

At the beginning you run the game, the real_fps will leap to 100 ~ 125. I tested this by using a text actor.
And then after sometime it will go down to the configured number. Though it's not unstable. It's constantly changing from 29 to 30 back to 29 etc.
That's in my case.

And for the animation approach, using Timer will make you miss (skip) some animation when the game lags. Because it uses real time event.
Both method is OK. It's all up to your interest :D
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: [Tutorial][Demo] Custom Animation Sequence and Fps

Postby digiot » Thu Jul 17, 2014 12:00 pm

Thanks for the quick response.
I'm really curious, which solution I will take, when it comes to animating.

Since this thread is about smooth animations, I hope, this here is not too ot :

Recently, I noticed weird background animations and actors, being displayed like
oldtime interlaced sprites, in some new GE games.
The best example is knucklecrunchgames Vicious Vehicles, there you have both.
What does cause such behaviour, is it a kind of clumsy code, or can't GE do
any better ?
You are such an experienced longtime user of GE, maybe you can clear this up ?


Cheers !
User avatar
digiot
 
Posts: 211
Joined: Sat Mar 17, 2012 2:29 pm
Score: 7 Give a positive score

Re: [Tutorial][Demo] Custom Animation Sequence and Fps

Postby Bee-Ant » Thu Jul 17, 2014 4:04 pm

digiot wrote:Recently, I noticed weird background animations and actors, being displayed like oldtime interlaced sprites, in some new GE games.
The best example is knucklecrunchgames Vicious Vehicles, there you have both.
What does cause such behaviour, is it a kind of clumsy code, or can't GE do
any better ?
You are such an experienced longtime user of GE, maybe you can this up ?

Can provide a screenshot for that? I have no idea on what you're talking about :/
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: [Tutorial][Demo] Custom Animation Sequence and Fps

Postby digiot » Thu Jul 17, 2014 4:20 pm

Ahh, it's hard to describe with words for me, and screenshots don't work.
The best is, you watch the trailer here :
http://game-editor.com/forum/viewtopic.php?f=4&t=13384&sid=e99e136eaf110e27e4f50e4a37cc42ec
There you should see what i mean.

P.s. : What's the Xml trick, to hide those urls behind some text, e.g. this link ?
User avatar
digiot
 
Posts: 211
Joined: Sat Mar 17, 2012 2:29 pm
Score: 7 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest

cron