#Multiple Spawn for Client

10 messages · Page 1 of 1 (latest)

gloomy wolf
#

Hey everyone, I am trying to make a multiplayer game in 4.2.2 version. But my client spawns twice. Can you help me ?

This is my output when host selects team;

Spawning player with name:1

This is my output when client pressed join button ;

connected: 1531137407 ```

This is my output when client selects team ;

``` Spawning player with name:1531137407
Spawning player with name:1531137407 ```

This is my script ;

extends Node3D

@onready var main_menu: PanelContainer = $CanvasLayer/MainMenu
@onready var team_select_menu: PanelContainer = $CanvasLayer/TeamSelectMenu

@onready var address_entry: LineEdit = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/AddressEntry
@onready var spawn_1: Marker3D = $Spawn_1
@onready var spawn_2: Marker3D = $Spawn_2

const Player = preload("res://player.tscn")
const PORT = 9999

var enet_peer = ENetMultiplayerPeer.new()
var team_number
var mp_mode

func _ready():
$MultiplayerSpawner.set_spawn_function(my_spawn)

func _unhandled_input(event: InputEvent) -> void:
if Input.is_action_just_pressed("Quit"):
get_tree().quit()

func _on_host_button_pressed() -> void:
main_menu.hide()

enet_peer.create_server(PORT)
multiplayer.multiplayer_peer = enet_peer
multiplayer.peer_connected.connect( func( id ) : print( "connected: ",  id ) )
multiplayer.peer_disconnected.connect(remove_player)

team_select_menu.show()

func _on_join_button_pressed() -> void:
main_menu.hide()
# connect client here
enet_peer.create_client("localhost", PORT)
multiplayer.multiplayer_peer = enet_peer

team_select_menu.show()    

func _on_ct_button_pressed() -> void:
team_select_menu.hide()
team_number = 2
# host can rpc it itself
add_player_to_team.rpc_id(1, team_number)

func _on_t_button_pressed() -> void:
team_select_menu.hide()
team_number = 1
# host can rpc it itself
add_player_to_team.rpc_id(1, team_number)

new

@rpc("any_peer", "call_local", "reliable")
func add_player_to_team( team : int ) -> void:
team_number = team
var peer_id = multiplayer.get_remote_sender_id()
if peer_id == 1 and multiplayer.is_server():
add_player(peer_id)
elif peer_id != 1 and multiplayer.is_server():
add_player(peer_id)
elif multiplayer.get_unique_id() != 1:
rpc_id(1, "add_player_to_team", team)

func add_player(peer_id):
if get_node_or_null(str(peer_id)) != null: return

var spawn_pos = spawn_1.global_position
if team_number == 2:
    spawn_pos = spawn_2.global_position

$MultiplayerSpawner.spawn( {"peer_id":peer_id,  "spawn_pos":spawn_pos } )

func remove_player(peer_id):
var player = get_node_or_null(str(peer_id))
if player:
player.queue_free()

func my_spawn(data:Dictionary) -> Node:
var player = Player.instantiate()
player.name = str(data.peer_id)
print("Spawning player with name:", player.name)

call_deferred("set_player_position", player, data.spawn_pos)

return player

func set_player_position(player: Node3D, spawn_pos: Vector3) -> void:
if player.is_inside_tree():
player.global_position = spawn_pos

merry wyvern
#

could be because of this part in add_player_to_team()

elif multiplayer.get_unique_id() != 1:
        rpc_id(1, "add_player_to_team", team)

it should't really be necessary since you always call it specifically for the server, but maybe it causes it to be called twice because you set it to "call_local".

gloomy wolf
# merry wyvern could be because of this part in `add_player_to_team()` ``` elif multiplayer.get...

thank you for your answer. I commented that section and nothing changed. Also i remove "call_local" from it, host can't spawn but client still spawn twice. Host got this error ;

E 0:00:02:0135 main_level.gd:50 @ _on_ct_button_pressed(): RPC 'add_player_to_team' on yourself is not allowed by selected mode. <C++ Error> Condition "p_peer_id == caller_id && !config.call_local" is true. Returning: ERR_INVALID_PARAMETER <C++ Source> modules/multiplayer/scene_rpc_interface.cpp:478 @ rpcp() <Stack Trace> main_level.gd:50 @ _on_ct_button_pressed()

merry wyvern
#

oh right i forgot that you also call it on the server.

#

oh wait, was the only way you could tell that the client spawned twice because of the print output?

#

that's just because it spawned both on the server and the client. Your server character also "spawns twice", just not at the same time.

gloomy wolf
merry wyvern
#

well it then sounds like it's more of a problem with that system. since that's the minimum amount of spawns you can have in multiplayer.

merry wyvern
#

you could just create the server without selecting a team, then let the client join and only afterwards select the team for both players. they should both behave the exact same with the print messages.