#State Machine is Not Swapping the Animations (Godot 4.2.1 Stable)

80 messages · Page 1 of 1 (latest)

obtuse compass
#

Problems I Found on the Project:
PlayerState: The game doesn't run with $Player, only with Player.new().
State: The variable state_machine isn't used.
Idle State & Walk State: The coding is the same for both Nodes.

Main Problem: Even coding the State Machine and the animations, the animations aren't playing properly. The animations should be swapping, but it isn't.

Extra Images: Used Nodes on the Character, Nodes in the Game's Room, Game's Images.

iron abyss
#

Player.new() is not the right thing to do there

obtuse compass
#

I know, I just don't know what to replace with

iron abyss
#

You don't have any node name Player, so $Player would not work.

#

you are already setting the player as the owner in the ready function

#

so no need to the variable to be @onready

#

it can just be var player:Player

obtuse compass
#

That's the thing

#

If I put like this, this happens:

iron abyss
#

maybe set process to false until player is not null

obtuse compass
#

Uhm... How would I do this?

iron abyss
#

set_physics_process(false)

#

put this as the first thing in the _ready function

#

then set it true after the player is set

obtuse compass
#

I put this in which script?

iron abyss
#

where you have your player variable

obtuse compass
#

Oh okay

obtuse compass
#

It is a condition?

iron abyss
#

no, I told you where

#

in the _ready function where you are getting the owner to set to the player variable

obtuse compass
#

Yes, I put the false statement, but what about the true one?

iron abyss
#

after setting player

#

same function

#

at the end

obtuse compass
#

Like this?

iron abyss
#

althoug you are not using the physics_process in that script, are you?

obtuse compass
#

No, i'm not

iron abyss
#

nevermind then. Just put if player == null: return at the start of the function where you are using is_on_floor

obtuse compass
#

Well, the game opened now.

#

However, the animations aren't playing.

#

The way i'm doing so far is calling the animation player specific animation after some conditions

#

I'm only using the State Machine, because I have too many animations to implement

iron abyss
#

are the code passing the conditions?

#

does your state_machine.transition_to() returns something?

obtuse compass
#

Let me check by printing

iron abyss
#

I see you are checking that in your ifs

#

why don't you call the animations in the State itself?

#

I usually change the animation on the enter function of the state

obtuse compass
#

So if I change the Physics_Update to Enter, it will work?

iron abyss
#

not what I meant

obtuse compass
#

Oh

iron abyss
#

here, you are changing state and playing animations

#

which state is this?

obtuse compass
#

It is the Idle one

iron abyss
#
class_name CharacterStateIdle
extends CharacterState

func enter(_msg = {}):
    character.velocity = Vector3.ZERO
    character.play_animation("Idle")
    if character.target == null:
        Signals.target_requested.emit(character)


func physics_update(_delta:float):
    if not character.target == null:
        var dist_to_target:float = character.position.distance_to(character.target.global_position)
        if dist_to_target >= character.attack_dist + 0.5:
            change_to(STATE_CHASE)
        else:
            if character.can_attack:
                change_to(STATE_ATTACK)

here is one example of my Idle state. Upon entering I change the animation.
And in the update I just check for conditions to change the state

#

and those states take care of updating the animations

#

btw, did you consider using an AnimationTree instead of StateMachine if it is just for the animations?

obtuse compass
#

Yes, I did

#

But it would be very simplistic for what I'm trying to do

#

Also, the Animation Tree only works with BlendSpace2D

iron abyss
#

they have a state machine as well

obtuse compass
#

And there's all of the sprites I want to implement

obtuse compass
iron abyss
#

if is just for the animations, yes. But the state machine you have implemented can help you with other stuff

obtuse compass
#

Any examples?

iron abyss
#

to break down the logic of your character

#

insted of having everything in one script

obtuse compass
#

Ah I see

#

Well, I definitely think it is useful to make the State Machine, but I do understand it can be very complex for me as a beginner

#

Because so far, all movement coding is inside of the Player Script

iron abyss
#

I have to go to bed now

#

I can help you tomorrow if you still need

obtuse compass
#

Yeah I still need

#

But I thank you enough

obtuse compass
#

Good Morning

#

I have remade some stuff from the StateMachine and Idle / Walk Animations.
This time, i'm trying to follow this tutorial:

#

StateMachine new coding

#

Idle Script

#

Walk Script

obtuse compass
#

@iron abyss Sorry for the ping, I just wanted to say that the animation only plays when I apply something to the Initial State export slot.

iron abyss
#

yeah, that is how it should be. You should have a state to start the state machine

#

otherwise the machine doesn't know what to do

obtuse compass
#

Honestly, I gave up on the State Machine

#

It never works for me and i'm like 3 days trying to make it work