This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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?

in Engine by (227 points)

2 Answers

+3 votes
Best answer

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.

by (22,191 points)
selected by

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)

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

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.

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! :)

How would you handle the current_weapon though?

–5 votes

Use - export var

by (208 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.