Have a weapon switching system that dont work as i thought it would,. How can i make it work?

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

I followed a tutorial for this weapon switching system. Its supposed to hide and deactivate the script of the guns that are not the current gun. However it doesn’t seem to actually deactivate the hidden guns.

i got a weapon manager with this script:


var current_weapon_index = 0

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

func _process(delta):
if current_weapon_index == get_child_count():
	current_weapon_index = 0
	
if Input.is_action_just_pressed("weapon_switch"):
	current_weapon_index = current_weapon_index + 1
	switch_weapons()

	

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

and a gun script:

onready var pistol_anims = $pistolanims
onready var pistol_smoke = $pistoleffectsmanager/pistolsmoke
onready var pistol_flare = $pistoleffectsmanager/pistolflare

func shoot():
if Input.is_action_just_pressed("fire"):
	#if !pistol_anims.is_playing():
		print("shot")
		pistol_anims.play("RESET")
		pistol_anims.play("shoot")
		pistol_smoke.emitting = true
		pistol_flare.emitting = true

func _physics_process(_delta):
if Input.is_action_pressed("fire"):
	shoot()

the problem is that if gun2 is selected and I shoot, then switch back to gun1, the particles and print(“shot”) still comes up 2 times from one click, as both guns are shooting once each.

how can I fix this so whenever gun1 is selected, gun2 is deactivated?
Help is badly appreciated!

Please edit your post and format the code for the forum. Otherwise, it’s really hard to read. To do that:

  • Edit the post
  • Select the block of code
  • Press the small { } button in the editor’s toolbar
  • Look at the Preview panel to ensure proper formatting

jgodfrey | 2022-08-11 15:07

alright i think ive fixed the formatting

King C rocodile | 2022-08-11 15:16

There are definitely still some indention problems above. Since gdscript is indention dependent, that’s a problem for interpreting the code.

For example, this indention is wrong:

func _process(delta):
if current_weapon_index == get_child_count():

And, if this code is indented as shown in the forum, it looks potentially problematic to me.

for child in get_child_count():
    get_child(child).hide()
    get_child(child).set_process(false)
    get_child(current_weapon_index).show()
    get_child(current_weapon_index).set_process(true)

But, I don’t know how your actual code is formatted. Please try to clean that up.

jgodfrey | 2022-08-11 15:50

To be fair it didn’t look like that on my PC, but i see what you mean here on my cellphone.
The indentation is different in the script

King C rocodile | 2022-08-11 17:08

:bust_in_silhouette: Reply From: Bernard Cloutier

It’s possibly because you are switching in _process, but shooting in _physics_process. The first might run slower than the second.

I see two possible fixes:

  • use _physics_processin both places.
  • use Input.is_action_just_pressed so that you only shoot in the first frame where this action is pressed.

Alright, makes sense. Il try that thanks!

King C rocodile | 2022-08-11 17:09