How not to render extra guns in Godot multiplayer FPS?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By heywannafunk

I’m trying to make an ordinary multiplayer FPS. I use a second Camera3D and a SubViewport to avoid player’s gun clipping into the wall:


Both cameras’ cull masks set up the way that everything works fine until the other player hasn’t connected.

As another player connects to a host, every player get an extra gun rendered on their screen:

Apparently it’s happening because every new connection instantiates a new player, hence there’s another viewport in the world which adds up everything it has to render to players’ screens. Also if I remove the whole SubViewportContainer, guns don’t render in the wrong places anymore, only in other players’ hands, but now they’re clipping into the walls.

Here’s my world script that handles connections:

extends Node

@onready var main_menu = $CanvasLayer/Menu
@onready var address_entry = $CanvasLayer/Menu/MarginContainer/VBoxContainer/AddressEntry

const Player = preload("res://scenes/Player.tscn")
const PORT = 8899
var enet_peer = ENetMultiplayerPeer.new()

func _unhandled_input(event):
	if Input.is_action_just_pressed("exit"):
		get_tree().quit()

func _on_host_pressed():
	main_menu.hide()
	enet_peer.create_server(PORT)
	multiplayer.multiplayer_peer = enet_peer
	multiplayer.peer_connected.connect(add_player)
	add_player(multiplayer.get_unique_id())

func _on_join_pressed():
	main_menu.hide()
	enet_peer.create_client(address_entry.text, PORT)
	multiplayer.multiplayer_peer = enet_peer

func add_player(peer_id):
	var player = Player.instantiate()
	player.name = str(peer_id)
	add_child(player)

This seems like a common task but still I couldn’t manage to find any Godot 4 solution on the Internet. I will be grateful if you share any advice on how to render only player’s gun on their screen and still avoid gun clipping.

The Godot project i’m working on is based on DevLogLogan’s Online Multiplayer FPS From Scratch tutorial: https://youtu.be/n8D3vEx7NAE

Hi!, What if you put the viewport in a group and then, when you create a player, check if that viewport already exists?

if(get_tree().get_nodes_in_group("weapon_viewport").size()==0):
   //ceate viewport

crossbito | 2023-06-06 15:23