Allow only 1 projectile until destroyed

I have various projectiles on a MuliGate (toggled by Z) and a specific one should be allowed to exist only once, until destroyed.

How would you cap this?

Here’s a solution but it is probably overkill but its the first thing i came up with. You only show a small snippet of your script so i had to guess as some bits.

anyway as shown in the picture below when Z is pressed the ammo types selections are looped through are set based on the multi-gate.

Q was the button i used for the weapon firing (shoot). when Q is pressed we first check to see if the projectile type is a key in the active projectiles map, if the key isn’t found then it should be invalid. if the key exists then we check to see if the associated actor is valid, if it is valid then we know we shouldn’t be able to fire again, if not valid then we proceed. i should mention here that the map variable types are the projectile class and a actor reference of that class, also i used one BP actor for the base projectile and the different types were children of that base class. now back to the script, so if the reference is not valid then it doesnt exist in the world (was destroyed) and we will then remove the key from the map. we then spawn our projectile based on the projectile variable. the last step is to create a new key in the map and set its associated actor reference to the actor we just created.

of course you could accomplish all this with something like an array or a bunch of variables bit the map made for a simpler script that doesn’t need to be copied many times (allows for easy expansion)

not really sure what your meaning us to do with the first two parts of the comment. they dont conflict with what i proposed nor is there a question in that part so not sure on that.

as for the variables i used, the projectile variable is just a class reference to a base projectile class which other projectile types inherit from. you could use a broader class as well. basically i was going off your picture. as for the map “active Projectile” the stats are shown in the image, the key is a bp actor named bomb which is the base projectile class i was using (named it different since i have a few projectile names already) and the value associated if a actor reference to the same class. basically the key is the class and the value is a specific instance of the class. can’t really show them to you as i didn’t save the project.

the important thing here is the inheritance. if you have a common class that the others inherit from then you can create a variable and it will allow you to save references to child classes. its a bit tough to go into detailed explanation here but theres a ton of tutorials out there on inheritance.

Thank you, I have one problem which is that the variable logic is very different:
Earlier, I have followed these steps to toggle sticky/normal projectiles, where I have replaced the FlipFlop with a MultiGate:
https://answers.unrealengine.com/questions/912738/view.html

And I have applied this logic to destroy all projectiles, where I can destroy the projectile type that I have toggled (which is very neat)
https://answers.unrealengine.com/questions/911808/view.html

Also, I can’t guess which variables you have set in this. If you could show them to me. The current logic is a CurrentProjectile variable.

The two links are just optional trackbacks in case I should define the whole background. It’s good to know that there’s no need for them.

It is now a difficult challenge for me to reproduce your idea entirely from text. I’m 2 months into the Unreal Engine and I will try to analyze your text piece by piece, so I can hopefully visualize it in its theory. It is a hard mountain to climb, especially trying to understand the variables.

In your BP, I have problems connecting the purple pins to FIND, REMOVE, ADD and I’m especially confused about the Q Button, the variables called “Active Projectiles” and “Projectiles”. Most importantly, I want to destroy all projectiles with the middle mouse button and make it the universal trigger for DestroyProjectile. The only variable I have is Current Projectile, and I wonder what to do with the old system and where I should define the variables called “Active Projectiles” and “Projectiles”.

I did reproduce a good part of it, but the variables are empty and not defined since I do not know in what order I have to set them up.

I have problems connecting the purple pins to FIND, REMOVE, ADD

first thing to check is that the nodes your using are for maps and not arrays. the next thing to check is that the map key and the projectile variable are the same class.

I’m especially confused about the Q Button

the Q input is what i was using as the fire weapon button. your fire script isnt shown in the picture so i had to fill things in as i saw fit. basically i have Z toggle the projectile type then have Q to fire the weapon.

the variables called “Active Projectiles” and “Projectiles”

the variable projectile tracks which projectile type is currently equipped / active.

the ActiveProjectiles map variable just tracks if there is a instance of the ammo types in the level. according to your post “a specific one should be allowed to exist only once, until destroyed” i take that as one round the each type is allowed to exist in the level and the weapon should not be able to fire another round of any type that exists in the world currently. so if you fire a rocket then you cannot fire another rocket until the first is destroyed. to that end we make a note of the projectile type and the actor we spawned of that type, then when we want to fire that type again we get the note and we check to see if that actor still exists or not.