Download

Setting in arrays and Spawning different Children of the same Parent Class.

Good evening lads,
I’m making a Space-shooter type game for a university project, but I’ve run into some problems with my spawning of the enemies.
I have 3 child classes, named Enemy A, B and C, all children of the Abstract Class, Enemy BP.
I’m basically creating arrays that will resemble my enemy groups as they spawn, so I have set the arrays to be of the Enemy BP type, so I can set my different enemy spawns to be like Group A: ABCA, Group BCBA etc.

https://puu.sh/woAW4/efe31e2f79.png

Problem is, that my spawn isn’t working, I try to dynamically get the class of each enemy in the array, but that doesn’t work…

https://puu.sh/woBaU/74c945b1e3.png

If I set the Spawned Class to be a specific class, Enemy A,B or C, then it works, but only spawns one class type as is logical. (all of them spawn at the same time, mind you, but that’s a whole different issue)
I’ve tried printing the contents of the Enemy Group Arrays, but that’s coming up empty, so i think I’m doing something wrong when it comes to how I’m approaching the inheritance of the Enemy BP class.

https://puu.sh/woBr8/1235b4d7f8.png

So yeah, if you guys have any tips on what I’m doing wrong, please tell me I’m really interested in solving this issue, or cough cough my delay issue.
In any case, thanks alot for reading and if you having any tips whatsoever, even unrelated to this exact issue, please feel free to chime in.

This is weird. It works for me if my Array is of type “Enemy BP Class”. I can easily fill this array with subclasses and I can also spawn them.

As Raildex said, if you change your group arrays to be of type ‘Enemy BP Class’ and feed them your wanted classes, it will work fine. Youve already named the problem, why it doesnt work right now:

With that, no matter what child you assign to those variables, since the variables themselfs are all of type ‘Enemy BP’, only those will spawn.

Unfortunately, there are currently no out-of-the-box solutions for delays within loops. However, you can quickly set it up manually. It would be quite tedious to explain with words, but I can post a screenshot when I’m back home in ~ 90 min. The idea is the following: You create your own loop by counting up an index integer and check everytime if the array is valid with that index. if it is, we spawn the specified enemy, increment the index integer, execute the spawn delay and go back. if it is not valid, we have reached the end of the current array, reset the index integer and move on the next array or something else.

As I said, I’ll post a screenshot later to show what I mean, but I have you have any questions, feel free to ask!

Ah, i haven’t seen the pictures before.

I recommend to encouple your spawning logic into a separate BP. In this BP you have a Variable of “Array of Enemy BP Classes”.
This way you can easily store the Enemies to spawn in this BP. And do you spawn logic in a separate Event. When I’m at home I can show you how I mean it.

You need to change your array to “Array of Enemy BP Classes” and not “Array of Enemy BP References”.
Your Enemy Variables for the groups need to be class instead of a reference, too.
This way you can dynamically spawn them.

Allright, I’ve put some thought into this and this is what I came up with:

We’ve got two variables: EnemyGroups is an array of structs I made, containing the class of the enemy and its spawning location (from your picture your whole group spawns at the same location, I wasn’t sure, if that was intended). It looks like this:

GroupSpawningIndex is used in the spawning process. In a function I would’ve made this into a local variable, but unfortunately you can’t use delay in there. Instead of specifying a whole group to spawn, you give this event a number, representing the group you want to spawn (Group A is 1, Group B is 2 and so on). The spcified struct is then broken into the actual classes of the enemies and their respective spawn locations (can be upgrade to transforms, if you want them to look into different directions for example). Now we check, if the class at the current index is valid, if it is that means we haven’t reached the end of the group yet and spawn the enemy. After that, we wait for the specified delay, increment our index and repeat the whole process till we find the end of the group array. At this point, we reset our index integer and are done with the spawning.

I dont know how you set Spline and Loops, but hopefully this can help you :slight_smile:

Oh my god, guys, you are completely correct, I didn’t sit and think through how the spawn was working with how I set it up, of course I should create a class reference, not an actor reference.
Also, yeah it’s intended to spawn all the enemies in the same location, I’m using splines to create paths for the enemies and then on spawn I attach them to the spline in their own BPs and move them along it. I just use the same spawn point to spawn them off screen before they are attached to the spline.

So if I understand this correctly adlipFTW, because I increment the index, it instantly changes the values coming out of the class array and going into the SpawnActor call, and since we’re doing this, the branch gates the delay and makes it work?
tl;dr that fixes the delay issue?

In any case, thank you VERY much for this, it really helps me out, to get my project done as well as understand UE4 a bit better.

Yes and I just saw a typo, the ‘Group Number’ integer can and should start with 0 instead of 1.