So im trying to make an AI perception system. Its practically done but im afraid i coded it in the worst possible way so im asking if the code could be improved (it probably can which is why im here) the perception component is implemented to my liking and when the character sees the player, it starts an event timer loop that increases or decreases the float at a certain rate depending on how alert i want my ai to be (this is all done in the ai controller blueprint btw).
and i have more or less an event tick in my character blueprint which casts to its controller and takes the float value to determine the character’s state (hostile, suspicious,etc…) so far so good imo.
What i feel is a bit messy is the way the ai state is determined. The only solution i’ve found is having a whole bunch of branches that check if the awareness float is within certain ranges (if between 0 and 25 then patrol, between 25 and to 50 then suspicious, etc…) and if the condition isn’t met then the false node connects to the next branch in the chain.
When the condition is me it executes the corrosponding commands depending on the float value as explained. So TLDR is this a good way of testing if the float is between cerain ranges? It feels messy and might be a bit expensive so im looking for someone to clear that up for me and maybe provide a better solution.
I don’t have access to my pc so im sorry but i cant provide pictures. hope i explained it well.
Also side question is casting expensive? Since im using it with the equevilant of an event tick to get the float from the character’s controller (all the other ways i tried resulted in the characters acting like a hivemind, not good.). Thanks for everything in advance.
Have you had any exposure to Blackboard scripting? You can use it with behavior trees to determine how your AI will react, and respond to certain stimuli.
You can also customize your variable types using “keys”, to work with an Task or Environmental Query System. (I.E An environmental query that can find the nearest bp player actor, or a task that will execute when a certain condition is met, such as transitioning a behavioral state from one task to another.)
I’m going to provide a link to additional documentation on how Unreal uses behavior trees, and a quick start tutorial that may help give you a better idea on how to organize your ai behavioral states.
Link: How Unreal Uses Behavior Trees
Behavior Tree Quickstart Guide
Most of my Ai is done with the perception component, blackboards and behaviour trees. All of that is done and dusted. I tried alot of different ways to get the awareness state to change through a behaviour tree service, and it works until i put in two or more instances of the ai character. They start acting like a hivemind cuz they share the awareness float. And it’s not instance synced before you ask. Probably cuz the nodes are in a behaviour tree it somehow runs the node on all instances of the tree, same thing with the ai controller.
Look. This isnt just for Ai. I have an idea for a melee combo system using an integer variable to determine which attack the player should do. On input, i need the integer checked. It has 10 possible values and each value need to execute a different command. So the only way i can think of doing it is 10 branches with each false node connecting to the input of the next branch. The branches check the nber and if it is equal it executes the commands. Sounds good on paper and it should work (haven’t tried it yet) but seems messy and ineffiecient. All im asking is : is there a better way to do this? And what is it?
If you would like a more efficient way of organizing an integer check for a branch, you could utilize a switch on int. This will allow you to plug in your integer variables as input to check which combo we are on, and then “tailor” your individual outputs for each integer check.
In this example, I made an array of integers to represent the number combo we are on, we loop through each element in the array, and perform our switch statement execution on selection.