【深入AGMaker】脚本片段-打印特定名字的变量和开关

脚本效果

在gdscript中获取特定名字的AGM变量和开关,并打印到控制台

要点

  1. 在GameObject下创建一个Node类型的节点,将此脚本依附其上
  2. 在检查器面板上分别指定VariableSettings和SwitchSettings两个节点
  3. my_var_namemy_switch_name赋值为你感兴趣的目标变量和开关的名字
  4. 本例中打印行为发生于_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)
「いいね!」 3

牛逼! :+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1: