Skip to content

Expanding the action set

Yxuer edited this page Aug 22, 2019 · 2 revisions

The way the engine has been designed, expanding the action set should be relatively easy. Just take into account that the engine is written in C++, and as Bjarne Strostrup said, "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.". So, if your new action somehow includes dynamic memory, remember to free that memory in the destructor.

Anyways, in order to create a new action, the first thing to do is to modify the Action.cpp file. Search for this line:

std::set<std::string> Action::listOfActions = {"setState", "print", "endGame"};

and add the new action name (exactly as it will be on the XML) anywhere you want on the list. Then, find the factory method, whose signature is:

Action* Action::make_action(const std::string &actionName)

and add the constructor for your new action in a similar way as the already existing constructors. Please note that action constructors must have no parameters, since the action-specific parameters are added by the engine itself.

Once you have done this, create your class files for the action implementation (both the .h and the .cpp) in the same folder where the Action.h and Action.cpp. If your IDE does not automatically add these files to the CMakeLists.txt file, you will have to do it manually. To do so, just open the CMakeLists.txt file, find the add_executable area, and write the paths to your .h and .cpp files anywhere you want in that region. Apart from this, remember to add an include of your .h file to the Action.cpp file.

Once you have created the .h and .cpp files, your .h contents should look as it follows:

#ifndef COLINA_ENGINE_SETSTATE_H
#define COLINA_ENGINE_SETSTATE_H

#include "Action.h"

class MyActionClass : public Action {
    public:
        MyActionClass();
        virtual void run() override ;
};


#endif //COLINA_ENGINE_SETSTATE_H

As for the .cpp file, it should look as it follows:

#include "MyActionClass.h"
#include "../GameManager.h"

MyActionClass::MyActionClass() : Action(){
    /*For each parameter that your action will take, 
    * put that parameter in the list of parameters structure as it
    * follows. Note that the given name must be 
    * exactly the one to be specified in the XML*/
    listOfParameterNames.emplace_back("parameter1");
    listOfParameterNames.emplace_back("parameter2");
    /* ... */
    listOfParameterNames.emplace_back("parametern");
}

void MyActionClass::run() {
    /*The specific behavior of your action goes here*/
}

Information about the interface of each class for implementing behaviors can be accessed through the section Engine structure and behavior

After you have implemented everything, compile and test your new functionality!

Clone this wiki locally