Issues with hiding unselected weapons in fps weapon swapping code

godot 4

im making a wolfenstein 3d style fps, and i have a weapon swapping system, and i do not know how to make it hide the currently unselected weapons
Screenshot 2024-07-30 203409
Screenshot 2024-07-30 211501
Screenshot 2024-07-30 210823
.

weaponswapping code:

extends Node3D

var Current_Weapon = 0

func _ready():
	for child in get_child_count():
		get_child(child).hide()
		get_child(child).set_process(false)
	get_child(Current_Weapon).show()
	get_child(Current_Weapon).set_process(true)

func _process(delta):
	if Current_Weapon == get_child_count():
		Current_Weapon = 0
		
	#ask godot forums how to make previous weapon thing
	if Input.is_action_just_released("Next_Weapon"):
		Current_Weapon = Current_Weapon+1
		switch_weapon()

func switch_weapon():
	for child in get_child_count():
		get_child(child).hide()
		get_child(child).set_process(false)
	get_child(Current_Weapon-1).show()
	get_child(Current_Weapon-1).set_process(true)

weapon code:

extends Node3D

@onready var Weapon_Sprite = $CanvasLayer/Control/WeaponSprite

@onready var Hitscan = $Hitscan.get_children()

var can_fire = true

func _ready():
	Weapon_Sprite.play("Idle")
	
func check_hit():
	for ray in Hitscan:
		if ray.is_colliding():
			if ray.get_collider().is_in_group("Enemy"):
				ray.get_collider().take_damage(5)
	
func _process(delta):
	if Input.is_action_just_pressed("main_fire_weapon") and can_fire:
		Weapon_Sprite.play("Fire")
		check_hit()
		can_fire = false
		
		await Weapon_Sprite.animation_finished
		
		can_fire = true
		Weapon_Sprite.play("Idle")

The default node layout shows all the weapons. Hide all the weapons. Then have the code switch show and hide like it’s supposed to do in the code. The rest of the baby code sounds like a personal problem.
jogot 1

I have not found any other workarounds. But, delta needs to be coded like this. Because, it will run an infinite iteration on your solution. Without actually counting 1. Sometimes, also returning infinite. This way seems to work on several occasions without issues.

ive set the weapons to hidden, and im now using the set_visible() func to make the weapons visible or hidden, and its still not working

func switch_weapon():
	for child in get_child_count():
		get_child(child).set_visible(false)
		get_child(child).set_process(false)
	get_child(Current_Weapon-1).set_visible(true)
	get_child(Current_Weapon-1).set_process(true)

why is this function using -1 when the duplicate version on _ready does not subtract? That seems like an issue, but I don’t believe you’ve stated what you expect to happen vs what really happens, so it’s hard to tell what you are having trouble with.

what im having trouble with is that the currently unused weapons wont go hidden and i want the currently unused weapons to be hidden, ive already stated what my problem was this. i dont get how its hard to understand what my problem is, because ive been as clear as i can, i need currently unused weapons to be hidden

After a little bit of testing I see that CanvasLayer does not inherit the visibility of it’s Node3D parent.

2024-08-04-013953_303x191_scrot notice only the 3D nodes are greyed out

If you want to hide the weapon you will need to hide it’s CanvasLayer instead.

for child in get_child_count():
	get_child(child).get_node("CanvasLayer").hide()
	get_child(child).set_process(false)

Sorry about the confusion I just couldn’t tell from the screen shot what was wrong. Still that -1 seems like an issue.

Thanks! this worked!

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