0 votes

I was using GameEndeavors statemachine tutorial and after some hiccups I had got it to work. However my next issue was that my character was not properly transitioning from the running state to the idle state. The running animation continued to play as the character was standing still.

Here is a look at all the code that I had used.

PlayerController.gd

extends KinematicBody2D

const UP = Vector2(0, -1)
const SLOPE_STOP = 64

var velocity = Vector2()
var movespeed = 7 * 16
var gravity = 1000
var jump
velocity = -420

onready var animplayer = $AnimationPlayer
onready var state
machine = $AnimationTree.get("parameters/playback")

func applygravity(delta):
velocity.y += gravity * delta
print(velocity.x)

func applymovement():
var snap = Vector2.DOWN * 32 if !isonfloor() else Vector2.ZERO
velocity = moveandslidewithsnap(velocity, snap, Vector2.UP)

func handlemoveinput():
var move
direction = -int(Input.isactionpressed("left")) + int(Input.isactionpressed("right"))
velocity.x = lerp(velocity.x, movespeed * movedirection, airres())
if movedirection!=0:
$AnimatedSprite.scale.x = move
direction

func airres():
return 0.2 if isonfloor() else 0.1

func dead():
pass


StateMachine.gd

extends Node
class_name StateMachine

var state = null setget setstate
var previous
state = null
var states = {}

onready var parent = get_parent()

func physicsprocess(delta):
if state != null:
statelogic(delta)
var transition = gettransition(delta)
if transition != null:
set_state(transition)

func statelogic(delta):
pass

func gettransition(delta):
return null

func enterstate(newstate, oldstate):
pass

func exitstate(oldstate, newstate):
pass

func setstate(newstate):
previousstate = state
state = new
state

if previous_state != null:
    _exit_state(previous_state, new_state)
if new_state != null:
    _enter_state(new_state, previous_state)

func addstate(statename):

states[state_name] = states.size()

PlayerFSM.gd

extends StateMachine

func ready():
add
state("idle")
addstate("run")
add
state("jump")
addstate("fall")
call
deferred("set_state", states.idle)

func input(event):
if [states.idle, states.run].has(state):
if event.is
actionpressed("jump"):
parent.velocity.y = parent.jump
velocity

func statelogic(delta):
parent.handlemoveinput()
parent.
applygravity(delta)
parent.
apply_movement()

func gettransition(delta):
match state:
states.idle:
if !parent.isonfloor():
if parent.velocity.y < 0:
return states.jump
elif parent.velocity.y > 0:
return states.fall
elif parent.velocity.x !=0:
return states.run
states.run:
if !parent.isonfloor():
if parent.velocity.y < 0:
return states.jump
elif parent.velocity.y > 0:
return states.fall
elif parent.velocity.x == 0:
return states.idle
states.jump:
if parent.isonfloor():
return states.idle
elif parent.velocity.y >= 0:
return states.fall
states.fall:
if parent.isonfloor():
return states.idle
elif parent.velocity.y < 0:
return states.jump

return null

func enterstate(newstate, oldstate):
match newstate:
states.idle:
parent.anim
player.play("Idle")
states.run:
parent.animplayer.play("Run")
states.jump:
parent.anim
player.play("Jump")
states.fall:
parent.anim_player.play("Fall")

func exitstate(oldstate, newstate):
pass

Any and all help appreciated. Thanks in advance.

in Engine by (37 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.