qbcore-framework / qb-core

FiveM RP Framework Core :muscle:
GNU General Public License v3.0
590 stars 960 forks source link

Update functions.lua - PlayAnim #1082

Closed mjvanhaastert closed 9 months ago

mjvanhaastert commented 9 months ago

The runningAnim function is designed to play a specific animation for a player's ped (character) in FiveM. Here's a detailed breakdown of what the function does:

The function takes four parameters: animDict (the name of the animation dictionary), animName (the name of the animation within the dictionary), duration (the duration of the animation in milliseconds), and upperbodyOnly (a boolean indicating whether the animation should only affect the upper body of the ped).

It starts by creating a new promise, animPromise, which will be used to handle the asynchronous nature of loading and playing the animation.

The function then checks if animDict and animName are strings. If not, it rejects the promise with an error message and returns.

It checks if the animation dictionary exists. If not, it rejects the promise with an error message and returns.

It sets the animation flags based on the upperbodyOnly parameter. If upperbodyOnly is true, the flags are set to 16, otherwise, they are set to 0. If the duration is -1 (indicating the animation should loop indefinitely), the flags are set to 49.

It gets the player's ped and the current game time.

It enters a loop where it requests the animation dictionary and waits until it's loaded. If the dictionary isn't loaded within 5 seconds, it rejects the promise with an error message and returns.

Once the animation dictionary is loaded, it plays the animation on the player's ped using TaskPlayAnim.

It waits for a short time to allow the animation to start, then gets the duration of the animation using GetAnimDuration.

If the animation duration is 0 (indicating the animation doesn't exist), it rejects the promise with an error message and returns.

It calculates the full duration of the animation in milliseconds and sets the wait time to the minimum of the provided duration and the full duration.

It waits for the duration of the animation, then removes the animation dictionary.

Finally, it resolves the promise with the current time, indicating when the animation concluded.