[Dive Into AGMaker] Script Gist - Print Specific Variable And Switch

Script Effect

Retrieve specific AGMaker variables and switches by name in GDScript and print them to the console.

Key Points

  1. Create a Node type node under GameObject and attach this script to it.
  2. Specify the VariableSettings and SwitchSettings nodes in the inspector panel.
  3. Assign my_var_name and my_switch_name to the names of the target variable and switch you are interested in.
  4. In this example, the printing occurs in the _ready function, so it will only print once. If it occurs in the _process or _physics_process function, it will print once per render frame or physics frame.

Core

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)