Godot Version
4.2.2
Question
I’m working on getting weapon dropping and picking up working in my game but i ran into this issue, i know what the problem is but i cant seem to figure out a proper solution basically when we put this code: get_tree().current_scene.add_child(self) after this code: self.get_parent().remove_child(self) it causes the game to crash when we drop because it doesnt know what scene to join after leaving the player, but if i put the first code before the second code, it works but it cant place the object in the world because the sword is still attached to the player node, whats a way we can get this to work properly, heres the full script for the dropping of the weapon
vvvvvvvvvvvvvvvvvvvvvvvv
Function to drop the sword
func drop_weapon():
if !is_dropped:
# Ensure the sword is not inside the tree before dropping
if self.is_inside_tree():
get_tree().current_scene.add_child(self)
self.get_parent().remove_child(self) # Remove sword from player
self.set_owner(null) # Unset the owner of the sword
self.global_position = player.global_position + Vector3(0, -1, -2) # Drop position (e.g., a little below the player)
self.is_dropped = true # Mark the sword as dropped
rigid_body.visible = true # Make the RigidBody3D visible when dropped
rigid_body.freeze_mode = RigidBody3D.FREEZE_MODE_STATIC # Allow physics to affect the sword now
rigid_body.set_physics_process(true) # Enable physics processing to let the sword fall
# Debugging: Print the position to ensure it’s correctly placed below the player
print("Sword dropped at position: ", self.global_position)
# Disable the 2D sprite
animated_sprite_2d.visible = false