Invalid access to property or key 'idle' on a base object of type 'int'.

Godot Version

Godot 4.5.1

Question

what’s wrong with my code? it gives me this error

If you share code, please wrap it inside three backticks or replace the code in the next block:

extends CharacterBody2D

#############################--PLAYER CONTROLS--#############################

@export_category("Movement")
@export var walk_speed: int = 300
@export var swift: int = 550
@export var gravity: float  = 700.0
@export var deceleration:= 0.2
@onready var anim = $AnimatedSprite2D
@onready var animPlayer = $AnimationPlayer
@export_category("jump_method") 
@export_range (0,1) var deceleration_jump_release         #VARIABELS#
@export var acceleration:float  = 290.0
@export var jump_force:  float  = -800.0
@export var jump_sliding:float  = 20.0
var jump_times: int = 2
@export_category("wall_jump_method")
@onready var rray: RayCast2D = $Node2D/Rray
@onready var lray: RayCast2D = $Node2D/Lray
const wall_jump_x =  1000
const wall_jump_y = -1000
var is_wall_jumping = false 
var on_ground: bool = false
var on_air: bool = false
var state = States.idle
var move = Vector2()
###############################################################################

enum States {idle,walk,jump}

func change_states(newstate):
	state = newstate
	
func _physics_process(delta):
	move_and_slide()
	
	match state:
		state.idle:
			idle()
			
		state.walk:
			walk()
			
		state.jump:
			jump()
			
	



func idle():
	move = Input.get_axis("left","right")
	
	if move:
		change_states(state.walk)
		velocity.x = move * walk_speed
		anim.play("walk")
	else :
		change_states(state.idle)
		velocity.x = move_toward(velocity.x,0, walk_speed * deceleration )
		anim.play("idle")
	
func walk():
	pass
	
func jump():
	pass

You are confusing yor variable state for your enum States. It should always be States.idle, never state.idle. Same for .walk and .jump.

Your code tries to access state as if it is an enum definition. state actually contains an enum value.

The enum is called States and you access its constants by using for example States.idle (like when you wrote var state = States.idle)

func _physics_process(delta):
	move_and_slide()
	
	match state:
		States.idle:
			idle()
			
		States.walk:
			walk()
			
		States.jump:
			jump()
1 Like

thx bro