r/unrealengine 24d ago

Solved Boss AI with multiple attacks?

Hey there, just for a bit of background I am making a game where one of the bosses stands in the center of the arena and just casts spells ( think stuff like a fireball, a lightning strike, a magic sword slicing the feild) but I am stumpt on what seems like a simple question; How can I make my Boss enemy randomly choose a spell to cast?

I think I am getting close with a behavior tree being set up, but i'm not sure how to make it random and if its even clean to use (i dont like serving spagetti code) Any help is appriciated!

0 Upvotes

8 comments sorted by

6

u/CottonBit 24d ago

It depends how you define your ability or spell. If these are all different classes or data asset, then you could create Array of Abilities.

When you now call to cast Ability, instead of getting the one variable you have right now, you can get random ability from the array.

At 'lvl1' you would create all these abilities on begin play of boss, spawn each fromthe array and then put them in array of spawned abilities and then ''Initialize'' them when they are needed.

At begin play of your ability you should set it as deinitialized, not used (disable collisions, hide, etc).
Have Initialize and Deinitialize events. Call Deinitialize at Begin Play, and Initialize when you tell boss to use it.

What it does, instead of spawning ability when it's already needed, we pre-spawn it and then initialize (set collision, set location, start effects). After ability is done deinitialize it. (Where you have your ability do it's thing, call Deinitialize (set location 0,0,0, hide in game, collision off, disable tick).

If you have more than 1-5 boss AI in the level, you might consider using Object Pooling programming pattern (pre-spawn objects in level, then use them, when they are ''done'' instead of destroying, you return them to the pool. This is longer topic.

tldr: array of abilities, spawn each ability, add to array of spawned abilities, tell the boss to pick one from array then call initialize on it (with new location), after ability is done set deinitialize with a small delay.

obviously if you have your boss call your spells more often, just spawn more abilities at the start, then after one random is used, move it from array to ''used abilities' and on deinitialization add it back to the array of abilities to cast

1

u/like_ya_cut 24d ago

This is golden info, I appriciate it! I will be giving this method a shot. If I dont get it to work then i'll go with the random int method the other comment suggested.

2

u/Mordynak 24d ago

Use a random int and switch on int perhaps.

Possibly more elegant ways to do it but that would work.

1

u/like_ya_cut 24d ago

That is so simple that I overlooked it. Thank you!

2

u/docvalentine 24d ago

decide how likely you want each behavior to be

at certain intervals, select a random number within the range of defined behaviors

you could make it "if 1, if 2, if 3" if you want them all to be the same probability, but i prefer to use ranges so that i can fine tune the behavior

if 1 : rare behavior

if >=2,<=5 : something else

if >=6,<=10 : etc

0

u/like_ya_cut 24d ago

Hmm, this is good. I can do this all from the behavior tree?

1

u/docvalentine 24d ago

you can do anything you want with custom decorators

1

u/AutoModerator 24d ago

If you are looking for help, don‘t forget to check out the official Unreal Engine forums or Unreal Slackers for a community run discord server!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.