Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc @ 10046

Last change on this file since 10046 was 10046, checked in by smerkli, 11 years ago

Fixed some more things. What works now:

  • Setting an ID for the scriptcontroller to be used from C++ in lua
  • Running a script from the controllerdirector triggered by an event
  • Getting an instance of a ScriptController object pointer from lua, and also calling a function of this object with parameters.

Next steps will be implementing different IDs per controller
object and actually swapping out controllers.

File size: 4.0 KB
RevLine 
[10016]1/*
[10046]2 * First try of a ControllerDirector. Target: An event occurs in the levelTry.oxw
3 * file, which is "heard" by an object of the type of this class. It then SHOULD
4 * (because it is not working) execute the party function.
[10016]5 */
6
[10024]7#include "ControllerDirector.h"
[10012]8#include "core/CoreIncludes.h"
9
[10024]10//#include "network/NetworkFunction.h"
11
12#include "infos/HumanPlayer.h"
13#include "interfaces/PlayerTrigger.h"
14#include "worldentities/pawns/Pawn.h"
[10035]15#include "core/LuaState.h"
[10024]16
[10026]17
[10016]18namespace orxonox
19{
[10024]20    RegisterClass(ControllerDirector);
[10012]21
[10024]22    ControllerDirector::ControllerDirector(Context* context) : ArtificialController(context)
[10016]23    {
[10046]24        // Register the object with the framework
[10024]25        RegisterObject(ControllerDirector);
[10046]26
27        // output a message to ensure we know the constructor was run
[10016]28        orxout()<<"hello universe constructor"<< endl;
[10012]29
[10046]30        // Initialize member variables
31        this->player_ = NULL;
32        this->entity_ = NULL;
33        this->pTrigger_ = NULL;
[10016]34    }
[10012]35
[10024]36    void ControllerDirector::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[10016]37    {
[10024]38        SUPER(ControllerDirector, XMLPort, xmlelement, mode);
[10012]39
[10046]40        orxout()<< "ControllerDirector::XMLPort " 
41          << "An instance of ControllerDirector has been created." << endl;
[10016]42    }
43
[10024]44    void ControllerDirector::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
[10012]45    {
[10046]46        // Call the xmleventport functions of the classes we derive from
[10024]47        SUPER(ControllerDirector, XMLEventPort, xmlelement, mode);
[10012]48
[10046]49        // Add an event sink for a "takeControl" event, which leads to the
50        // function takeControl() being called.
51        XMLPortEventSink(ControllerDirector, BaseObject, "takeControl", 
52          takeControl, xmlelement, mode);
[10024]53    }
54
55
56
57
[10046]58    void ControllerDirector::takeControl(Controller * controller, BaseObject * trigger) 
59    {
60       /* Output a message confirming that the function was called */
61       orxout()<<"test takecontrol."<< endl;
[10016]62
[10046]63       /* First, we set up a new controller to attach to the unit that
64        * triggered our event.
65        */
66       static int ctrlid = 0;
[10035]67       // preparationTo(trigger);
68       // setNewController(controller);
[10046]69       
70       /* Set up a luastate to use for running the scripts */
71       LuaState * ls = new LuaState();
72       
73       /* Assemble a string to define a controller id variable in lua space */
74       std::stringstream tmp;
75       tmp << "newctrlid = " << ctrlid;
76       std::string todo = tmp.str();
77
78       /* Run the string using the luastate created earlier */
79       ls->doString(todo);
80
81       /* Now run the script on this controller. This will still have the above
82        * variable "newctrlid" defined, which means it can make use of it.
83        */
84       ls->doFile("testscript.lua");
85
86       /* Increase the controller ID so we have a different one for
87        * the next time it is triggered */
88       ctrlid += 1;
[10024]89    } 
90
[10016]91       
[10035]92    /*bool ControllerDirector::preparationToTakeControl(BaseObject * trigger) {
[10016]93
[10024]94            this->pTrigger_ = orxonox_cast<PlayerTrigger*>(trigger);
[10016]95        this->player_ = NULL;
96
[10024]97        orxout() << "Preparation to take Control!" << endl;
[10016]98        // Check whether it is a player trigger and extract pawn from it
[10024]99        if(this->pTrigger_ != NULL)
[10016]100        {
[10024]101           
102            player_ = this->pTrigger_->getTriggeringPlayer();  //Get the object which triggered the event.
[10016]103        }
104        else
105        {
[10024]106            orxout() << "ControllerDirector::preparationToTakeControl Not a player trigger, can't extract pawn from it.." << endl;
[10016]107            return false;
108        }
109
110       
111        this->entity_ = this->player_->getControllableEntity();
112        assert(this->entity_);
113
[10024]114    return true;
115
[10016]116    }
117
[10024]118    void ControllerDirector::setNewController(Controller * controller) {
[10016]119
120
[10024]121        orxout() << "New Controller is going to be set!" << endl;
[10016]122
[10024]123        this->entity_->setDestroyWhenPlayerLeft(false);
124        this->player_->pauseControl();
125        this->entity_->setController(controller);
126        this->setControllableEntity(this->entity_);
127
[10026]128
129
[10024]130    }
[10035]131*/
[10016]132       
[10012]133   
134
[10024]135}
[10016]136
137
138
139
Note: See TracBrowser for help on using the repository browser.