Manually spawn weapons fixed

Pop the world weapon scene into weaponcontainer
This commit is contained in:
Twirpytherobot
2025-11-16 20:56:34 +00:00
parent 3431c12dc2
commit 38994776df
5 changed files with 121 additions and 13 deletions
+66
View File
@@ -39,15 +39,81 @@ func _ready():
add_child(weapons_container)
print("Created WeaponsContainer")
# Clients: Remove manually placed weapons (server will sync them via RPC)
if not multiplayer.is_server():
_cleanup_manual_weapons_on_client()
return
Network.connect("player_connected", Callable(self, "_on_player_connected"))
multiplayer.peer_disconnected.connect(_remove_player)
# Initialize any manually placed weapons in the scene
_initialize_manual_weapons()
# Spawn initial weapons when server starts
_spawn_initial_weapons()
func _cleanup_manual_weapons_on_client():
"""Remove manually placed weapons on clients (server will sync them via RPC)"""
if not weapons_container:
return
var weapons_to_remove = []
for child in weapons_container.get_children():
if child is WorldWeapon and child.weapon_id == -1:
weapons_to_remove.append(child)
for weapon in weapons_to_remove:
print("[Client] Removing manually placed weapon: ", weapon.name)
weapon.queue_free()
func _initialize_manual_weapons():
"""Initialize any WorldWeapon nodes manually placed in the level scene"""
if not multiplayer.is_server():
return
if not weapons_container:
return
# Find all WorldWeapon nodes in the weapons container
var manual_weapons = []
for child in weapons_container.get_children():
if child is WorldWeapon:
manual_weapons.append(child)
if manual_weapons.is_empty():
print("[Server] No manually placed weapons found")
return
print("[Server] Found ", manual_weapons.size(), " manually placed weapon(s)")
# Initialize each manually placed weapon
for weapon in manual_weapons:
# Skip if already initialized (weapon_id != -1)
if weapon.weapon_id != -1:
continue
# Assign unique ID
_weapon_spawn_counter += 1
weapon.weapon_id = _weapon_spawn_counter
# Set deterministic name for networking
var old_name = weapon.name
weapon.name = "WorldWeapon_" + str(weapon.weapon_id)
# Track in active weapons
_active_weapons[weapon.weapon_id] = weapon
# Connect cleanup signal
weapon.tree_exiting.connect(_on_weapon_removed.bind(weapon.weapon_id))
print("[Server] Initialized manual weapon '", old_name, "' with ID: ", weapon.weapon_id, " at position: ", weapon.global_position)
# Verify weapon_data is set
if not weapon.weapon_data:
push_error("Manual weapon '", old_name, "' has no WeaponData assigned!")
continue
func _spawn_initial_weapons():
if not multiplayer.is_server():
return
+7
View File
@@ -14,6 +14,8 @@ var _pickup_area: Area3D = null
var _is_being_picked_up: bool = false
func _ready():
print("[WorldWeapon ", name, "] _ready() called. weapon_id=", weapon_id, ", weapon_data=", weapon_data)
# Set collision layer to "weapon" (layer 3 = bit 4)
collision_layer = 4
collision_mask = 2 # Collide with world
@@ -21,10 +23,14 @@ func _ready():
# Set up physics
if weapon_data:
mass = weapon_data.weight
print("[WorldWeapon ", name, "] Set mass to ", mass)
# Spawn mesh
if weapon_data and weapon_data.mesh_scene:
print("[WorldWeapon ", name, "] Spawning mesh from ", weapon_data.mesh_scene)
_spawn_mesh()
else:
print("[WorldWeapon ", name, "] ERROR: Cannot spawn mesh. weapon_data=", weapon_data, ", mesh_scene=", weapon_data.mesh_scene if weapon_data else "null")
# Create collision shape if not exists
_setup_collision()
@@ -44,6 +50,7 @@ func _spawn_mesh():
# Instantiate mesh
_mesh_instance = weapon_data.mesh_scene.instantiate()
add_child(_mesh_instance)
print("[WorldWeapon ", name, "] Mesh spawned successfully: ", _mesh_instance)
func _setup_collision():
# Check if we already have a collision shape