AI: How do I store multiple possible actions without producing spaghetti-code?

I wan’t to build an AI-Character with an vast array of possible actions that depend on different environmental conditions (distance to target, angle to target, target health,…). I wan’t my AI to check these conditions and then randomly select one action that fulfills all the condition. For example: Action A is valid while the target is between 100 and 500 units away and has a health below 60, Action B is valid for distances below 250,… So it’s basically tabular data - actionName | validDistance | validAngle | validHealth | …

Now I’m looking for an clear and easy to maintain/expand system on how to store these actions and search for the proper set without stringing a long line of conditions together. But I just can’t find the right data- and codestructure for this. How would you solve such a task? Could you point me in the right direction?

Hey, you could create a structure (New > Blueprint > Structure) that contains all the data you need to check to see if an action is valid to perform (so define just one generic structure that contains the data required to cover all actions (range, angle, etc.) and even if for example the action can’t be used at range just fill that particular variable of the structure with eg. 0).

Then create an array of that structure (the “actions array”) with one element for each possible action, and populate each element with the data required to define that action.

Then create a boolean array the same length as your actions array (the index of both the action array and the boolean array will refer to the same action (bool_array[0] will refer to actions_array[0]), and initialise all elements in the bool array to true (the elements in the bool array will be set false if an action fails a validity check).

Then in the code just run a loop on your actions array. First thing inside the loop save the current loop index (action array index). Then run your code checking each environmental condition (eg. range, angle, health etc.) against the data held in your actions array for the current index. If any of the checks calculate the action is not valid set bool_array[current_loop_index] = false, and immediately drop out of the current iteration of the loop.

Once you’ve looped for all the actions in your action array you should be left with the boolean array with elements true for each action that passed all tests, and false for all the other actions. To choose one of the valid actions at random, you could maybe create an int array that contains the index of all actions that passed, then run a get random number between 0 to int_array_len…

Thank you for you’re suggestion… Great tip to use a array of structures - I think I can work with this :slight_smile:

I use custom state machine components.