脚本效果
在gdscript中获取特定名字的AGM变量和开关,并打印到控制台
要点
- 在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]please assign VariableSettings node![/b][/color]")
if not switch_settings:
print_rich("[color=red][b]please assign SwitchSettings node![/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("variable named '%s' does not exist!" % 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("switch named '%s' does not exist!" % my_switch_name)