How to switch weapons?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Serenade
:warning: Old Version Published before Godot 3 was released.

How can you make 2D character that has 2 similar functions, like, 2 type of guns (sprite and logic) and switch between them with buttons like 1 and 2?
How would you handle this?

:bust_in_silhouette: Reply From: VitaZheltyakov

Use - export var

This doesn’t answer his question at all. He wants to be able to change weapons during gameplay using a keypress. export var makes a variable appear in the Inspector so you can change it using the GUI.

kidscancode | 2017-08-15 21:18

:bust_in_silhouette: Reply From: kidscancode

Add a Sprite for each of the weapons to your player scene. It’s OK if they overlap, because you’re going to only make one of them visible at a time, using the visibility/opacity property.

First, have a variable to track which weapon:

var current_weapon = 1

Use input to switch it:

func _input(event):
    if event.is_action_pressed("weapon_1"):
        current_weapon = 1
        get_node("weapon 1").set_opacity(1)
        get_node("weapon 2").set_opacity(0)
        # you can also trigger animations or play sound effects here
    elif event.is_action_pressed("weapon_2"):
        current_weapon = 2
        get_node("weapon 1").set_opacity(0)
        get_node("weapon 2").set_opacity(1)

Then whenever the player uses the weapon (keypress, autofire - however you have it implemented), call the appropriate function that fires a bullet, plays an animation, etc.

nice way of doing it…i was more thinking about making a different sprite for the guns and simply changing the sprite and the properties in the code itself(and maybe the bullet’s sprite) that way you sort of use less resources i believe(changing stuff like rate of fire, fire type or even the whole firing function based on the weapon used)

rustyStriker | 2017-08-15 21:42

You could also use hide() and show() instead of setting opacity to 0 or 1, remember these won’t prevent the sprite from being processed by the graphics card, and it’s simpler to write :wink:

Also if your game features more weapons, you can also use an array and have a single function to set the current one by index.

Zylann | 2017-08-15 23:24

You should not be worrying about “less resources” - just find something that works and do it. If it turns out to actually cause a performance problem, then you worry about optimizing it. Otherwise you’re just wasting your time on an overly complicated solution that you don’t need.

You could load two separate textures and use set_texture() to change them in the code, but that seems more complicated to me, and placement would be more difficult if they’re not exactly the same size. Using my solution, you can visually arrange each weapon so that it appears in the right place, which is sort of the point of having a GUI dev environment in the first place! :slight_smile:

kidscancode | 2017-08-15 23:24

How would you handle the current_weapon though?

Serenade | 2017-08-16 15:35