Godot 4.2.1
After the player drops the gun they cant repick it. Why?
Player’s code
extends CharacterBody2D
class_name Player
u/onready var animated_sprite_2d = $AnimatedSprite2D
u/onready var gun = $Gun
const speed : float = 10000.0
var is_moving : bool = false
var gun_scene : PackedScene = null
var gun_in_range : bool = false
var gun_name : String = “”
func _physics_process(delta):
movement()
animate(Xdirection)
if gun_in_range:
pick_gun()
drop_gun()
move_and_slide()
func pick_gun():
if Input.is_action_just_pressed(“Pick”):
var new_gun = gun_scene.instantiate()
gun.add_child(new_gun)
get_parent().find_child(gun_name).queue_free()
func drop_gun():
if Input.is_action_just_pressed(“Drop”):
var dropped_gun = gun.get_child(0).duplicate()
get_parent().add_child(dropped_gun)
gun.get_child(0).queue_free()
dropped_gun.position = position
Gun’s code
extends Node2D
class_name Gun
func _on_pick_area_body_entered(body):
if body is Player:
var player = get_parent().find_child(“Player”)
player.gun_scene = preload(“res://shot_gun.tscn”)
player.gun_in_range = true
player.gun_name = “ShotGun”
func _on_pick_area_body_exited(body):
if body is Player:
var player = get_parent().find_child(“Player”)
player.gun_scene = null
player.gun_in_range = false