How do I replace weapons from my player character's hand if the character already has a weapon in it's hand?


#1

I’m doing another Gdevelop project, so I can become familiar with Gdevelop. Lat time I was working on an experimental Top Down Shooter, which I thought it was a failure. I did try to ask some question in this community, but my question and example wasn’t enough to make people understand. But that’s fine, I’ll find out some day. So the pictures below are my new project, take a look and please help answering the question. Thank you.


How do I make my player character switch between two or more weapons?
#2

You didn’t explain the issue last time. You just had a title, a video and said ‘Fix it’. You never provided any snippets of the events. But anyway.

From reading your events snippet (which, BTW help a lot in figuring out what is happening), when you come across a RPG or a HandCannon, you delete it and recreate it at the co-ordinates or Player.Hand.X and Player.Hand.Y. There are a few things here :

  1. If Player.Hand is within the player’s collision box, the RPG and/or HandCannon will be deleted and recreated. Every. Single. Frame. This is not good practice.
  2. You’re not removing the existing weapon if you pick up a new one.


    My way of approaching this is :
  • Create an object group (in the editor) called SelectedWeapon, in which each weapon type is a member.
  • When the player collides with a weapon:
    • delete any existing SelectedWeapon.
    • create a new SelectedWeapon, using the name of the weapon you just collided with,
    • delete the weapon you collided with.
  • Finally, make an event with no condition, in which the only action is to place SelectedWeapon at Player.Hand

#3

Yeah, I’m really sorry about last time, I’m just hopeless.


#4

Sorry to say this, but, I still don’t understand. Can you explain a little more? I’m still confused.Thank you.


#5

Which part is unclear? The reason, or my way of approaching it? Just so I know what bit to explain better, rather than cover things you do understand.


#6

The part about how you approach


#7

Ok, I’ll detail it in a few hours. I’m at work at the minute, and can’t spend a fair chunk of time on this until I get home.


#8

Take your time, I won’t force you


#9

I’ve had to add a couple of things to make it work, but here is a gif of the end result and a quick walk through:

PickUpWeapon

So first, create the SelectedWeapon group:
image

Then add your weapons to this group:

Next add the events:

I’ve added a scene text variable _PickedUpWeapon, to keep track of the weapon that the player just collided with.

I also added a boolean object variable IsCarried to indicate whether the wepaon is on the player.

When testing for collision with any of the gun objects in the SelectedWeapon group, just refer to the group name. You can get the specific weapon object name from SelectedWeapon.ObjectName():

Also, note that you can add variables without having to declare them. GDevelop will add _PickedUpWeapon and IsCarried without having to declare them in the scene variable list or on the objects.

[edit] - added events image with comments.


#10

Might I ask what and how a boolean object/value works?


#11

Booleans are a variable that have one of two values - true or false. They default to false. They’re relatively new in GDevelop, and I think came out in version 106.

You access them just like any other variable, and you’ll notice the options for variable conditions and action now include boolean. To define a boolean variable, click on the little spanner, and you can change it to a boolean