[originally for the Living City Build Jam]
What lives in cities? Oh yeah, people. Well, also non-people. But this package is about the people.
Navigating the ever-shifting landscapes of the cities of tomorrow requires being aware of the context around you, being able to avoid any obstacles that come your way. This package lets the AI do just that.
It’s also easy to get overwhelmed by everything in the world. This system gives your AI an npcBrain to learn how to do just that! (note: you have to do all the teaching yourself)
To get started, look up Priority AI with Context Aware Movement in the community tab.
You’ll find the following :
We’ll take a tour by examining the exampleNPC template, who should look something like
First, we’ll click the base entity/npc (nPC6) to look at its delicious npcBrain
npcBrain
Pretty simple, yeah?
Every frame, it will go through the list of behaviors, starting with the first one, and attempt to see if that behavior can be activated. If so, none of the other entries in the list will fire.Of course, that’s not all the brain does, it also plugs right in with the default healthscript, monitoring your status. And, probably most importantly, it also monitors your senses.
Senses
The brain will always poll the senses to see what they know. Each script with Sense in its name (not a requirement, just a convention) is primarily concerned with finding a specific target to point our character towards. Each sense has a priority, and of all the senses that callback to the brain, it’ll pick the one with the lowest number (highest priority). It’ll also tell all the losers they weren’t picked, which is important.
Our character just has patrolSense, which tracks points to patrol between, and targetPlayerSense, which tries to find a player to pick as a target.
Because targetPlayerSense has a higher priority (lower number) than patrolSense, if it can identify a player to target, it will pick that as our target. Otherwise, our target will be wherever the patrolSense tells us to go. By the way, the Patrol Pointer is a necessary inclusion for that script, but it doesn’t really do anything of note but enable it to function.
Which target you give the brain determines the behavior of the… behaviors you have given the npc.
Behaviors
Each behavior must be a separate child locator of the entity that has the npcBrain, which should be your npc.
As already discussed, these will try to activate in order. In this case, our character will attack if there’s a target in range and the target is tagged as a player. Using the simpleTag script, it’s easy to compare, but case must match!
If there’s no target in range, or the target isn’t tagged as a player, then the Context Aware Movement script will activate and try to move to whichever target was deemed the highest priority from the senses.
Context Aware Movement
This script is commented with explanations throughout, as well as the tooltips, so please consult it directly to get a sense of what it does and how to customize it.
But really it’s pretty plug and play. Just choose which of the three movement modes the NPC will use, and whether or not they can jump. The other options provide tools for debugging and a use of tags to make it so our npc doesn’t treat enemies and players as if they were walls.
This script isn’t 100% perfect especially at speedmultipliers significantly different than 1 for the NPC, but it does try to compensate for that.
It shouldn’t be that hard to disconnect it from the npcBrain script if you have another way you want to feed it targets.
SimpleTag
Literally just tag it with strings, and there’s a function to compare if any of those strings are there.
I have a lot to say about this but I am posting this now so I can get a link and update the itch page and submit there so all I have left to do is type on this thanks bye