why is only one section of code working even though all three are the same

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

I am trying to make a weapon switching mechanic to my game. however, only one line of the nearly identical code is working and I am getting thousands of errors a second. the central issue of the code is the “test_weapon_select” function and how only the last line works. also, even though I’m only spawning in one instance, it is extremely slowing my computer down.
code:

extends CharacterBody3D

const SENS_DIV = 240
const SPEED = 5.0
const JUMP_VELOCITY = 4.5

var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)
var current_weapon = 1

@onready var neck = $Neck
@onready var camera = $Neck/Camera3D
@onready var right_hand = $Neck/Camera3D/Right_Hand
@onready var test_weapon_var1 = preload(“res://test_objects/test_object.tscn”)
@onready var test_weapon_var2 = preload(“res://test_objects/test_object_2.tscn”)
@onready var test_weapon_var3 = preload(“res://test_objects/test_object_3.tscn”)

@export var sens = 1.2/SENS_DIV

func _unhandled_input(event):
if event is InputEventMouseButton:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event.is_action_pressed(“ui_cancel”):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
	if event is InputEventMouseMotion:
		neck.rotate_y(-event.relative.x * sens)
		camera.rotate_x(-event.relative.y * sens)
		camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-90), deg_to_rad(90))

func _physics_process(delta):
if not is_on_floor():
velocity.y -= gravity * delta

if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMP_VELOCITY

var input_dir = Input.get_vector("left", "right", "forward", "backward")
var direction = (neck.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()

if direction:
	velocity.x = direction.x * SPEED
	velocity.z = direction.z * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)

move_and_slide()

test_weapon_select()

func test_weapon_select():
if Input.is_action_just_pressed(“1”):
current_weapon = 1
elif Input.is_action_just_pressed(“2”):
current_weapon = 2
elif Input.is_action_just_pressed(“3”):
current_weapon = 3

if current_weapon == 1:
	var weapon_one = test_weapon_var1.instantiate()
	right_hand.add_child(weapon_one)
elif current_weapon != 1:
	var remove_one = right_hand.get_child(0)
	right_hand.remove_child(remove_one)

if current_weapon == 2:
	var weapon_two = test_weapon_var2.instantiate()
	right_hand.add_child(weapon_two)
elif current_weapon != 2:
	var remove_one = right_hand.get_child(0)
	right_hand.remove_child(remove_one)

if current_weapon == 3:
	var weapon_three = test_weapon_var3.instantiate()
	right_hand.add_child(weapon_three)
elif current_weapon != 3:
	var remove_one = right_hand.get_child(0)
	right_hand.remove_child(remove_one)

any help is appreciated

:bust_in_silhouette: Reply From: Enfyna

Try to move your testweaponselect function call from physics process to a input(event) function. You are basically instantiating a weapon and trying to delete two weapons every frame. This can slow down your game alot.

And in your testweaponselect function combining your elif statements would be better. They are all doing the same thing and lets say your current weapon is 1 the function will add the weapon first in if current_weapon == 1: then it will try to delete that weapon twice in the != 2: and !=3: elifs. Just make a new variable in your function like new weapon then check if new weapon and current weapon is different, if it is delete the current weapon and add the new one.

Something like :

func testweaponselect():
var new_weapon
if Input.isactionjustpressed("1"):
    new_weapon = 1
elif Input.isactionjustpressed("2"):
    new_weapon = 2
elif Input.isactionjustpressed("3"):
    new_weapon = 3

if current_weapon == new_weapon:
    return # weapon isnt changed no need to do anything

# delete current weapon
var remove_one = right_hand.get_child(0)
right_hand.remove_child(remove_one) 

# add the new weapon
if new_weapon == 1:
    var weapon_one = test_weapon_var1.instantiate()
    right_hand.add_child(weapon_one)

if new_weapon == 2:
    var weapon_two = test_weapon_var2.instantiate()
    right_hand.add_child(weapon_two)

if new_weapon == 3:
    var weapon_three = test_weapon_var3.instantiate()
    right_hand.add_child(weapon_three)
 
current_weapon = new_weapon #new weapon is now our current weapon