bw logo

Chapter 2. The MovementFactory

We will deal with the implementation of the MovementFactory first because you cannot test your MovementController without a working one, and it also illustrates the parameters that will be available to your MovementController when it is instantiated.

2.1. Constructor and global registration

The MovementFactory need not be declared in your MovementController's header file, as it will never be instantiated directly. Instead, you will declare a single static instance of the MovementFactory following its definition, whose constructor globally registers it as the factory for that controller type.

For example, in src/server/tools/bots/patrol_graph.cpp:

/* Source abbreviated for illustrative purposes */
namespace
{
    class PatrolFactory : public MovementFactory
    {
    public:
        PatrolFactory() : MovementFactory( "Patrol" ) 
        {
            /* any required init, usually unnecessary */
        }

        MovementController *create( const std::string & data, float & speed, Vector3 & position ) 
        { 
            /* parse data string and create concrete subclass of MovementController */
        }
    };

    PatrolFactory s_patrolFactory;
}

The constructor for PatrolFactory calls the constructor of MovementFactory to globally register the object as the factory for generating MovementController objects of type Patrol.

After the class definition, we declare a single instance of the factory class, which will associate itself with the name Patrol from that point on. It is useful to declare the MovementFactory class in an anonymous namespace (or declare it static) to avoid polluting the top-level namespace with the name of the declared instance.

2.2. The create() method

The first parameter passed to this method (const std::string & data) is the parameter passed to the setDefaultControllerData and updateMovement watchers when controlling bots. The create() method needs to parse this string, and then use the parsed data to create an instance of the movement controller.

Additionally, the method receives the speed and position of the bot, which is usually passed to the constructor of the movement controller.

Note

For more details, see the Server Operations Guide's chapter Stress Testing with Bots.