godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.33k stars 21.24k forks source link

Program Crashes After Using @tool #98698

Open ALNY-AC opened 3 weeks ago

ALNY-AC commented 3 weeks ago

Tested versions

v4.3.stable.official [77dcf97d8]

System information

Godot v4.3.stable - Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4060 Laptop GPU (NVIDIA; 32.0.15.6094) - AMD Ryzen 9 7945HX with Radeon Graphics (32 Threads)

Issue description

image

@tool
class_name CharacterModel
extends Node2D
## 角色模型类
## 实现模型相关功能,包括动画播放等

const DEFAULT_TEXTURE = preload("res://assets/textures/characters/Main.png")

@onready var animation_loop: AnimationPlayer = $AnimationLoop
@onready var animation_region: AnimationPlayer = $AnimationRegion
@onready var hand_spot: Marker2D = $HandSpot

## 模型状态枚举
enum State{
    ## 默认状态
    IDLE,
    ## 移动状态
    RUNING,
}

## 模型方向枚举
enum Direction{
    UP,
    RIGHT,
    DOWN,
    LEFT,
}

## 当前模型的朝向
@export var direction :Direction = Direction.DOWN:
    set(v):
        direction=v
        update_animation()

## 当前模型的状态
@export var state :State = State.IDLE:
    set(v):
        state=v
        update_animation()

## 当前模型所使用的图像
@export var texture :Texture = DEFAULT_TEXTURE

## 当前模型是否抬手
@export var is_holding :bool = false:
    set(v):
        is_holding=v
        update_animation()

# 获取文字的方向
var dir_text:
    get:
        var map=['up','right','down','left']
        return map[direction]

func _ready() -> void:
    update_animation()

# 更新模型动画
func update_animation():
    if not is_node_ready():
            await ready
    animation_loop.play('default')

    if state == State.IDLE:
        animation_region.play('idle_'+dir_text)
        if is_holding:
            animation_region.play('holding_'+dir_text)
            animation_loop.stop()

    if state == State.RUNING:
        animation_region.play('move_'+dir_text)
        if is_holding:
            animation_region.play('holding_'+dir_text)

## 获取当前模型手的位置,注意,仅当is_holding为true时,握持物品才显得正常
func get_hand_spot()->Vector2:
    return hand_spot.global_position

Steps to reproduce

image

1、在其他场景中实例化了此场景。 2、当我关闭当前场景,或者点击选项卡切换到其他场景时,并未发生场景切换,而程序直接就崩溃了,也没有报错提示。

Here’s the translation:

  1. This scene was instantiated in other scenarios.
  2. When I close the current scene or click the tab to switch to another scene, the scene doesn’t switch; instead, the program crashes directly without any error message.

Minimal reproduction project (MRP)

N/A

Zireael07 commented 3 weeks ago

Please use English on this bug tracker (even if you need to use an automatic translator for that)

ALNY-AC commented 3 weeks ago

I suspect it may be related to the AnimationPlayer, but I lack the ability to investigate further.

jinyangcruise commented 3 weeks ago

Here is a workaround: adding this await get_tree().process_frame before your animation_loop.play('default')