[深入 AGMaker] 脚本摘要 - 打印变量和开关

脚本效果

创建一个 Label 节点,并在每一帧将某个 GameObject 的所有变量打印到该 Label 节点上。
注意:此外,演示如何打印游戏对象的速度 var v = prototype_player.velocity

关键点

  1. 此脚本应附加到一个独立的 Control 节点上。
  2. prototype_player 的路径需要根据您的游戏对象的实际节点进行修改。
  3. variable_settings 需要指定为您实际的变量设置节点的名称(有些人可能已经提前修改过此名称)。

核心代码

extends Control

@onready var prototype_player: GameObject = $"../.."
@onready var variable_settings: VariableSettings = $"../../VariableSettings"
@export var label: Label

func _ready() -> void:
	if not label:
		print_rich("[color=red][b]请分配 label![/b][/color]")

func _process(_delta: float) -> void:
	if not label:
		return
	
	var v = prototype_player.velocity
	var new_text = ""
	new_text += "velocity : %s" % [v]
	new_text += "\n"
	
	var all_variables = variable_settings.get_variables()
	for i:VariableData in all_variables:
		new_text += "%s: %s \n" % [str(i._variable_name),str(i.get_value())]
		pass
	
	label.text = new_text