finite state machine

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pretzldonut

I am trying to build simple state machine which switches between walking and idling. However when running the script I cannot move my character and can’t quit by pressing ESC. Joystick is a 2D vector for the direction of the arrow keys defined by get_input (). any help on why this may not be working correctly will be aprreciated.

extends KinematicBody2D

func _input(event): #quits on ESC
	if event.is_action_pressed("ui_cancel"):
		get_tree().quit()

#State Vars
var states = ["idle", "walk"]

var current_state = "idle"
var previous_state = null

const SPEED = 125
#const GRAVITY= 9
#const JUMP_FORCE = -400
var velocity = Vector2(0,0) 
var joystick = Vector2(0,0) 
const FLOOR = Vector2(0, -1)


func  _process(delta):
	joystick=get_node("functions").get_input() #gets joysrick input
	call(current_state + "_logic", delta)

func set_state(newState : String):
	#change values
	previous_state = current_state
	current_state = newState

func idle_logic(_delta):
	if joystick.x != 0:
		#set state to walk if you press left or right
		set_state("walk")
	else:
		velocity.x=0

func move_logic():
	if joystick.x == 0:
				#if no direction pressed set to idle
		set_state("idle")
	else:
		velocity.x=joystick.x*SPEED	



func _physics_process(delta):
	move_and_slide(velocity,FLOOR)
:bust_in_silhouette: Reply From: njamster

From the code you provided it’s not clear what the “functions”-node is and what exactly its get_input-method returns. Consequentially I’m not able to run your code myself, but I can tell you nonetheless that you need to rename move_logic into walk_logic and add an an delta-argument for it. The escape key is working fine for me (I only removed the first line of _process for the reasons stated above).