How to solve the problem with spawning peer positions?

I’ve bought a Godot Multiplayer course (in early access) and can’t fix issue with client spawn. It’s spawning wrong, I know that problem with authority in player’s script, it’s not giving permission to change his position, but how to fix it?)


I have Node2D called SpawnPoint, which has 2 child-nodes, instead of spawning on second spot, client is spawning on (0,0) position

extends CharacterBody2D  # player.gd

var owner_id = 1


func _enter_tree() -> void:
	owner_id = name.to_int()
	set_multiplayer_authority(owner_id)
	if owner_id != multiplayer.get_unique_id():
		return

	set_up_camera()
extends Node2D  # level.gd

@export var player_scene: PackedScene

@onready var players_container: Node2D = %PlayersContainer
@onready var spawn_points: Node2D = %SpawnPoints

var next_spawn_point_index = 0


func _ready() -> void:
	if !multiplayer.is_server():
		return

	Lobby.player_disconnected.connect(delete_player)

	for id in multiplayer.get_peers():
		add_player(id)

	add_player(1)


func add_player(peer_id):
	var player_instance = player_scene.instantiate()
	player_instance.position = get_spawn_point()
	player_instance.name = str(peer_id)
	players_container.add_child(player_instance)


func get_spawn_point() -> Vector2:
	var spawn_point = spawn_points.get_child(next_spawn_point_index).position

	next_spawn_point_index += 1
	if next_spawn_point_index >= spawn_points.get_child_count():
		next_spawn_point_index = 0
	
	return spawn_point

Players synchronized, all is working OK, issue is only with spawning, thanks!)

1 Like

Hi AntiVirusJ,

Apologies as our autotagging system seems to not be working entirely correctly for this.
Can you let me know which lecture you are on as the system has not added it.

The code looks okay at a glance but the issue might be in the get_spawn_point function

Let me know on this and i can take a closer look.
Thanks

Hi Marc!

Godot 4 Multiplayer: Make Your Own Online Game - EARLY ACCESS

Multiplayer Connection → Spawning Players (on 12:37 Nathan started to talk about it)

His code has a bit difference from mine, but I really don’t like his technique with adding @export to spawn_point nodes instead of @onready, I guess it’s much cleaner. But if problem with it - I will not understand why it works))

I noticed the same issue and posted a fix in a different thread, take a look here MultiplayerSpawner Bug

3 Likes

Thanks! It works)

extends Node2D  # level.gd

@onready var players_spawner: MultiplayerSpawner = %PlayersSpawner


func _ready() -> void:
	players_spawner.spawn_function = spawn_player


func add_player(peer_id):
	players_spawner.spawn(peer_id)


func spawn_player(peer_id):
	var player_instance = player_scene.instantiate()
	player_instance.position = get_spawn_point()
	player_instance.name = str(peer_id)
	return player_instance

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms