Need help with weapon switching system C#

Godot Version



how to make a weapon changing system with mouse wheel control for a 3D game in C#

  1. Create an action under Project Settings > Input Map for weapon switching. Like next_weapon and prev_weapon bound to Mouse Wheel Up and Mouse Wheel Down events.
  2. Godot has a multitude of ways to handle inputs, but per the docs one way would be to check Input.IsActionJustPressed("next_weapon") (Input) in the _Process method of your weapon handler node or character script. I’d also recommend creating a private static readonly StringName NextWeapon = "next_weapon" field to avoid recreating StringNames on every frame, which may cause some GC problems.
  3. Then…the rest is up to you. Deselect the old weapon and select the new weapon somehow.

during the day I just completed 2 points (except for the recommendation), but I can’t do the third point

Now I still have this script for picking up a weapon from the floor and processing the scrolling of the mouse wheel

This is just a simple example ,If you have a weapon pack, you can dynamically destroy the existing weapon and load a new weapon into the scene tree when switching weapons.

extends Node

var weapons = []
var active_weapon_index = 0

func _ready():
    # Load weapons
    var weapon_paths = [
        # Add more weapons
    for path in weapon_paths:
        var weapon = load(path).instantiate()

func _input(event):
    ## When the mouse wheel is up active_weapon_index += 1 else -=1
     ## then call switch_weapon(active_weapon_index)

func switch_weapon(index):
    for i in range(weapons.size()):
        if i == index:
1 Like

I need more help. I have a script to select an object, I need to adapt your example to C# and write it so that they work together. but I don’t understand how to do it

Also…didn’t you already ask about this? Weapon switching system C# - #2

Thanks for the note, I’ve merged the duplicate topics into one