The gun ammo goes back to max ammo when i drop the gun

i have a usp gun and it has 100 ammo and when i shoot it should be reduced to 100 and it is correct, but the problem is when i put that gun down, and i pick it up again it goes back to 100 ammo, the i wish if i shot 5 bullets, it’s still 95 even though i dropped it, same as counter strike 2d.

:point_down:
GIF_20240611_112711_919

Sounds like you aren’t changing the ammo settings when you pick up the gun. But it’s not possible to tell without a screen shot of the events.

Are you storing the ammo details on the gun object when you drop it, and changing the ammo settings when it’s picked up?

1 Like

I didn’t do any ammunition storage, these are the only events I have that have to do with guns and ammunition




Unfortunately you’ve made it quite difficult by using the bullet fire action to throw the uspD object. You can’t get a reference to the uspD object that’s just been fired, meaning you cannot save the ammo amount left in the uspD object.

I’d suggest that instead of firing it like a bullet, you create a new uspD object and add the ammo amount to it as an instance variable, before adding a force to it. You can keep the wait and other actions though.

Then when you pick up the uspD object, get the ammo amount from it.

1 Like

now my problem is that all uspD have force even if i put nearest, this is the reason why i used fire bullet in uspD

Why are you using the spawn function from an extension? Just use the GDevelop create object function, Then the force will only be applied to that one object.

Also, what is Pistols? Is Pistols an object group that has uspD in it? If so, why are you deleting it when you’re dropping the weapon?

2 Likes

I fixed it, thank you very much, but my real problem is how to fix the ammo amount

yes the pistols are group objects of usp or other pistols held by the player and i have another group made it is pistolsD that land on the ground like uspD, they are separated.

Oh, that’s quite straight forward:

First create an object variable named AmmoAmount on the upsD object (in the editor).

Then in the action you posted, after you create the upsD and before the wait, set an instance variable named AmmoAmount to the ammo amount.

When you collect the upsD, use the variable AmmoAmount to set the ammo amount for te weapon that was just picked up.

1 Like

I’m sorry but I don’t understand, and is the variable in uspD a string or a number or something?

It’s a number. In the scene editor, right click on the upsD object and select “Edit object variables”. Now add the variable to the object.

I don’t understand this, sorry

How are you keeping track of how much ammo a weapon has left when you fire bullets from it?

My usp gun have 100 ammo and 12 ammo per next reload

Ok, but how are you keeping this information? Using a variable, an extension, some other way?


I’m going on the assumption of the bullet extension, as you’ve used it in previous screen shots. Add the instance variable to the usp object in the scene editor:



When you drop the weapon, store the ammo quantity into the usp AmmoAmount variable:

image


And when you pick up the weapon, set the ammo quantity to the ups AmmoAmount variable:

image

1 Like

I don’t know why it doesn’t work, did I put the variable and events upside down, or is the problem the spawn extension, but when I use create object not the expansion, it creates an object continuously and gives lag to game, btw thank you for your example to make this

Heres my events:



In the pickup event, there’s no action to retrieve the ammo quantity from the usp and set it for the pistol.


How do you want the spawner to work? Should it create a new weapon once the currently spawned on has been collected? Because as you have it now, it looks like, over time, it it’ll spawn multiple weapons at the same spot. So when you collect one you’re actually collecting a number of them. You just can’t tell because they’re all at the same position with the same rotation

in the behavior only one spawns in the object spawner extension, that’s why I used him, not the create object which is not an extension, is that okay?

Ok, I wasn’t aware the spawner extension could be configured like that.

If, for example, it spawns a usp, you update the usp straight after and it only affects the one you spawned, then yes, it’ll be fine.