Drop function I made doesn't work on all weapons

Godot Version

4.2.1

Question

I’m currently making a Hotline Miami esque game and ran into an issue. I currently have a weapon class that has a function to drop. The problem is that whenever dropping a weapon, it works on one weapon but when dropping the other the weapon just vanishes. Code below

extends Node2D
class_name Weapon

@export var strength : float = 1000
@export var can_drop : bool
@export var player : Player

var weapon_pickup

func _physics_process(delta):
	pass

func drop():
	if can_drop:
		if weapon_pickup != null:
			var wp = weapon_pickup.instantiate()
			var throw_direction = (get_global_mouse_position() - global_position).normalized()
			wp.global_position = get_parent().global_position 
			wp.linear_velocity = throw_direction * strength
			wp.rotation = get_parent().get_parent().rotation
			get_parent().get_parent().get_parent().get_parent().add_child(wp)
			if wp is RigidBody2D:
				var rotation_speed : int = 10  # Adjust this value to control the rotation speed
				wp.angular_velocity = rotation_speed

The working gun:

extends Weapon

@onready var bullet = preload("res://weapons/pistol/bullet/bullet.tscn")

var is_shooting = false


# Called when the node enters the scene tree for the first time.
func _ready():
	weapon_pickup = load("res://weapons/pistol/pistol_pickup.tscn")

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Input.is_action_just_pressed("shoot"):
			shoot()

func shoot():
	if !is_shooting:
		#is_shooting = true
		#$Delay.start()
		# add new Bullet scene to World
		var Bullet = bullet.instantiate()
		get_tree().current_scene.add_child(Bullet)

		# set Bullet position at the gun barrel position
		Bullet.global_position = $barrel.global_position

		# set Bullet rotation_degrees equal current Gun's rotation_degree
		Bullet.rotation_degrees = global_rotation_degrees
		Bullet.velocity = get_global_mouse_position() - Bullet.global_position

The non-working gun:

extends Weapon

@onready var bullet = preload("res://weapons/smg/bullet/bullet.tscn")

var is_shooting = false


# Called when the node enters the scene tree for the first time.
func _ready():
	weapon_pickup = load("res://weapons/smg/smg_pickup.tscn")

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Input.is_action_pressed("shoot"):
			shoot()

func shoot():
	if !is_shooting:
		is_shooting = true
		$delay.start()
		# add new Bullet scene to World
		var Bullet = bullet.instantiate()
		get_tree().current_scene.add_child(Bullet)

		# set Bullet position at the gun barrel position
		Bullet.global_position = $barrel.global_position

		# set Bullet rotation_degrees equal current Gun's rotation_degree
		Bullet.rotation_degrees = global_rotation_degrees
		Bullet.velocity = get_global_mouse_position() - Bullet.global_position



func _on_delay_timeout():
	is_shooting = false 

I’ve got no clue what I’m doing wrong, i tried attaching the pistol script to the smg to see if that would fix it but it didn’t.

Animation

Likely one of these two if statements is bypassing the drop code.
Since weapon_pickup is assigned in the _ready() function it is not likely to be the culprit.
can_drop is an export variable. Are you assigning it for the newly added weapon?
If you start with the smg gun can you drop it?

1 Like

I figured it out. The can_drop variable didn’t have a default state and i guess the engine didn’t know what to do with it. Thank you for pointing out my issue.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.