• This repository has been archived on 09/Sep/2018
  • Stars
    star
    367
  • Rank 115,536 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C# behaviour tree library with a fluent API

Fluent-Behaviour-Tree

C# behaviour tree library with a fluent API.

For a background and walk-through please see the accompanying article.

Understanding Behaviour Trees

Here are some resources to help you understand behaviour trees:

Installation

In the Visual Studio Package Manager Console:

PM> Install-Package FluentBehaviourTree

Or clone or download the code from the github repository.

Creating a Behaviour Tree

A behaviour tree is created through BehaviourTreeBuilder. The tree is returned when the Build function is called.

using FluentBehaviourTree;

...

IBehaviourTreeNode tree;

public void Startup()
{
	var builder = new BehaviourTreeBuilder();
	this.tree = builder
		.Sequence("my-sequence")
			.Do("action1",  t => 
			{
				// Action 1.
				return BehaviourTreeStatus.Success;
			})
			.Do("action2", t => 
			{
				// Action 2.
				return BehaviourTreeStatus.Success;
			})
		.End()
		.Build();
}

Tick the behaviour tree on each update of your game loop:

public void Update(float deltaTime)
{
	this.tree.Tick(new TimeData(deltaTime));
}

Behaviour Tree Status

Behaviour tree nodes return the following status codes:

  • Success: The node has finished what it was doing and succeeded.
  • Failure: The node has finished, but failed.
  • Running: The node is still working on something.

Node Types

Action / Leaf-Node

Call the Do function to create an action node at the leaves of the behavior tree.

.Do("do-something", t => 
{
	// ... do something ...
	// ... query the entity, query the environment then take some action ...
	return BehaviourTreeStatus.Success;
}); 

The return value defines the status of the node. Return Success, Failure or Running.

Sequence

Runs each child node in sequence. Fails for the first child node that fails. Moves to the next child when the current running child succeeds. Stays on the current child node while it returns running. Succeeds when all child nodes have succeeded.

.Sequence("my-sequence")
	.Do("action1", t => 
	{
		// Sequential action 1.
		return BehaviourTreeStatus.Success; // Run this.
	}) 
	.Do("action2", t => 
	{
		// Sequential action 2.
		return BehaviourTreeStatus.Success; // Then run this.
	})
.End()

Parallel

Runs all child nodes in parallel. Continues to run until a required number of child nodes have either failed or succeeded.

int numRequiredToFail = 2;
int numRequiredToSucceed = 2;

.Parallel("my-parallel", numRequiredToFail, numRequiredToSucceed)
	.Do("action1", t => 
	{
		// Parallel action 1.
		return BehaviourTreeStatus.Running;
	})
	.Do("action2", t => 
	{
		// Parallel action 2.
		return BehaviourTreeStatus.Running;
	})		
.End()

Selector

Runs child nodes in sequence until it finds one that succeeds. Succeeds when it finds the first child that succeeds. For child nodes that fail it moves forward to the next child node. While a child is running it stays on that child node without moving forward.

.Selector("my-selector")
	.Do("action1", t => 
	{
		// Action 1.
		return BehaviourTreeStatus.Failure; // Fail, move onto next child.
	}); 
	.Do("action2", t => 
	{
		// Action 2.
		return BehaviourTreeStatus.Success; // Success, stop here.
	})		
	.Do("action3", t => 
	{
		// Action 3.
		return BehaviourTreeStatus.Success; // Doesn't get this far. 
	})		
.End()

Condition

The condition function is syntactic sugar for the Do function. It allows return of a boolean value that is then converted to a success or failure. It is intended to be used with Selector.

.Selector("my-selector")
	.Condition("condition1", t => SomeBooleanCondition())	// Predicate that returns *true* or *false*. 
	.Do("action1", t => SomeAction())					// Action to run if the predicate evaluates to *true*. 
.End()

Inverter

Inverts the success or failure of the child node. Continues running while the child node is running.

.Inverter("inverter1")
	.Do("action1", t => BehaviourTreeStatus.Success) // *Success* will be inverted to *failure*.
.End() 


.Inverter("inverter1")
	.Do("action1", t => BehaviourTreeStatus.Failure) // *Failure* will be inverted to *success*.
.End() 

Nesting Behaviour Trees

Behaviour trees can be nested to any depth, for example:

.Selector("parent")
	.Sequence("child-1")
		...
		.Parallel("grand-child")
			...
		.End()
		...
	.End()
	.Sequence("child-2")
		...
	.End()
.End()

Splicing a Sub-tree

Separately created sub-trees can be spliced into parent trees. This makes it easy to build behaviour trees from reusable components.

private IBehaviourTreeNode CreateSubTree()
{
	var builder = new BehaviourTreeBuilder();
	return builder
		.Sequence("my-sub-tree")
			.Do("action1", t => 
			{
				// Action 1.
				return BehaviourTreeStatus.Success;
			})
			.Do("action2", t => 
			{
				// Action 2.
				return BehaviourTreeStatus.Success;
			}); 
		.End()
		.Build();
}

public void Startup()
{
	var builder = new BehaviourTreeBuilder();
	this.tree = builder
		.Sequence("my-parent-sequence")
			.Splice(CreateSubTree()) // Splice the child tree in.
			.Splice(CreateSubTree()) // Splice again.
		.End()
		.Build();
}

More Repositories

1

AngularJS-FlowChart

An example/template WebUI control for visualizing and editing flow charts
JavaScript
273
star
2

pnpm-workspace-examples

Example code showing how to create a multi-package mono repo using pnpm.
TypeScript
88
star
3

nodejs-microservices-example

Example of a monorepo with multiple Node.js microservices (using Docker, Docker-Compose and Kubernetes) that have separate CI/CD pipelines.
JavaScript
85
star
4

sql-to-mongodb

A Node.js script to convert an SQL table to a MongoDB database.
JavaScript
36
star
5

sharing-typescript-code-libraries

Examples of sharing TypeScript code libraries between microservices and between backend and frontend.
Dockerfile
34
star
6

alpha-vantage-cli

Command line tool and API for retrieving stock market data from Alpha Vantage
TypeScript
34
star
7

debugging-async-operations-in-nodejs

Example code to accompany my blog post on debugging async operations in Node.js.
JavaScript
26
star
8

fusion

A simple automated dependency injection library for TypeScript, supporting React class and functional components.
TypeScript
21
star
9

docker-nodejs-examples

A basic example of building a Node.js into a Docker container
Dockerfile
21
star
10

datakit

Simple JavaScript toolkit for data transform across JSON, CSV and YAML.
TypeScript
20
star
11

live-reload-examples

Examples of live reloading code to create a fast feedback loop.
JavaScript
20
star
12

promises-for-game-development

In this article we talk about our experience making the promises pattern work for game development. We aim to explain how promises can improve your game development process.
20
star
13

react-outliner

A React UI component for typing notes.
TypeScript
16
star
14

simple-fullstack-examples

Minimal examples of Node.js fullstack applications, showing that, at least in principal "fullstack" isn't that difficult.
JavaScript
13
star
15

typescript-template

A template for a TypeScript app. To make it easier to start a new app without having to go through all the configuration.
TypeScript
10
star
16

highstock-yahoo-demo

A demo of Highstock using data loaded from Yahoo
HTML
9
star
17

authentic

A microservice for authentication
TypeScript
8
star
18

javascript-testing-examples

A collection of examples of effective testing techniques for JavaScript.
JavaScript
8
star
19

chatbot-example

An example of how to create an Open AI Chatbot (like ChatGPT!)
TypeScript
8
star
20

kubernetes-log-aggregation-example

Shell
7
star
21

ts-project-bundle

A simple bundler for TypeScript projects using TypeScript project references.
TypeScript
7
star
22

rabbit-messaging-example

Examples of using Rabbit for messaging between Node.js microservices.
JavaScript
6
star
23

taskboard-vscode-extension

An extension to Visual Studio Code to render a Kanban board from task lists in markdown files.
TypeScript
6
star
24

eleventy-plugin-react-static

A plugin for Eleventy that renders React JSX and TSX files to static HTML.
JavaScript
5
star
25

AngularJS-MouseHandling

Implements mouse handling (mouse capture and dragging) for AngularJS.
JavaScript
5
star
26

internet-speed-test

Code for the Automated internet speed testing article on the The Data Wrangler
JavaScript
5
star
27

wunderlust-example

My version of the Open AI Wunderlust example.
TypeScript
5
star
28

AngularJS-Skeleton

Simplest possible AngularJS skeleton with jQuery 2.0.
HTML
4
star
29

electron-react-typescript-template

Minimal template for app build on Electron, React and TypeScript.
TypeScript
4
star
30

graphing-stock-data-with-highstock-and-data-forge

Shows how to visualize stock data with Highstock and Data-Forge.
JavaScript
4
star
31

figit

JavaScript and Handlebars data templates - Producing templated configuration has never been easier!
TypeScript
4
star
32

docker-compose-nodejs-with-typescript-example

An example of Nodejs and Mongodb servers built using Docker Compose with support for compiling TypeScript code.
TypeScript
4
star
33

react-parcel-typescript-example

The simplest possible example of a frontend built with TypeScript and React and bundled with Parcel.
HTML
3
star
34

monaco-editor-example

Simplest possible example of the Monaco editor.
HTML
3
star
35

angularjs-material-drag-and-drop-example

Example of drag and drop with Angularjs + Angular Material.
HTML
3
star
36

getting-started-with-microservices

Example code created during live coding at my talk "Getting Started with Microservices" at London Code Skill Buddy.
HCL
3
star
37

nodejs-docker-build-for-bitbucket-pipelines

Simplest example of a Node.js app with configuration for Bitbucket Pipelines that builds a Docker image and pushes it a private repository.
Dockerfile
3
star
38

nodejs-example

A simple Node.js example with Dockerfile and Kubernetes deployment configuration.
Shell
3
star
39

cronolog

Cron based task runner.
TypeScript
3
star
40

ig-cfd-cli

CLI tool and API for the IG CFDs API.
TypeScript
2
star
41

insta-mongo

Instantly start a MongoDB dev server and load database fixtures.
TypeScript
2
star
42

nodejs-chart-rendering-example

Example of command line chart rendering using Node.js and c3-chart-maker
JavaScript
2
star
43

file-transfer-example

An example of streaming file transfer via between Node.js applications.
JavaScript
2
star
44

c3-chart-maker

Generate a chart from a spreadsheet using a c3 spec.
JavaScript
2
star
45

react-typescript-docker-template

An example of using React with TypeScript, bundled using Docker / Docker-Compose.
TypeScript
2
star
46

open-telemetry-nodejs-microservices-example

An example of using open telemetry across Node.js microservices.
JavaScript
2
star
47

routey

Simple convention over configuration route setup for Express/NodeJS.
JavaScript
2
star
48

terraform-azure-example-for-bitbucket-pipelines

An example of infrastructure as code that provisions Azure infrastructure using Terraform automatically through Bitbucket Pipelines.
HCL
2
star
49

pdf-server-example

Example Node.js server that downloads a PDF file to a web app
JavaScript
1
star
50

minikube-playground

This repo contains a Vagrant script that boots a VM with Kubectrl and Minikube installed so that you can experiment with Kubernetes on a dev PC.
Shell
1
star
51

docker-compose-nodejs-example

An example of Nodejs and Mongodb servers built using Docker Compose
Shell
1
star
52

mongodb-simple-rest

A simple (to use and modify) MongoDB rest API.
JavaScript
1
star
53

apex-charts-example

An example of using Apex Charts
JavaScript
1
star
54

typescript-compilation-example

An example of compiling TypeScript code in-memory.
TypeScript
1
star
55

golden-layout-complex-angular-example

A more complex example of GoldenLayout with Angularjs.
JavaScript
1
star
56

electron-print-to-pdf-example

Example of using Electron to print a web page to a PDF.
JavaScript
1
star
57

mongodb-playground

A Vagrant virtual machine that installs MongoDB for testing and experimentation.
Shell
1
star
58

react-flow-chart

Example code demonstrating how to make a simple flow chart editor with SVG and React. This is a work in progress.
JavaScript
1
star
59

nightmare-example

An example of using the Nightmare library to automate the browser and take as screenshot.
JavaScript
1
star
60

db-fixture-rest-api

A REST API for loading and unloading MongoDB database fixtures.
JavaScript
1
star
61

maths-visualizer

Fun project to visualize mathematical transformations, built using TypeScript and React.
JavaScript
1
star
62

machine-learning-setup-2

A machine learning setup using Docker Compose.
Dockerfile
1
star
63

speech-to-text-example

An example of converting speech to text using the OpenAI API.
JavaScript
1
star