Friday, June 24, 2016

[TUT] GTA V - Playing and handling animations

Playing Ped animations

To play Ped animations in V we seems to only have one option (at least my tests with other methods didn't worked :) ), we need to task the Ped to play the animation.

This is the method: TASK_PLAY_ANIM

Native.Function.Call(Native.Hash.TASK_PLAY_ANIM, thePed, sDict, sAnim, speed, speed * -1, -1, flags, 0, False, bDisableLegIK, False)

The params description (seems to be):

Native.Function.Call(Native.Hash.TASK_PLAY_ANIM
thePed, - The Ped that will play the animation
sDict,  - The dictionary where the anim is located
sAnim,  - The anim name
speed,  - The play start speed (This is important to make smooth changes between anims)
speed * -1, - I don't know what exactly is, but in my tests work fine this way
-1,  - Unknown
flags, - Flags that you can set for the playback, some of them are described some lines below
0,  - Unknown
False,  - Unknown
bDisableLegIK,  - If the anim will ignore the leg/foot interaction with obstacles
False) - Unknown

Flags that i found based on my tests:

        normal = 0
        repeat = 1
        stop_last_frame = 2
        unk1 = 4
        unk2_air = 8
        upperbody = 16
        enablePlCtrl = 32
        unk3 = 64
        cancelable = 128
        unk4_creature = 256
        unk5_freezePos = 512
        unk6_rot90 = 1024

You can sum them to make combination of flags.

In the Speed param of initial playback i commonly use 8.0, works fine in great part of situations.

You will find a full (seems to be) list of the dictionary and anim names here:

Important: You need to request the animation Dictionary before start using it in your script:

Native.Function.Call(Native.Hash.REQUEST_ANIM_DICT, sDict)
sDict - The anim dictionary

To check if the Dictionary was loaded you can use:

Native.Function.Call(Of Boolean)(Native.Hash.HAS_ANIM_DICT_LOADED, sDict)
sDict - The anim dictionary


Handling the animations

To handle the anims we have some interesting methods like:

-STOP_ANIM_TASK
-IS_ENTITY_PLAYING_ANIM
-GET_ENTITY_ANIM_CURRENT_TIME
-SET_ENTITY_ANIM_CURRENT_TIME
-SET_ENTITY_ANIM_SPEED

Native.Function.Call(Native.Hash.STOP_ANIM_TASK
thePed,  - Ped playing the anim
sDict,  - Anim dictionary
sAnim,  - Anim name
speed) - Stop speed, used for smoothness control

Native.Function.Call(Of Boolean)(Native.Hash.IS_ENTITY_PLAYING_ANIM
theEntity,  - Entity/Ped playing the anim
sDict,  - Anim dictionary
sAnim,  - Anim name
3) - Unknown

Native.Function.Call(Of Double)(Native.Hash.GET_ENTITY_ANIM_CURRENT_TIME
theEntity, - Entity/Ped playing the anim
sDict, - Anim dictionary
sAnim) - Anim name

Obs.: The time returned is a number between 0.0 and 1.0 (ex.: 0.35), it represent how much percent of total playback was played, 0.35 for example means 35% ;)

Native.Function.Call(Native.Hash.SET_ENTITY_ANIM_CURRENT_TIME
theEntity, - Entity/Ped playing the anim
sDict, - Anim dictionary
sAnim - Anim name
time) - New time

Obs.: The time param here use same idea as the return of GET_ENTITY_ANIM_CURRENT_TIME method, so, to set anim to half playback time you should use 0.5 for example.

Native.Function.Call(Native.Hash.SET_ENTITY_ANIM_SPEED
theEntity, - Entity/Ped playing the anim
sDict, - Anim dictionary
sAnim - Anim name
speedCoef) - New speed coefficient, the default is 1.0


Observations

-The knowledge show in this post is result of my tests, nothing official, some details/params may be wrong, some important info/methods may be missing in this post :)

-The fact we need to use a Task to play ped anims make somethings more difficult, sometimes we need to call a CLEAR_PED_TASKS_IMMEDIATELY before try to task the Ped to play the anim.

-The method PLAY_ENTITY_ANIM didn't worked in my tests, maybe im doing something wrong :(, this is probably the best method to force a anim playback.

-It's good have a small time wait (i use 100ms) before try to retask the ped to play the anim again, if you keep retasking the ped it may not play the anim due to tasks "override", with lower FPS this issue is bigger.



You can find more animation related methods in the "Bible" of V native methods:


Example

In this example i will task the ped to play a anim, get his playback time, reset his playback time and reduce the play speed:


Obs.: The example only contains part of a script, but you already know that, right? ^^
//propaganda YT float style='display:none;'