im making a wolfenstein 3d style fps, so the weapon sprites use canvas layers:

and i have a weapon swapping system, and i do not know how to make it hide the canvas layer of the weapon.

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")

    JetteBoyTV use classes.

    1 - create a new script that will be the base of your class:

    weapon_class.gd

    class_name WeaponClass
    extends Node3D
    
    func weapon_sprite_visibility(vis : bool = false) -> void:
        pass

    2 - now, in each of the scripts of each of the weapons (or the same if they share the same script), extend from WeaponClass instead of Node3D and override the function weapon_sprite_visibility

    
    extends WeaponClass
    
    func weapon_sprite_visibility(vis : bool = false) -> void:
        $CanvasLayer.visible = vis

    3 - finally, on weapon swap, call the function to hide the CanvasLayer:
    weapon_sprite_visibility(true)


    oh, I see what you mean, I wrote all this and you are asking something else.
    well, start by putting your weapons into a dictionary on ready, each will have a number:

    
    var weapon_nodes : Dictionary = {}
    
    func _ready():
        weapon_nodes[0] = $Knife
        weapon_nodes[1] = $HandGun

    now, when going to next, add 1, check if it overflows and if it does go to 0

    Current_weapon += 1
    if Current_weapon > 2:#3 weapons
        Current_weapon = 0
    #then hide the weapons
    for i in get_children():
        i.hide()
    #then show the current one
    weapon_nodes.get(Current_weapon, $Knife).show()

      JetteBoyTV the solution chatgpt gave me didnt work

      Ask it again. Sometimes it gives wrong answers and sometimes it gives right answers. A very knowledgeable chatgpt expert told me that.

        xyz are you sure it wasnt chatgpt that told you that?

        • xyz replied to this.

          kuligs2 are you sure it wasnt chatgpt that told you that?

          Of course not. How can one ever be sure? At this point in human history, ai zealots got so advanced in mimicking the ai that they are practically indistinguishable from each other.

          JetteBoyTV that stuff didnt work, im basically just trying to hide the weapon scenes canvas layer when that weapon isnt the active weapon

          what exactly is the problem?

          xyz Ask ChatGPT.

          you keep saying that, but it's not good advice. it almost sounds like you are trying to start a meme.

          JetteBoyTV xyz the solution chatgpt gave me didnt work

          don't use chatGPT.

          xyz Ask it again. Sometimes it gives wrong answers and sometimes it gives right answers. A very knowledgeable chatgpt expert told me that.

          it always, consistently, gives bad answers.
          the problem with stupidGTP is it doesn't have context of your project and it only gives you code, but making a game also requires you to click on shit in the tree, the inspector and the tools, and to use the animationplayer, etc.
          it also can't tell the difference between godot 3 and 4 code, because it's a stupid chatbot. and it will only get worse as it continues to mix old code, new code, and code from other engines, and made up shit.
          just look at it playing chess: king takes its own pawn and generates a bishop outside the board.

            Jesusemora it always, consistently, gives bad answers.

            Not according to DaveThePrompter.

            xyz chatgpt didnt work, asking it again wont work, every time i have asked it to help solve other problems in the past it hasnt worked, just dont say anything or actually try to help me instead of telling me to ask a ai that never gives working solutions to problems in godot, chatgpt is good at a lot of things, but solving problems in godot is not one of them

            • xyz replied to this.

              Jesusemora my problem is that the weapon swapping code wont hide the currently unused weapons

              ok let's look at your code again:

              is this working? are the weapons hidden when the game starts?

              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)

              instead of using for child in get_child_count() I would use:

              for i in get_children():
              	i.hide()

              this could be causing problems:
              get_child(Current_Weapon).set_process(true)
              use a variable to tell the weapon if it should be active or not, setting this to true or false could be causing show() and hide() to not work.

              var is_active : bool = true
              func _process(delta):
              	if is_active:
              		if Input.is_action_just_pressed... etc
              for i in get_children():
              	i.hide()
              	i.is_active = false
              get_child(Current_Weapon).show()
              get_child(Current_Weapon).is_active = true

              Current_weapon is an int? so you are using the index for get_child.
              you should make sure the variable is restored to 0 if it overflows, otherwise it will return null
              https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-get-child

              if Input.is_action_just_released("Next_Weapon"):
              		Current_Weapon = Current_Weapon+1
              		if Current_Weapon > 3:#max number weapons
              			Current_Weapon = 0
              		switch_weapon()

              here is the same thing, the problem is probably because you are setting process to false, which disables logic on the node and prevents the show() and hide() functions from working?

              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)

              you are also using Current_Weapon-1 for some reason.
              also this code is problematic, I would just remove it:

              if Current_Weapon == get_child_count():
              		Current_Weapon = 0

              also try adding a print to

              if Input.is_action_just_released("Next_Weapon"):
              		Current_Weapon = Current_Weapon+1
              		switch_weapon()
              		print("next weapon")

              if you are using the scroll wheel it could be causing problems, the scroll wheel is weird. try using a button like q and see if it works.

                JetteBoyTV xyz chatgpt didnt work, asking it again wont work, every time i have asked it to help solve other problems in the past it hasnt worked, just dont say anything or actually try to help me instead of telling me to ask a ai that never gives working solutions to problems in godot, chatgpt is good at a lot of things, but solving problems in godot is not one of them

                Maybe you're just ignorant about ai. You need to treat chatgpt like a very knowledgeable person who sometimes gives right and sometimes wrong answers. Just make sure that the answer is right before using it.

                  xyz dude, chatgpt isnt designed to fix problems in a video game engine, its designed to do writing, asking chatgpt to help problems in godot will damn near never be correct because asking it about something like that is simply misusing the ai because its not designed to do that. chatgpt cant even tell the difference between godot 3 and 4 code, and any code solution it gives you could be completely made up, this happens because, CHATGPT IS NOT DESIGNED TO FIX GAME ENGINE CODE

                  • xyz replied to this.

                    JetteBoyTV As I replied 5 days ago, animationsprite3d node doesn't need canvaslayer node,so there will be no such problem.

                    JetteBoyTV dude, chatgpt isnt designed to fix problems in a video game engine, its designed to do writing, asking chatgpt to help problems in godot will damn near never be correct because asking it about something like that is simply misusing the ai because its not designed to do that. chatgpt cant even tell the difference between godot 3 and 4 code, and any code solution it gives you could be completely made up, this happens because, CHATGPT IS NOT DESIGNED TO FIX GAME ENGINE CODE

                    Would you mind explaining that to Dave"TheCoder"?