How to set the spawn_math of Multiplayer Spawner when it is used as a global script?

Godot Version

4.5.stable

Question

I have a NetworkManager global script for managing network links. I would like to set up a Spawner node for generating player information and directly use it as a child node of the Spawner.
I tried to generate it using code, but I encountered an issue when setting the spawn_math: I cannot set it through NodePath (“PlayerConfigCollection”), it will prompt Cannot find the spawnnode and must use an absolute path (as shown below). Why is this? Is it appropriate to use an absolute path directly?

func _ready() -> void:
	generate_player_config_spawner()


# 生成玩家配置集合节点
func generate_player_config_spawner()->void:
	var player_config_spawner: MultiplayerSpawner = MultiplayerSpawner.new()
	player_config_collection = player_config_spawner
	self.add_child(player_config_spawner)

	player_config_spawner.name = "PlayerConfigCollection"
	player_config_spawner.spawn_function = _add_player_config_function
	player_config_spawner.spawn_path = "/root/NetworkManager/PlayerConfigCollection"
	player_config_spawner.spawn_limit = 0
	player_config_spawner.add_spawnable_scene(player_config_path)

The MultiplayerSpawner.spawn_path is the root node where the spawner will spawn the nodes. If you want to point it to itself then you’ll need to use NodePath(".")

If you want to point to another node then you can use Node.get_path() to get the absolute path of that node or Node.get_path_to() to get the relative path.

2 Likes