How do I replace an object on the player when it collides with an object?


So I’m trying to figure out how do I get my player hold a different weapon when it collides with an object
currently my player already has a weapon attached to it from the get go but I want the weapon to get replaced by a new one when it collides with certain power ups but I don’t know commands how to do so.

First part, this is the point where the gun is located at on the player
Second part is when the gun rotates along with the player

Hi, what you basically need is just an event that has the condition: “player collides with power up” and the actions: “delete gun” + (for example) “create shotgun” at player.PointX(“Gun”), player.PointY(“Gun”).

If you have many weapons and don’t want to make a “change position”-event for all of them individually, you could make a weapon group and use that instead.

Ok so I tried this out, and here are the results.
The yellow box represents the power up to be collided with and the blue box represents the new weapon I’m supposed to pick up but when I collided with the yellow box the blue box doesn’t attach itself to the player.

You have Player.PointY(“Gunpoint”) twice.

You can create weapon at X1 Y1 or X99999999 Y32321313565123
I does not matter
What do is you need separate event without condition
And action
Which is named Position

Here you fill where object you want should be moved to
And this will attach your gun to where you want it to be

You mean like this?

Because the results are still the same, it still doesn’t attach itself to the player
So Instead of the player colliding with the yellow box I instead have it collide with the blue box instead so that fixed the problem. but I still have something in mind which is having the weapons on the players hand rotate, I already have a condition for the default gun to rotate along with the player when its facing different directions, my question is can I just make the weapons rotate based on what the player is holding or do I have to make a condition for every weapon?

You have changed the typo with PointY but your event is different (now it is ‘change the position’ instead of ‘create’). My suggestion was to create the new weapon-sprite at the GunPoint in the collision-event and to move the new weapon, like the gun, with the ‘change the position’-action without condition in another block - you’ve got it already, there was just the typo.

If you just want to have one event for rotating (and maybe just one for changing the position) for all your weapons, you can make a group (e.g. ‘weapons’) and apply all the weapon sprites to that. In your events you can use the weapon group instead of each single sprite-object and the events will work for all the members of this group.

1 Like

Like Drona have it on a screenshot and like i said

Event without condition
So change position action ALWAYS work

I have another question, what if I want to replace the gun whatever is in the “GunPoint”? because I have plans on adding multiple weapons.
How would I tell the game to delete whatever the player is holding and replace it?

Like for example, player is currently holding gun A and it collides with a gun create
I want the game to delete whatever it is holding and replaced with a new one.

There are different ways to do that and this question has been asked a few times before. Here is a thread that may be interesting for you:

The thread didn’t help on anything there as the thread starter’s problem was holding 2 weapons while I just want mine to delete whatever the player is holding and replaced with whatever weapon it collides with

I would simply make variable for player called WepType
And make it number

0 = NO gun
1 = revolver
2 = shotgun
3 = rifle

Now when player is in collision with revolver i would set his WepType variable to 1
And delete other 2 guns from scene create revolver and put it in guns spot of player

And repeat same process for 2 other weapons

For example player is in collision with rifle and have in hand revolver
Change player WepType variable to 3

If player WepType = 3
Delete revolver
Delete shotgun
Create rifle
Move rifle to Player.PointX(“GunPoint”) Player.PointY(“GunPoint”)

That is not perfect method but will do the trick

1 Like

Yes, but this doesn’t mean that you cannot use some of the logic in the posted events for your case. If you start to search a bit in the forum yourself, you will find even more possibilities.

Anyway, ZeroX4 made a good suggestion and I would go with this one.

Ok so I’ve tried this method and it works just fine thank you!
Now I’m just curious if instead of deleting the weapons one by one in the events, is there a better way of just getting rid of them in 1 action? because one way I’ve tried is this

once the player collides with a box it deletes the box then deletes gunGroup incase the player is holding any current weapon and then create object for it to get the new weapon.

1 Like

Damn you
That is pretty clever
I did not think of that
And i abuse groups in my projects left and right
If i understand correctly you are deleting each object from gun group so no matter which gun player is holding it will get deleted?

If so then this is kinda best way
I mean there are others but they would involve checking variables
Or making slot object and deleting anything from slot object
But your method is pretty much most simple i could think of

Again damn clever idea
You really should be proud of yourself