Switch system for gun

Godot Version

4.4

Question

Hi everyone I hope you all have a great day. Well I have a question I made different guns but how can I make a switch system just like in hotline-miami that when you are in the area of that gun by pressing f key for example ,you can grab that gun and switch it with the current gun .I would be so happy if you guys tell me .thanks

Hi!

That’s a very vague problem.

To detect that you’re inside an area, you can use Area2D nodes as they’re designed precisely for that.
To detect that you’re pressing F, you can use Godot’s input system, by defining an action called “pickup_weapon” or something, and get it through code using Input.is_action_pressed("pickup_weapon").
To switch weapon, well, it depends on a lot of things, mostly how your weapons are represented in your code. Are they classes you instantiate in code? Custom resources? Something else? Anyway, you should probably have some switch_weapon() function that takes in the new weapon as a parameter, but it’s impossible to give you a full explanation on how to do that, as we lack context here.

Is there anything more specific you need to know? If not, I’d suggest you simply go for a first implementation, and ask questions with more context if you’re stuck :slight_smile: (not that I don’t want to help you right now, ofc, but I guess you see my point)

Ok well about the classes you said I made a gun as a mother gun and then I made other guns by instance from this mother gun um about input yeah in godot there is a system that you can choose which key should be for walk ,shoot or other things but as you said about swtich function should I write it in the main node that everything plays there or should i write it in the mother gun if you want I can send you my codes and thanks for your answer.and I think I get it what you mean since you don’t know my code you can’t really guess which way should I go right?

[…] about swtich function should I write it in the main node that everything plays there or should i write it in the mother gun

[…] since you don’t know my code you can’t really guess which way should I go right?

Well, for the reason you explained yourself, I cannot tell where to put your code. :sweat_smile:

You should honestly just give it a try and see how it goes. For instance, put this in your “main node”, and see if it’s troublesome or if it works fine. Keep in mind that you can always come back to any code and refactor it if needed, your first decision is never set in stone.

1 Like