Living City - Living NPC Crowds and Flocks

Search for Community packages for: Living City - Living NPC Crowds and Flocks

See working showcase with NPCs here : Crayta

Blueprint with cute voxelmesh ducks but no NPCs (courtesy of Andy Sergeant) search for :“LivingCrowdsAndFlocks - FlockingDucks”

[Editing in Progress] Screenshots and more examples to come

What is it?

This is a package that allows you to create realistic motion of entities and then in turn the NPCs that follow them. It is based on the principal of ‘boids’ (which we will call ‘flockers’)

WTF (What to Flock)?

It can be used to create anything from flocking birds, fish, spaceships, enemy armies (using NPC followers) etc.

Meet the flockers

It follows flocking principles of being attracted to its fellow flockers but also trying to avoid crashing into them (and other objects) along with trying to match its neighbours velocity.

Essentials to get it working:

Install the packages from the community

[The instructions below assume you are in advanced mode]

  • On the left hand side click on the Community Tab, Type in “Living NPC” (should be enough) and click Install under “Living City - Living NPC Crowds and Flocks” (author: IorekByrnison)

  • If you want to add Pathfinding (big more complicated) then repeat the above but type in “Pathfinding” and Install “Pathfinding” (author: AdamO_Crayta)

Add AdamO’s Pathfinding Package also from community packages (optional) if you want to use pathfinding for your flockers

Get started with the FlockerController Template

  • One of the items the package will have installed is the FlockControllerTemplate

  • In the left hand panel click the dropdown and select ‘Templates’ or click the ‘Templates’ icon (which looks like a tree structure)

  • Click on the FlockControllerTemplate template and drag it into the world

At this point you can try hitting preview and see what happens. You should see some jogger NPCs spawn from the Controller and a flag (‘food’ for the invisible flockers they are chasing) spawn from one the other locators. The NPCs should run towards the flag as a crowd and when ‘captured’ the flag will then disappear. A flag will appear at a new locator and they will then run towards that. This will continue until you get bored!

What is going on?

  • You should see a locator appear in the world and in the world tree on the right you will see the flockControllerTemplate1 locator appear as a child of ‘terrain’

  • If you expand the flockControllerTemplate1 locator in the world view you will see that underneath there is a script folder and a series of locators

Food Spawners

  • The locators are (by default) named flockerFoodSpawnLocator01’ etc.

  • These locators will spawn ‘food’ (a specific template) which your ‘flockers’ will be attracted to.

  • If you click on one of the locators you’ll see what makes it spawn food - it has a script attached to it called FlockInteractionScript

  • The scripts under the flockControllerTemplate will look for child entities or grandchild entities that have a FlockInteractionScript with the entityType set to foodspawn

  • When it finds these it will remember them and use them as possible locations to spawn new food

  • If there are no food spawn locators found then food will be spawned at the Controller location

  • Note: If you create a locator with foodspawn EntityType set in the FlockInteractionScript but it is not a child or grandchild of a Controller then it will not spawn food (e.g. in this case if you moved one of these foodspawners under ‘terrain’)

  • Also Note: The EntityTeam and EntityIdentifier properties are currently ignored for food but may be used in the future. debug property is to allow some verbose debugging to the console in cases of problems

Flock Controller Script Folder

  • If you click on the FlockControllerScriptFolder (script folder) you will see there are two scripts

  • The FlockInteractionScript is the same one at used with the food spawner but this time the entityType property is set to ‘controller’

  • The FlockControllerScript is used to control how items are spawned and which templates are used

  • By default the flockerTemplate property will be set to ‘FlockerTemplate’ which by default is a small invisible UFO mesh

  • The flockerTemplate will be spawned either:

  • At locators with the FlockInteractionScript script attached and EntityType editor property set to flockerspawn -or-

  • If there are no locators like the above then they will spawn at the FlockControllerTemplate locator (this is the case by default with this package)

  • The flockerNPCTemplates property is an array of templates

  • If the 'matching NPCPerFlocker property check box is set, one of these templates (chosen at random) will be be spawned at the same location as the flocer

  • Just after the flockerTemplate is spawned at the same location and the NPC will follow the flocker around

  • The flockerFoodTemplate will be used as the template to spawn food at the food spawners as described above

  • Note: The flockerTemplates will spawn facing in a random direction at the start. This is partly to help them disperse if many are spawned at the same time and place.

Scheduled spawning
  • You can set the flockers (we’ll assume there may be NPC followers but won’t mention it below) to spawn on a schedule if desired

  • The numTeams editor property determines how many ‘teams’ of flockers there are

  • The teamsSpawnTogether check box property determines whether teams of flockers are spawned at the same flockerspawn location if there is more than one

  • numFood property decides how many pieces of ‘food’ spawn at once (this can’t be more than the number of foodspawn points)

  • timing section of the properties should be pretty self explanatory but allows you to have a staggered timing between teams and individual flockers within teams spawning

What to try next?

Add new flocker spawn points

  • At this point all the flockers (and NPCs) are spawning on top of each other at the controller

  • To have them spawn in more than one spot and/or in a different spot from the controller you need to:

  • Create a locator which is a child or grandchild of the controller (right click in world tree on the flockControllerTempate1 → Create Child → Locator → New Locator)

  • The name of the locator doesn’t matter but you might want to call it something identifiable as a flockerspawn point

  • Attach the FlockInteractionScript to the locator (right click in world tree on your new locator → Add → Script → FlockInteractionScript)

  • Set the entityType editory property to flockerspawn

  • Move the locator away from the controller (and ensure it stays above ground!)

  • Note: The other editor properties such as entityTeam or entityIdentifier are not used for spawn points at the moment

Try hitting preview at this point. The same total number of NPCs should spawn but at the same location

  • Try duplicating the locator (right click and choose duplicate) and moving the duplicate to a different location

Hit preview again - there will still be the same number of NPCs but they will spawn from two different locations. As they start to find the flags they will form a crowd.

  • Go to the controller and try changing the schedule editor properties so that there is a minimum wait of 1 and a maximum wait of 5 between each flocker

  • minWaitBetweenFlockers: 1 , maxWaitBetweenFlockers: 5

This time when you hit preview the NPCs will gradually arrive on the scene and will take longer to form a crowd.

[To be furthered…]

1 Like

Edit: Update: Now the Build Jam code freeze is over I’ve included the changes below in the package.

Please note I accidentally typed 200300 (instead of deleting 200 and typing 300!) into the fixedZHeight property for the FlockerTemplate–> FlockerScriptFolder–> FlockerMainScript .

Please change this to a sensible value (e.g. the script default = 300) otherwise the flockers will not search for food as they will be too high up!


Too late for the deadline but I also noted this typo that may stop a flocker detecting its neighbours.

In FlockTriggerScript replace the line (occurs twice):

if (not entityType == "food" and not entityType == "npc") then

with

if (entityType ~= "food" and entityType ~= "npc") then :+1:

Finally - please also note that in the Blueprint the OfficeWorkers for Office01 are acting crazy because ‘scheduled spawning’ was accidentally ticked. Untick it for better results as shown below:

This doesn’t form part of my Build Jam entry (past the deadline) but do a search in Blueprints for “FlockingDucks” for a nice little example using Andy’s animated ducks.

2 Likes