Skip to main content
Version: bleeding-edge ๐Ÿฉธ

Artificial Intelligence

How AI works in nanos world

Every Character or CharacterSimple spawned without a Player possessing it will automatically be possessed by an AI Controller. Which means you can use Character methods such as :MoveTo() and :LookAt().

In nanos world, AI as well as Physics are things distributed through Clients to be calculated and shared with the other Players (please refer to Network Authority), which means the AI will only work if there is a Player connected to the server.

info

If you call :MoveTo() on a NPC and there is no Player connected, it will only start walking once a Player joins the server.

Moving NPCsโ€‹

The main AI methods are available in all Pawns (Character and CharacterSimple):

When a movement finishes (or fails, e.g. if there is no path), the event MoveComplete is triggered:

Server/Index.lua
-- Spawns a NPC (a Character not possessed by any Player)
local npc = Character(Vector(0, 0, 100), Rotator(), "nanos-world::SK_Male")

-- Makes it walk to a location, considering arrived when it's at 50 units from it
npc:MoveTo(Vector(1000, 0, 100), 50)

npc:Subscribe("MoveComplete", function(self, succeeded)
if (succeeded) then
Console.Log("The NPC arrived!")
else
Console.Log("The NPC couldn't find a path!")
end
end)

To find points where NPCs can walk to, the client side Navigation Static Class provides methods such as Navigation.GetRandomReachablePointInRadius() and Navigation.FindPathToLocation().

In order to NPCs to figure out the path to walk, the map will need to have a NavMesh configured.