Godot Version
v 4.3
Ok so I am making a 2d platformer fighting game and currently am trying to set up a state machine but whenever I run my code it keeps telling me “Invalid access to property or key ‘id’ on a base object of type ‘CharacterBody2D (FOX.gd)’.”
the: @onready var id = get_parent().id
is where all my problems are coming from
this is my code:
extends StateMachine
@onready var id = get_parent().id
func _ready():
add_state(‘STAND’)
add_state(‘JUMP_SQUAT’)
add_state(‘SHORT_HOP’)
add_state(‘FULL_HOP’)
add_state(‘DASH’)
add_state(‘MOONWALK’)
add_state(‘WALK’)
add_state(‘CROUCH’)
call_deferred(“set_state”, states.STAND)
func state_logic(delta):
parent.updateframes(delta)
parent._physics_process(delta)
func get_transition(delta):
parent.move_and_slide_with_snap(parent.velocity2,Vector2.ZERO,Vector2.UP)
parent.states.text = str(state)
match state:
states.STAND:
if Input.get_action_strength(“right_%s” % id) == 1:
parent.velocity.x = parent.RUNSPEED
parent.frame()
parent.turn(false)
return states.DASH
if Input.get_action_strength(“left_%s” % id) == 1:
parent.velocity.x = -parent.RUNSPEED
parent.frame()
parent.turn(true)
return states.DASH
if parent.velocity.x > 0 and state == states.STAND:
parent.velocity.x += -parent.TRACTION1
parent.velocity.x = clamp(parent.velocity.x,0,parent.velocity.x)
elif parent.velocity.x < 0 and state == states.STAND:
parent.velocity.x += parent.TRACTION*1
parent.velocity.x = clamp(parent.velocity.x,parent.velocity.x,0)
states.JUMP_SQUAT:
pass
states.SHORT_HOP:
pass
states.FULL_HOP:
pass
states.DASH:
if Input.is_action_pressed(“left_%s” % id):
if parent.velocity.x > 0:
parent.frame()
parent.velocity.x = -parent.DASHSPEED
elif Input.is_action_pressed(“right_%s” % id):
if parent.velocity.x < 0:
parent.frame()
parent.velocity.x =parent.DASHSPEED
else:
if parent.frame >= parent.dash_duration-1:
return states.STAND
states.MOONWALK:
pass
states.WALK:
pass
states.CROUCH:
pass
func enter_state(new_state, old_state):
pass
func exit_state(old_state, new_state):
pass
func state_includes(state_array):
for each_state in state_array:
if state == each_state:
return true
return false