【Deep Dive into AGMaker】Script Snippets - Print Variables and Switches with Specific Names

Script Effect

Retrieve AGM variables and switches by specific 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. In the Inspector panel, assign the VariableSettings and SwitchSettings nodes respectively.
  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 print action 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)
3 Likes

Awesome! :+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1: