Needing help with this

Godot Version

4.7.2

Question

Was following a tutorial about state machines by chris tutorials and fond a comment that provided a solution for the problem with switching states when going downhill, I applied it and it got me an error:

@export var raycast_2d: RayCast2D


func state_process(_delta):
	if raycast_2d.get_collider()==null:
		playback.travel(jump())
		next_state = charairstate

Since it’s an @export variable, you need to go to the inspector in Godot and actually select your RayCast2D node, otherwise it’s going to be null.

got it but it still wont work

Maybe you want to use "jump" or your variable jump_animation instead of calling the function jump()


got it, but got this error

You haven’t shown us any code using current_state, chances are you need to fill in another @export variable through the editor.


Make sure to paste code instead of screenshots

extends State



class_name groundstate

@export var jump_velocity : float = -150.0
@export var charairstate : State
@export var jump_animation : String = "jump"

@export var raycast_2d: RayCast2D


func state_process(_delta):
	if raycast_2d.get_collider()==null:
		playback.travel(jump_animation)
		next_state = charairstate
		
#this is how you write when programming a state
func state_input(event : InputEvent):
	if(event.is_action_pressed("ui_accept")):
		jump()
		
func jump():
	character.velocity.y = jump_velocity
	next_state = charairstate
	playback.travel("jump")

extends Node


# Called when the node enters the scene tree for the first time.
class_name charstatemachine



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

var states : Array[State]


func _ready():
	for child in get_children():
		if(child is State):
			states.append(child)
			
			#get states that are need to function
			
			child.character = character
			child.playback = animation_tree["parameters/playback"]
			
		else:
			push_warning("Child" + child.name + "is not a state for charstatemachine")
			
func _physics_process(_delta):
	if(current_state.next_state != null):
		switch_states(current_state.next_state)
		
		
	current_state.state_process(_delta)

func check_if_can_move():
	return current_state.can_move

func switch_states(new_state : State):
	if (current_state != null):
		current_state.on_exit()
		current_state.next_state = null
	
	current_state = new_state
	
	current_state.on_enter()


func _input(event: InputEvent):
	current_state.state_input(event)

heres my code

extends Label


@export var state_machine : charstatemachine

func _process(_delta):
	text = "state:" + state_machine.current_state.name

have to repeat this, got a problem

Your state machine is null, you didn’t set it in the inspector. Please show your inspector with that node selected.