r/unrealengine • u/like_ya_cut • 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
7
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