Godot 4 的新功能,https://youtu.be/fuGiJdMrCAk?si=RGKBqgyjoi9khdoG 教程。在 StateEnter 和 OnExit 结束时,我收到堆栈跟踪错误

New to Godot 4 and following https://youtu.be/fuGiJdMrCAk?si=RGKBqgyjoi9khdoG tutorial. At the end of StateEnter and OnExit I get a stack trace error

提问人:user22698085 提问时间:10/7/2023 更新时间:10/7/2023 访问量:8

问:

Stack Trace 调试器 说 无效的获取索引 'next_state'(on base: 'Nil')。

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)
            
            # Set the states up with what they need to function
            child.character = character
        else:
            push_warning("Child" + child.name + " is not a State for CharacterStateMachine")
            



func _physics_process(delta):
    if(current_state.next_state != null):
        switch_states(current_state.next_state)

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)

我做错了什么,我该如何解决?

我不知道该怎么做,我希望它能工作。我尝试使用聊天 gpt,它使它工作,但后来代码不同,我无法移动,所以我撤消了它。

评论


答: 暂无答案