Nodes are connected properly but gives same error over and over again

I


n godot 4.2 nodes are connected properly but game crashes whenever i try to move my player

Is the error in red what is causing the problem? If so, it means that Godot couldn’t find a check_can_move() function in your CharacterState class. Either that means it doesn’t exist, you maybe made a typo, or you haven’t declared the function as static, which in this case you would need to.

If the error still persists even after checking these things, please share your CharacterState code with us. :>

nope it is correct

extends CharacterBody2D

@onready var AnimSprite = $AnimatedSprite2D
@onready var Hbox_standing = $Hbox_standing
@onready var Hbox_crouching = $Hbox_crouching
@onready var dash = $Dash 
@onready var Hbox_attacking_R = $hbox/Hbox_attack_right
@onready var Hbox_attacking_L = $hbox/Hbox_attack_left
@onready var attack_timer = $Timer
@onready var combo_timer = $Timer 
@onready var HboxTimer = $hbox/HitboxTimer
@onready var AnimTree : AnimationTree = $AnimationTree
@onready var Sprite : Sprite2D = $Sprite2D
@onready var CharacterState : CharacterStateMachine


var direction : Vector2 = Vector2.ZERO


const GRAVITY = 1000
const SPEED = 300
const JUMP = -350
const AIR_SPEED = 250
const DASH_SPEED = 600


enum State { idle, run, jump, duck, slide, attack, end_dash }

var current_state = State.idle
#var crouching = false
#var sliding = false
#@export var is_attacking = false
#@export var attack_duration = 0.9
#@export var combo_window = 0.6 
#var combo_stage = 0 

func _ready():
	AnimTree.active = true
	print("Player script ready!") 

func _physics_process(delta):
	# Apply gravity 	
	direction = Input.get_vector("move_left", "move_right" , "jump" , "duck")
	if direction.x != 0 && CharacterState.check_can_move():
		velocity.x = direction.x * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

		
	facing()
	player_animation()
	move_and_slide() 



func facing():
	if direction.x > 0 :
		Sprite.flip_h = 0
	elif direction.x < 0 :
		Sprite.flip_h = 1
		
		

func player_animation(): 
	AnimTree.set("parameters/Run/blend_position", direction.x)


CharacterState is declared but not assigned. It has no value (‘Nil’), which is what’s causing the error.

I meant share the code of your CharacterState, not of the player. But I think I can already guess the problem.

In order to statically call the CharacterState class, THAT script, no the one of the player, needs to start with class_name CharacterState. Only then can you call it by name and access the function.

If that still isn’t it, please share the code of your CharacterState also. :>

mb this is full CharacterState node code which is child node of Class State

extends Node

class_name CharacterStateMachine

@export var character: CharacterBody2D
@export var current_state: State

var states: Array[State] = []

func _ready():
	for child in get_children():
		if child is State:
			states.append(child)
			child.character = character
			print("State found: " + child.name)
		else:
			push_warning("Child " + child.name + " is not a State!")

	if states.size() > 0:
		current_state = states[0]
		print("Initial current_state set to: " + current_state.name)
	else:
		push_warning("No valid states found!")

func check_can_move():
	return current_state.it_can

This one is State node script

extends Node

class_name State

@export var it_can : bool = true

var character : CharacterBody2D


func state_input(event):
	pass

Ah, yeah. It’s what @soundgnome said. You made your CharacterState variable @onready but never declared a value to it.

I was just confused because you declared the variable with a capital letter name. Vars in GDScript are usually declared in snake_case while class names are written in PascalCase. So CharacterState can mean something different than character_state for example.

Sorry for confusion, it worked ;DDD and thanks a lot both of you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.