[Resuelto]
Buenas, llevo apenas unos meses con Godot y aprendiendo de cero programación, me encontré con este problema intentado hacer una maquina de estados, seguí paso a paso los tutoriales:
pero me sigue tirando error, mas específicamente el "Invalid get index 'idle' (on base: 'Dictionary'), les dejo el código de la maquina de estados base y la que se encuentra como un nodo en el jugador por si alguno sabe bien, de antemano gracias.
pd: Ni idea como funciona bien el editor del foro jaja, por eso quedo asi
var state = null
var previous_state = null
var states = {} #Diccionario de estados
onready var parent = get_parent()
func _physics_process(delta):
if state != null:
_state_logic(delta)
var transition = _get_transition(delta)
if transition != null:
set_state(transition)
func _state_logic(delta):
pass
func _get_transition(delta):
return null
func _enter_state(new_state , old_state):
pass
func _exit_state(old_state , new_state):
pass
func set_state(new_state):
previous_state = 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 add_state(state_name):
pass
`
`
extends state_machine
func _ready():
add_state("idle")
add_state("run")
add_state("jump")
add_state("fall")
call_deferred("set_state" , states.idle)
func _input(event):
if [states.idle , states.run].has(state):
if event.is_action_pressed("saltar"):
parent.velocity.y = parent.jump_velocity
func _state_logic(delta):
parent._handle_move_input()
parent._apply_movement()
parent._apply_gravity(delta)
func _get_transition(delta):
match state:
states.idle:
if !parent.is_on_floor():
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.is_on_floor():
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.run:
if parent.is_on_floor():
return states.idle
elif parent.velocity.y >= 0:
return states.fall
states.fall:
if parent.is_on_floor():
return states.idle
elif parent.velocity.y < 0:
return states.jump
return null
func _enter_state(new_state , old_state):
match new_state:
states.idle:
parent.anim_player.play("idle")
states.run:
parent.anim_player.play("run")
states.jump:
parent.anim_player.play("jump")
states.fall:
parent.anim_player.play("fall")
func _exit_state(old_state , new_state):
pass