How to make AI throw up to 8 different projectiles

Hello, I have just been able to make an AI throw a projectile, but I think the method i used (making the throw a function that spawns projectile at a point in the animation) does not allow me to mess around with it much.

Is there a way i can make the projectile my AI spawns be a random projectile out of about 8, or even up to 25 if possible?

I really would be grateful if anyone could let me know how they would make this happen.

This is the tutorial I used to set up my throwing function UE4 Throwing Projectiles - YouTube

I do not know how you’re spawning the projectiles and I’m not going to watch a 40min tutorial to find out but it’s a matter of having classes or blueprints in an array and [selecting a random one][1], something like this:

There’s no upper limit on how many you can have, really. It all comes down to maintenance in the end. If you’re going this route, you will want to explore blueprint’s version of inheritance: [child classes][3]

Basically, you would design a parent projectile class and create children based on that, each with different set of rules. The core of OOP.

Oh ok, I’m spawning the projectile in an animation montage, so basically, the projectile blueprint will spawn at a point in a throw animation.

I don’t have much knowledge in arrays or child classes so will go read up.

Thank you so much for your time, I really appreciate it.

Will let you know if I make any progress.
Does clicking the up arrow near your comment mean you were helpful?

It means that you found it useful :slight_smile: - it essentially helps others find their answer quicker when they stumble upon your thread.

Ok good, I clicked it.

The classes approach may not be technically necessary if you’re planning something simple. I probably would not bother with multiple projectile classes if the only difference in the projectile was speed, damage and the visual effect.

But as soon as you move into something more serious, they become indispensable, providing flexibility; they help you organise your project more logically, and most importantly, allow to maintain much neater, modular code.

Have a look at this one for a 5min primer:

Well yes, their only differences would be mostly what you mentioned; speed, damage/collision, visual effect, direction…what would need to be different before I’ll need classes? And if I don’t need classes, should I just put the projectile blueprints in an array and place it in my animation montage?

Let’s say you want the projectiles to behave more uniquely. One of them hits the ground and causes area of effect damage, the other one can penetrate surfaces, the next one bounces off the ground and explodes mid-air with shrapnels - spawning another, brand new type of projectile!

Cramming it all it into a single blueprint is doable but that’s where the classes come in handy. You can encapsulate this behaviour and make it available only in the child actor. The parent does not need to know all the details.

The parent will know to how to fire, play sounds, detonate, deal damage - and all children will automatically inherit this behaviour; once you update the methods in the parent, you automatically update all children. On top of that, you can now make sure the children handle the details: which sound to play, how to detonate, what kind of damage to deal and so on…

If you plan on using just a single class, you can expose variables and feed the projectile a unique set of data that will define it:

221846-expose.png

Ideally, this should be done with a [struct][3], to simplify and automate the process. If you plan on having a lot of diversity, you could even consider a DataTable.

For something really simple, you can store the data sets inside the projectile itself.

Oh wow, that’s a lot to soak in, I don’t really know my way around classes, but if it’s what will get me the results you speak of, then I’ll hop right to learning more. Will try find some tutorials on it while making reference to this. Thank you so much for your time.

Oh, just wanted to know…I’ll understand if you are not up for it, is there a way I can speak to you? Like, over discord or skype…I really want to understand, instead of just copy pasting. And if so, do you charge for that kind of stuff?

But again, I will understand if you don’t want to, I’m trying to be a bit greedy, lol

Thirsty, perhaps. I’ll be quite happy to drop a suggestion here, one on one sessions are not really my thing.

Maybe you can try to create a Struct + DataTable approach for your projectile. I’ll try to chip in in case you get stuck:

Try to keep it simple, this can be always expanded later.

Lol, yeah probably thirsty. My issue is I’ve used actor blueprints and Projectile movement to do almost everything so far, I have no clue what to do with most of this. I can blindly copy but will definitely get stuck. Will try go study a bit more and get back to you. Else my ignorance might ■■■■ you off, lol. Thank u soo much btw

Like, do I make the struct then open it in the variable section in my projectile BP???

Once you’ve created the struct in the Content Browser, populate it with the variables you’d like to make your projectiles differ by, like in the image above.

Then create a DataTable, it will hold rows of data that define a projectile:

Choose previously created struct as a row template for the DataTable.

You will end up with a static database you can now populate manually:

Like, do I make the struct then open
it in the variable section in my
projectile BP???

Yes, add a struct variable to you projectile. It will hold the data we will randomly pull out of the DataTable.

Let me know if it makes sense.

At this point I’ve just been copying, so I’ve pretty much copied everything you have posted. I’ve made rows for the different projectiles, but all have no data in them, where and when do I input their differences?

Like so?

Depends on you’re going to utilise it later, the RowName can be either a number like in my example, or a name like in yours. The retrieval method will be somewhat different. Whichever you prefer will work. Add a couple of more projectile types so you have some data to work with.

Now in the projectile’s Construction Struct you can pull a row from the DT and set it as projectiles variable. Providing your DataTable rows are numbers, like in the example I mentioned above.

Whenever a projectile spawns it will be a random one. You can alter the chances of a certain projectile spawning later on. So, let’s say, every 10th-ish attack is more brutal, or does something different.

Basically I’m trying to get an AI to throw different kinds of projectiles in 3 directions. high, straight and low. Each projectile will deal a specific kind of damage.

Direction is something else altogether but can be easily incorporated here.

I do not know if your game is 2d/3d or 1st, 3rd person, top-down view or isometric. How your AI targets the player (or not all) and so on. I also do not know how you envisioned the system to work. Trying to help with the original question here :). Hard to advise on other things.

Once you have the basic spawning with some randomness working, you can start expanding it by adding the direction.

Generally, you’d get a forward vector of the character and send the projectile it that direction. Add an offset and it will go lower / higher - this offset can be a part of the data set so certain projectiles always go high while others always go low.

If I find some time later on in the day, I’ll try to peek at the tutorial to wrap my head around what we’re dealing with here. There are as many ways of achieving what you want as there are developers.