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

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

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

问题

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

  1. extends Node3D
  2. enum {
  3. Attack1,
  4. Death1,
  5. Idle,
  6. Pose,
  7. Walk
  8. }
  9. var state = Idle
  10. onready var raycast = $RayCast3D
  11. onready var ap = $"maxdamage_zombie-low-poly"
  12. func _ready():
  13. pass # 用实际的函数内容替换此行。
  14. func _process(delta):
  15. if raycast.is_colliding():
  16. state = Attack1
  17. else:
  18. state = Idle
  19. match state:
  20. Attack1:
  21. ap.play("Attack1")
  22. Death1:
  23. ap.play("Death1")
  24. Idle:
  25. ap.play("Idle")
  26. Pose:
  27. ap.play("Pose")
  28. Walk:
  29. 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

  1. code:
  2. extends Node3D
  3. enum
  4. {
  5. Attack1,
  6. Death1,
  7. Idle,
  8. Pose,
  9. Walk
  10. }
  11. var state = Idle
  12. onready var raycast = $RayCast3D
  13. onready var ap = $"maxdamage_zombie-low-poly"
  14. func _ready():
  15. pass # Replace with function body.
  16. # Called every frame. 'delta' is the elapsed time since the previous frame.
  17. func _process(delta):
  18. if raycast.is_colliding():
  19. state = Attack1
  20. else:
  21. state = Idle
  22. match state:
  23. Attack1:
  24. ap.play("Attack1")
  25. Death1:
  26. ap.play("Death1")
  27. Idle:
  28. ap.play("Idle")
  29. Pose:
  30. ap.play("Pose")
  31. Walk:
  32. ap.play("Walk")

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

答案1

得分: 1

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

  1. @onready var raycast = $RayCast3D

链接

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

英文:

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

  1. @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:

确定