[Solved] How to make Drop and take weapon like COUNTER STRIKE 2D?

I’m making a game like Counter strike 2d/CS2D and I don’t know how to drop and take a weapon, thanks for the help even if I don’t have it yet…

I would make 2 variables
One called CurrentWeapon which would be string variable
And other called WeaponHeld which would be boolean variable

Now if player have no weapon and he is in collision with weapon which is on the ground
Depending on it if you would require a key to pick it up or not but let’s say you do

Condition
Player is in collision with WEAPON < (which would be a group containing all weapon objects)
SOME key is pressed
Trigger Once

Action
Change Object.Variable(CurrentWeapon) set to WEAPON
Change Object.Variable(WeaponHeld) set to TRUE

Another event
Condition
Object.Variable(WeaponHeld) is TRUE

Action
Change position of WEAPON set to Player.PointX(“GunPoint”) Player.PointY(“GunPoint”)

And that should stick gun you are in collision with to your player

Now if you would wish to drop weapon
Condition
SOME key is pressed
Trigger Once

Action
Change Object.Variable(CurrentWeapon) set to NONE
Change Object.Variable(WeaponHeld) set to FALSE

1 Like

I’m very grateful for the quick response, and the long and clear explanation, I’ve been working on this project for a long time but I couldn’t really do it so thank you very much, I hope it helps and be successful, god bless you…

I think i missed an event where name of weapon should be taken from variable of weapon
For example you give each weapon String variable called WeaponName
And put there name of that weapon
For example for pistol object you put there Pistol
For assault rifle you would give it M4 or something

Condition
Player is in collision with WEAPON
SOME key is pressed
Trigger Once

Action
Change CurrentWeapon variable to WEAPONS variable WeaponName
Change Object.Variable(WeaponHeld) set to TRUE

Maybe it will need more tweaking but bottom line this should work exactly as you expected
So if something is not working just come here

Also good idea would be to print CurrentWeapon variable to text to see what exactly it is atm

BTW CurrentWeapon and WeaponHeld should be your player object variable

Reply the result and we will figure something if anything goes wrong

1 Like

ohhh, now I understand the variables little by little because of you, thanks man, it gave me a lot of knowledge, but you said the current weapon and weapon held are only variables in my player? because I put it on 1 object, not a group object, and another thing… when I lowered the gun, he just stayed in that position and there was no force

Variables are nothing else than name tags


I can attach name tag to your chest and write anything on it
With only difference i can change whatever i wrote on it with some commands
For example i can write on yours
LeftHandedPerson

And now i can tell everyone on this forum that if they see any1 with name tag with word LeftHandedPerson they should send :wink: to that person

And this is how variable works
You create variable name it something and give it some value
And then you can check if that variable is at that value or not
If it is you perform some action
in above example if value is LeftHandedPerson
Action send :wink:

Now show your events for dropping and picking up weapon

1 Like

wow the explanation is very clear, now I understand clearly how the string variables work and of course also the boolean, you explained a lot for me to understand thank you very much, now I know how to get the different weapons​:clap::clap::clap:

1 Like

bro, I want you to know that my previous problem has really been solved, but I have another problem. how can the player collide with a desert eagle just once and once they collide they can’t collide again, and when I drop the gun they can collide once again, I hope you get it.
giphy (1)
giphy

I don’t get you
Step by step
Player drops pistol
Player collide with pistol
And now what you expect?
Or you mean something else?

1 Like

yes, in my previous project I successfully get the dropped guns that are close to the player or collided with the player, but the problem is, when I pick up the desert eagle gun, I get the other desert eagle again when they collide again of the player, what I would like is that once I get the desert eagle once, I will not pick up the desert eagle again or the pistols group

So you would need to make variable for either each weapon or weapon group
So they do not duplicate
A boolean variables
And now for example you went with group weapons
You make variable Pistols which is false
And now when you pick up desert eagle or revolver you change Pistols variable to true

And now you need your event for picking weapons in this pistols have condition
Boolean variable Pistols is FALSE

And when you pick up weapon Set it to true
And now you can’t pick 2nd pistol

1 Like

My solution to picking up guns is to have a group called guns. For every object in this group, add a boolean called isEquipped. Now whenever you want the gun to be picked up, (on collision or button press), just loop through all of the guns in the group and see if the meet the requirements (close enough, etc.). If it does, set all guns equipped gun to false, and set gun you want equipped to true. You can handle your own logic for visuals. If you want slots, just extend this to isEquipped slot1, isEquipped slot2.

1 Like

thanks a lot to both of you, I understand a bit, so does that mean I will create a boolean variable for each weapon object? not in the player? because in the player the boolean I made is only weaponheld…and do I still need to make a boolean for each group object like pistols/ smgs/ rifles like that?

It can be player variable
And look if you want be able to have 2 weapons of same group then you need to do it per weapon
IF you want to allow only one weapon per group then you can do it per group

1 Like

ok maybe i can do it thanks again…

1 Like
  1. Create one gun
  2. Add a Boolean to it called isEquipped. If your game has multiple slots, add isEquipped1, isEquipped2 and so on
  3. Make it so all equipped guns go to player. Hide one’s that aren’t in active slot
  4. Make a group called guns or weapons
  5. Add your gun to this group
  6. Every frame, loop through all guns and make two variables called closestgunX and closestgunY. When looping, if the gun is closest to the player, set the variables to the guns x and y
  7. If the player presses f, check if closestgunx and y are close enough. If they are set all weaponsEquipped# to false and set the closest one to true

If you need any more help, just ask :grinning:

1 Like

bro I did it successfully because of you, my player now accepts only one pistol and when it is accepted it does not accept any more pistol, thank you both very much. i will ask again if something qoes wrong again😅

1 Like