Spawning Items

So I created a system that spawns a coin when I kill an enemy. I have 2 problems I can’t seem to overcome and I think I am overthinking it at this point.

  1. If i spawn multiple enemies only the first one will drop the coin.

  2. Is it possible to draw a random number (This part I know how to do) and use that number to generate how many coins are spawned.

1, seems weird, wouldnt you just spawn the coins on death? if so, then the number of enemies spawned shouldnt matter.

UNLESS, you are “spawning” them by placing them in the world, and using an exposed variable to set a value for the number of coins dropped.

2, Yes, there are random number nodes, clamp it to a certain range, feed it into a for loop, the for loop has a max loop count of (random number) each time the loop passes, it spawns another coin, until it hits the loop count.

For debug I am spawning them by clicking in the world. I will use traditional pawn spawning and see if that helps. That is an interesting idea, I didn’t think of.

As far as the loop for the coins, I will try and recreate what I did that didn’t work because it was the same idea. When I do that i will post an image of the BP structure. Usually when I run into these issues it is very simple mistakes that cause me big problems.

So I was spawning the enemies just by hitting space-bar originally…turns out that they would spawn a coin on death. The first coin would spawn at the site of the death. The subsequent coins spawn half way across the map…so I need to figure that out now…

Potentially you are spawning them after destroying the actor, in which case the coins would spawn at world origin.

or you store a variable for the location, and spawn using that position…

or you destroy the actor after the coins have all spawned, using the completed node on the for loop.

Have a look at the screen shots. I am spawning the coin(treasure) after death but I am using the location of the death.

The strange part is that the first enemy I spawn and kill it will work as intended, all additional spawns is what breaks it.

What i see is, you destroy the actor, and then spawn the coin.

The node you have pulled off for the transform is lost the moment you destroy the actor.

You could drag off the pin and “Promote to variable”, and then set the variable with the current location, in which case it would persist after “death” of the actor

Or even easier… put the destroy actor node last.

Also, in the screenshots, you only spawn one coin? (or does your other blueprint spawn multiple?)

YES this solves it. It was so simple but I get so wrapped up I miss these things…

Yes in the version I posted I am only spawning one coin. I didn’t want to create a dev glad for UI testing so I am using the BP that does not have the random value spawner.