Spawn an item once from a random enemy in level

So what I want to achieve is at start pick a random enemy in the level and then once they are destroyed spawn a key that will unlock the door. I can’t seem to figure this out. I know how when an enemy dies to roll a number and if that number is the right number spawn the actor, but this will promote problems as in what if the player kills all the enemies but no key has dropped then the player wont be able to move on.

Thank you in advance for the help!

Hi ThatGuyMiniB, using your die roll idea might be a better option in this scenario, because you can modify the calculations to change the spawn chance based on how many enemies are remaining in the level. If you assign it to a random enemy at start, it could sometimes be the first enemy that the player encounters which could leave him with no motivation to kill any more enemies. On the other hand, with a die roll, you could check how many enemies are remaining and based on that start increasing the chance from 0% to 100%, once the player has killed more than certain number of enemies.

You could use ‘Get all actors of class’ to get a list of all enemies to store them in an array. Then every time an enemy dies, remove it from this array and check it’s length to see the number of enemies remaining. Accordingly, you can keep increasing the chance until it’s 100% for the last enemy.

That’s a good idea!

Note that you don’t actually need to store the enemies in an array. Updating some kind of global int value to keep track of their number would be enough.

As for the formula, you could use something like: chance = 1 / numberOfRemainingEnemies if you want a flat chance for each killed enemy, or you could combine that with a check comparing the number of remaining enemies is smaller than a certain cutoff.

So far this is what I have and I feel like I am doing it wrong becaues it is not working lol. This right now is in the level blueprint.

Well, you don’t need to use for loop to store the array. Get all actors of class itself outputs an array of actors of the specified class. So just take that output and set it as the Enemy Array. As erinacea mentioned, you do not have to necessarily store the enemies in an array unless you plan to use those references. In that case, just store the array length in a ‘Number of Enemies’ int variable.

As for checking the number of enemies, you’ll have to do it at ‘Event End Play’ of the enemy AI blueprints. So everytime one dies, you remove that enemy from the Enemy array, then check the length of the array and accordingly get your spawn chance to determine whether you want to spawn the key or not.

On a side note, try not to use ‘Event Tick’ unless you absolutely have no other better alternative. It is always better to use custom events or functions that are called when something happens in the game, as opposed to continuously ticking items like Event Tick, for performance reasons.

How would I go about doing that since I can’t reference an array that is in the level bp? Also how would I detect which of those gets killed and get their location after for if the random roll hits the number. Sorry I am just overall confused lol.

Ok so this is what I have right now but it seems that it is always equaling zero when an enemy is hit.

Alright, so now that’s one of the issues with putting your code in Level Blueprint. It’s kind of like Event Tick as in it’s something which people tend to avoid unless it’s the best option. So in this case, you can put the enemy reference array in some other class like your Game Mode or a custom actor blueprint that you place in the level to handle these situations.

As I mentioned in the second para in previous comment, you can use the End Play event in your enemy AI blueprints. This event gets called inside the blueprint of actors, whenever they’re destroyed. Say, Enemy 1 died. So it will call the End Play event in it’s blueprint to see if there’s anything in particular that it has to do before it is completely destroyed. So from here you can check with the array length of Enemy ref array in your Game mode. Based on that number and the corresponding random chance, you use the dying enemy actors’ transform to spawn the key at it’s location.