Following along with Bram I found an issue that was previously discussed with a different end result (that user had made a new very different weapon).
When I shoot my Rifle it expends both my Medium and Small ammo types and vice versa, SMG fires Rifle rounds.
I am not sure what the issue could be the code looks spot on and I set the ammo types as Bram did using the inspector assign function.
I thought it could be caused by the copy pasting of the weapon models/scenes but that’s exactly what Bram did and did not exhibit this issue.
Video of the issue:
https://jumpshare.com/embed/aHPjJuhhr4Y8IUB3AKx8
extends Node
class_name AmmoHandler
@export var ammo_label: Label
enum ammo_type{
LARGE_AMMO,
MEDIUM_AMMO,
SMALL_AMMO,
}
var ammo_storage := {
ammo_type.LARGE_AMMO: 6,
ammo_type.MEDIUM_AMMO: 12,
ammo_type.SMALL_AMMO: 60,
}
func has_ammo(type: ammo_type) -> bool:
return ammo_storage[type] > 0
func use_ammo(type: ammo_type) -> void:
if has_ammo(type):
ammo_storage[type] -= 1
update_ammo_label(type)
func update_ammo_label(type: ammo_type) -> void:
ammo_label.text = str(ammo_storage[type])
extends Node3D
@export var weapon_1: Node3D
@export var weapon_2: Node3D
func _ready() -> void:
equip(weapon_1)
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("weapon_1"):
equip(weapon_1)
if event.is_action_pressed("weapon_2"):
equip(weapon_2)
if event.is_action_pressed("next_weapon"):
next_weapon()
if event.is_action_pressed("previous_weapon"):
next_weapon()
func equip(active_weapon: Node3D) -> void:
for child in get_children():
if child == active_weapon:
child.visible = true
child.set_process(true)
child.ammo_handler.update_ammo_label(child.ammo_type)
else:
child.visible = false
child.set_physics_process(false)
func next_weapon() -> void:
var index = get_current_index()
index = wrapi(index +1, 0, get_child_count())
equip(get_child(index))
func previous_weapon() -> void:
var index = get_current_index()
index = wrapi(index -1, 0, get_child_count())
equip(get_child(index))
func get_current_index() -> int:
for index in get_child_count():
if get_child(index).visible:
return index
return 0
As a less important, yet also very visually annoying bug/issue you may have noticed in the video is whenever I switch weapons, one frame of the muzzle_flash particle effect plays when the new weapon appears on screen. I am not sure why this is happening either. I even tried completely remaking the particle effects and it continues to happen.