Skip to main content

Spawning things

Player

basics

in godot, we usually need to define what things are synchronized between server and client. for spawning, this is done with MultiplayerSpawner nodes.

In our main scene, add a node3d called "players". this is so we keep things organized - every player we spawn will be a child of that node. also add a MultiplayerSpawner (preferably not under players, as it is not a player). In the node properties of the MultiplayerSpawner, we need to set 2 things: where the spawned nodes shall appear -> Spawn Path, set this to the players node we just created, and what scenes it's allowed to spawn. this spawner is for players, so in it's Auto Spawn List, select the player scene we've created in initial setup. this tells the spawner exactly what scene to put where.

However, it doesn't actually contain any spawning logic - all it does is make sure that if the multiplayer authority (todo: link to explain authority) spawns the correct scene at the correct location, this same scene is replicated to any clients.

NOTE: for the spawner to work, you need to use add_child(instance, true) to give the node a proper name, otherwise the spawner will refuse to work.

to see it working, add the following to the _on_player_connected(id):

func _on_player_connected(id):
	print("player connected. id: " + str(id))
	var player_scene = preload("res://player.tscn")
	var player = player_scene.instantiate()
	$players.add_child(player, true)

however, the players now spawn inside each other and physics probably freaks out. let's add:

Spawn Points

we can add a node "SpawnPoints" to our main scene, and inside it add points called 1, 2, 3 and so on. move them around a bit. then, we'll need to assign players a number - by default, their ID looks like this, a random number: player connected. id: 834055311
player connected. id: 271090083

for now, we just put a random selection in. (todo - fix spawn point and player ids)

however, even spawning in different places, physics freaks out - turns out, we did not spawn players correctly yet - we need to know authority, to know which client controls which player scene.

Authority