脚本效果
通过名称在 GDScript 中检索特定的 AGMaker 变量和开关,并将其打印到控制台。
关键点
- 在 GameObject 下创建一个 Node 类型的节点,并将此脚本附加到该节点上。
- 在检查器面板中指定 VariableSettings 和 SwitchSettings 节点。
- 将
my_var_name和my_switch_name分配给您感兴趣的目标变量和开关的名称。 - 在此示例中,打印操作发生在
_ready函数中,因此只会打印一次。如果发生在_process或_physics_process函数中,则会在每个渲染帧或物理帧打印一次。
核心代码
extends Node
@export var variable_settings: VariableSettings
@export var switch_settings: SwitchSettings
func _ready() -> void:
if not variable_settings:
print_rich("[color=red][b]请分配 VariableSettings 节点![/b][/color]")
if not switch_settings:
print_rich("[color=red][b]请分配 SwitchSettings 节点![/b][/color]")
var my_var_name = "max_hp"
if variable_settings.has_value(my_var_name):
print(variable_settings.get_value(my_var_name))
else:
printerr("名为 '%s' 的变量不存在!" % my_var_name)
var my_switch_name = "my_switch"
if switch_settings.has_value(my_switch_name):
print(switch_settings.get_value(my_switch_name))
else:
printerr("名为 '%s' 的开关不存在!" % my_switch_name)