Godot Onready var not working and getting error Unexpected "Identifier" in class body

huangapple go评论109阅读模式
英文:

Godot Onready var not working and getting error Unexpected "Identifier" in class body

问题

尝试使用onready变量来启动射线检测以播放NPC动画,但即使射线工作,动画也不会播放。

extends Node3D

enum {
    Attack1,
    Death1,
    Idle,
    Pose,
    Walk
}

var state = Idle

onready var raycast = $RayCast3D
onready var ap = $"maxdamage_zombie-low-poly"

func _ready():
    pass  # 用实际的函数内容替换此行。

func _process(delta):
    if raycast.is_colliding():
        state = Attack1
    else:
        state = Idle

    match state:
        Attack1:
            ap.play("Attack1")
        Death1:
            ap.play("Death1")
        Idle:
            ap.play("Idle")
        Pose:
            ap.play("Pose")
        Walk:
            ap.play("Walk")

解释/替代简单解决方案以修复代码,适用于初学者/中级水平的解释。

英文:

trying to use onready var to start a raycast to play a animation for a npc but even though the ray works it wont play the animation

code:

extends Node3D

enum
{
Attack1,
Death1,
Idle,
Pose,
Walk
}

var state = Idle

onready var raycast = $RayCast3D
onready var ap = $"maxdamage_zombie-low-poly"
func _ready():
pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):

if raycast.is_colliding():
	state = Attack1
else:
	state = Idle

match state:
	Attack1:
		ap.play("Attack1")
	Death1:
		ap.play("Death1")
	Idle:
		ap.play("Idle")
	Pose:
		ap.play("Pose")
	Walk:
		ap.play("Walk")

Explanation/Alternative simple answer on how to fix the code in a beginner/intermediate level explanationyour text

答案1

得分: 1

我通过在 onready 前面加上一个 "@ " 来解决了这个问题。

@onready var raycast = $RayCast3D 

链接

在升级到 Godot 4 后,没有 "@ " 的 "onready" 似乎对我不起作用。

英文:

I fixed this issue by putting a "@" in front of onready.

@onready var raycast = $RayCast3D

https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/gdscript_basics.html#onready-annotation

"onready" without the "@" seemed to break for me after upgrading to Godot 4.

huangapple
  • 本文由 发表于 2023年2月14日 01:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定