NextBot NPC Creation
Nextbot is what the AI in Team Fortress 2 and Left 4 Dead use. This tutorial will go over the steps for a simple AI that will search for enemies (you) and chase them until they die or are too far away. It will also do some random other stuff when there are not any enemies.
Lets get started
First create the .lua file for your entity. The one I made for this tutorial is in "addons/Nextbot_tut/lua/entities" and is named "simple_nextbot.lua". Now open that file so you can start adding the code.
The code
The basic stuff we need for entities
Start off with defining the base entity to use and making it spawnable. Pretty much the same as any other entity so far. Here we set the model and define some variables we will use later.
Enemy related stuff
This adds some useful functions for enemy related stuff. An NPC/bot isn't complete if it can't target stuff, right? These include a function to check if there is still an enemy or if it got away and a function to search for enemies. I've added all sorts of comments so you know exactly what they do.
Coroutines
Now that the bot can find enemies we need to get it to actually do something other then just having an enemy. This next part is where most of our AI will be set up. The function is a coroutine, or pretty much a giant looping section of code, except you can pause it for a period of time using coroutine.wait( time ). Coroutines allow you to do things in a timed order, letting you pause the function so we can make the bot face the player or play an animation. And since its all inside a while true loop, it will run for as long as the bot exists. So after your ai has finished running everything it can do, it will go back and do it again. Here is an example of a very simple bot.
Pretty straight forward right? Walk to a random spot and sit idle for 2 seconds before walking to another random spot. As nice as this is its not what we want, up next is a much better AI.
The "brain" of our bot
As scary as this code may look to some, it is actually pretty simple:
- Check if we have an enemy, if not it will look for one using the above HaveEnemy() function.
- If there is an enemy then play some animations and run at the player.
- If there are not any enemies, then walk to a random spot.
- Stand idle for 2 seconds. Not that bad right? Have a look at the code, I've flooded it with comments so you should know what everything does
What is the ChaseEnemy() function?
After looking through the code you probably noticed a function that isn't anywhere in the wiki and not yet posted in this tutorial. ChaseEnemy() is what we will be looking at now, its pretty much identical to the MoveToPos function Garry made in the Nextbot base except for some useful changes:
- It builds a path to follow leading directly to the set enemy.
- Keep updating the path as the enemy and the bot moves around.
- Stop chasing the enemy if we don't have one any more. Using the HaveEnemy() function. This is what it looks like:
Have a look at Garry's function and then this one and see what was changed in more detail.
Almost done
Now that the bot is complete you will want to add it to the NPC spawn tab. Pretty easy, but here is the code used for this tutorial anyways:
When you use this just replace both "simple_nextbot" with the file name of the bot and the rest is pretty easy to figure out yourself.
Challenges
You now have a basic bot running around the map and that's pretty much it. Here are some things you can try on your own to spice it up:
- Search for more then just players
- Play sounds when its wandering around.
- Only search for enemies that are in front of it, not all around.
- Make it hide if the enemy is holding a shotgun.
- Stop chasing the enemy when it's really close and do a melee attack.
The full code
Useful Console Commands
Here are some useful console commands that can help you create and debug your NextBots.
Command | Description |
---|---|
nb_debug <values> | Allows you to debug various parts of nextbots. Values can be combined with a space. Possible values are: BEHAVIOR, LOOK_AT, PATH, ANIMATION, LOCOMOTION, VISION, HEARING, EVENTS, ERRORS |
Further Examples
Here are some further examples to guide you.