CUGL 1.3
Cornell University Game Library
Public Member Functions | Static Public Member Functions | List of all members
cugl::ai::BehaviorParser Class Reference

#include <CUBehaviorParser.h>

Public Member Functions

 BehaviorParser ()
 
 ~BehaviorParser ()
 
void dispose ()
 
bool init ()
 
void addPrioritizer (const std::string &name, std::function< float()> prioritizer)
 
void addPrioritizer (const char *name, std::function< float()> prioritizer)
 
std::function< float()> getPrioritizer (const std::string &name) const
 
std::function< float()> getPrioritizer (const char *name) const
 
std::function< float()> removePrioritizer (const std::string &name)
 
std::function< float()> removePrioritizer (const char *name)
 
void addAction (std::shared_ptr< BehaviorActionDef > actiondef)
 
void addAction (const std::string &name, std::shared_ptr< BehaviorActionDef > actiondef)
 
void addAction (const char *name, std::shared_ptr< BehaviorActionDef > actiondef)
 
std::shared_ptr< BehaviorActionDefgetAction (const std::string &name) const
 
std::shared_ptr< BehaviorActionDefgetAction (const char *name) const
 
std::shared_ptr< BehaviorActionDefremoveAction (const std::string &name)
 
std::shared_ptr< BehaviorActionDefremoveAction (const char *name)
 
std::unordered_map< std::string, std::shared_ptr< BehaviorNodeDef > > parseFile (const std::string &file)
 
std::unordered_map< std::string, std::shared_ptr< BehaviorNodeDef > > parseFile (const char *file)
 

Static Public Member Functions

static std::shared_ptr< BehaviorParseralloc ()
 

Detailed Description

A class designed to parse a JSON file describing a behavior tree.

An instance of this class is used to parse a JSON file into a collection of behavior trees. When finished, this class produces a mapping from the names of each behavior tree to the BehaviorNodeDef of that root of that tree.

The JSON file cannot contain the definitions of actions or priority functions, as these are function definitions. However, they can contain the names of these functions. When this parser is created, these functions can be added manually to the parser (before parsing) to support this feature.

When parsing a JSON file, each named, top-level object will be considered a BehaviorNodeDef. The node name is the name of the object, and is not an attribute of the JSON object. The supported attributes are:

type: A string representing the type of the node definition prioritizer: A string naming a priority function background: A boolean indicating whether this node processed in the background preemptive: A boolean indicating whether this node can be prempted uniform: A boolean indicating whether any random selection is uniform delay: A number, giving the time delay in seconds children: A list of named objects defining behavior nodes action: A string naming a possible action

With the exception of type, all attributes are optional and have default values.

Constructor & Destructor Documentation

◆ BehaviorParser()

cugl::ai::BehaviorParser::BehaviorParser ( )
inline

Creates an uninitialized parser.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.

◆ ~BehaviorParser()

cugl::ai::BehaviorParser::~BehaviorParser ( )
inline

Deletes this parser, disposing all resources.

Member Function Documentation

◆ addAction() [1/3]

void cugl::ai::BehaviorParser::addAction ( const char *  name,
std::shared_ptr< BehaviorActionDef actiondef 
)
inline

Adds an action definition for the given name.

This method will fail if the name is already in use.

Parameters
nameThe name to associate with the action definition
actiondefThe action definition

◆ addAction() [2/3]

void cugl::ai::BehaviorParser::addAction ( const std::string &  name,
std::shared_ptr< BehaviorActionDef actiondef 
)

Adds an action definition for the given name.

This method will fail if the name is already in use.

Parameters
nameThe name to associate with the action definition
actiondefThe action definition

◆ addAction() [3/3]

void cugl::ai::BehaviorParser::addAction ( std::shared_ptr< BehaviorActionDef actiondef)
inline

Adds an action definition to the parser.

This method assigned the action name to the one in the definition. This method will fail if the name is already in use.

Parameters
actiondefThe action definition

◆ addPrioritizer() [1/2]

void cugl::ai::BehaviorParser::addPrioritizer ( const char *  name,
std::function< float()>  prioritizer 
)
inline

Adds a prioritizer for the given name.

This function should return a value between 0 and 1 representing the priority of a node or action. This method will fail if the name is already in use.

Parameters
nameThe name to associate with the priority function
prioritizerThe priority function

◆ addPrioritizer() [2/2]

void cugl::ai::BehaviorParser::addPrioritizer ( const std::string &  name,
std::function< float()>  prioritizer 
)

Adds a prioritizer for the given name.

This function should return a value between 0 and 1 representing the priority of a node or action. This method will fail if the name is already in use.

Parameters
nameThe name to associate with the priority function
prioritizerThe priority function

◆ alloc()

static std::shared_ptr<BehaviorParser> cugl::ai::BehaviorParser::alloc ( )
inlinestatic

Returns a newly allocated behavior parser.

This parser starts with no predefined prioritizers or actions. They should be added (if needed) before parsing.

Returns
a newly allocated behavior parser.

◆ dispose()

void cugl::ai::BehaviorParser::dispose ( )

Disposes all of the resources used by this parser.

A disposed action can be safely reinitialized.

It is unsafe to call this on an action that is still currently inside of a running behavior tree.

◆ getAction() [1/2]

std::shared_ptr<BehaviorActionDef> cugl::ai::BehaviorParser::getAction ( const char *  name) const
inline

Returns the action definition for the given name.

This function returns nullptr if there is no definition for that name.

Parameters
nameThe name associated with the action definition
Returns
the action definition for the given name.

◆ getAction() [2/2]

std::shared_ptr<BehaviorActionDef> cugl::ai::BehaviorParser::getAction ( const std::string &  name) const

Returns the action definition for the given name.

This function returns nullptr if there is no definition for that name.

Parameters
nameThe name associated with the action definition
Returns
the action definition for the given name.

◆ getPrioritizer() [1/2]

std::function<float()> cugl::ai::BehaviorParser::getPrioritizer ( const char *  name) const
inline

Returns the prioritizer for the given name.

This function returns nullptr if there is no prioritizer for that name.

Parameters
nameThe name associated with the priority function
Returns
the prioritizer for the given name.

◆ getPrioritizer() [2/2]

std::function<float()> cugl::ai::BehaviorParser::getPrioritizer ( const std::string &  name) const

Returns the prioritizer for the given name.

This function returns nullptr if there is no prioritizer for that name.

Parameters
nameThe name associated with the priority function
Returns
the prioritizer for the given name.

◆ init()

bool cugl::ai::BehaviorParser::init ( )
inline

Initializes a behavior parser.

This parser starts with no predefined prioritizers or actions. They should be added (if needed) before parsing.

Returns
true if initialization was successful.

◆ parseFile() [1/2]

std::unordered_map<std::string, std::shared_ptr<BehaviorNodeDef> > cugl::ai::BehaviorParser::parseFile ( const char *  file)

Parses the JSON file provided to create behavior node definitions.

This method adds the BehaviorNodeDef objects to a map, which the user can extract using the name of the root as the key. Hence all root nodes should have unique names.

This function assumes that the file name is a relative path. It will search the application asset directory {

See also
Application::getAssetDirectory()} for the file and return false if it cannot find it there.
Parameters
fileThe relative path to the file.

◆ parseFile() [2/2]

std::unordered_map<std::string, std::shared_ptr<BehaviorNodeDef> > cugl::ai::BehaviorParser::parseFile ( const std::string &  file)
inline

Parses the JSON file provided to create behavior node definitions.

This method adds the BehaviorNodeDef objects to a map, which the user can extract using the name of the root as the key. Hence all root nodes should have unique names.

This function assumes that the file name is a relative path. It will search the application asset directory {

See also
Application::getAssetDirectory()} for the file and return false if it cannot find it there.
Parameters
fileThe relative path to the file.

◆ removeAction() [1/2]

std::shared_ptr<BehaviorActionDef> cugl::ai::BehaviorParser::removeAction ( const char *  name)
inline

Removes the action definition for the given name.

This function returns the definition removed. It returns nullptr if there is no definition for that name.

Parameters
nameThe name associated with the action definition
Returns
the definition removed.

◆ removeAction() [2/2]

std::shared_ptr<BehaviorActionDef> cugl::ai::BehaviorParser::removeAction ( const std::string &  name)

Removes the action definition for the given name.

This function returns the definition removed. It returns nullptr if there is no definition for that name.

Parameters
nameThe name associated with the action definition
Returns
the definition removed.

◆ removePrioritizer() [1/2]

std::function<float()> cugl::ai::BehaviorParser::removePrioritizer ( const char *  name)
inline

Removes the prioritizer for the given name.

This function returns the prioritizer removed. It returns nullptr if there is no prioritizer for that name.

Parameters
nameThe name associated with the priority function
Returns
the prioritizer removed.

◆ removePrioritizer() [2/2]

std::function<float()> cugl::ai::BehaviorParser::removePrioritizer ( const std::string &  name)

Removes the prioritizer for the given name.

This function returns the prioritizer removed. It returns nullptr if there is no prioritizer for that name.

Parameters
nameThe name associated with the priority function
Returns
the prioritizer removed.

The documentation for this class was generated from the following file: