Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10046


Ignore:
Timestamp:
May 7, 2014, 2:03:18 PM (11 years ago)
Author:
smerkli
Message:

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.

Location:
code/branches/ScriptableController
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ScriptableController/data/gui/scripts/testscript.lua

    r10045 r10046  
    99
    1010local ctrl = orxonox.ScriptController:getScriptController()
    11 --orxonox.ScriptController:moveToPosition(x, y, z)
     11if ctrl ~= nil then
     12  ctrl:moveToPosition_beta(x, y, z)
     13end
     14
    1215--ctrl.moveToPosition(x,y,z)
     16--
     17if newctrlid ~= nil then
     18  orxonox.execute("orxout message test " .. newctrlid)
     19end
    1320
    1421local docks = orxonox.Dock:getNumberOfActiveDocks()
  • code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc

    r10035 r10046  
    11/*
    2 First try of a ControllerDirector. Target: An event occurs in the levelTry.oxw file, which is "heard" by an object of the type of this class. It then SHOULD (because it is not working) execute the party function.
     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.
    35 */
    46
     
    2022    ControllerDirector::ControllerDirector(Context* context) : ArtificialController(context)
    2123    {
    22         //Working
     24        // Register the object with the framework
    2325        RegisterObject(ControllerDirector);
     26
     27        // output a message to ensure we know the constructor was run
    2428        orxout()<<"hello universe constructor"<< endl;
    2529
    26            this->player_=NULL;
    27            this->entity_=NULL;
    28            this->pTrigger_=NULL;
     30        // Initialize member variables
     31        this->player_ = NULL;
     32        this->entity_ = NULL;
     33        this->pTrigger_ = NULL;
    2934    }
    30 
    3135
    3236    void ControllerDirector::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     
    3438        SUPER(ControllerDirector, XMLPort, xmlelement, mode);
    3539
    36        
    37         orxout()<<"ControllerDriector::XMLPort An instance of ControllerDirector has been created."<< endl;
     40        orxout()<< "ControllerDirector::XMLPort "
     41          << "An instance of ControllerDirector has been created." << endl;
    3842    }
    3943
    4044    void ControllerDirector::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
    4145    {
     46        // Call the xmleventport functions of the classes we derive from
    4247        SUPER(ControllerDirector, XMLEventPort, xmlelement, mode);
    4348
    44         XMLPortEventSink(ControllerDirector, BaseObject, "takeControl", takeControl, xmlelement, mode);
    45 
     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);
    4653    }
    4754
     
    4956
    5057
    51     void ControllerDirector::takeControl(Controller * controller, BaseObject * trigger) {
     58    void ControllerDirector::takeControl(Controller * controller, BaseObject * trigger)
     59    {
     60       /* Output a message confirming that the function was called */
     61       orxout()<<"test takecontrol."<< endl;
    5262
     63       /* First, we set up a new controller to attach to the unit that
     64        * triggered our event.
     65        */
     66       static int ctrlid = 0;
    5367       // preparationTo(trigger);
    5468       // setNewController(controller);
    55         LuaState * test = new LuaState();
    56         orxout()<<"test takecontrol."<< endl;
    57         test->doFile("testscript.lua");
    58        
     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;
    5989    }
    6090
  • code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc

    r10045 r10046  
    6767    ScriptController* ScriptController::getScriptController()
    6868    {
     69      /* Output a message that confirms this function was called */
    6970      orxout() << "Great success!" << std::endl;
     71
     72      /* Loop over all the scriptcontrollers currently present in the game */
    7073      for(ObjectList<ScriptController>::iterator it =
    7174        ObjectList<ScriptController>::begin();
     
    8386    void ScriptController::moveToPosition_beta(float x, float y, float z )
    8487    {
     88        /* The section commented out here below throws segfaults */
     89        //const Vector3 local=getPosition();
     90        //const Vector3 target=Vector3(x,y,z);
     91        //Vector3 way=target-local;
     92       
     93       
     94        //this->controlled->lookAt(target);
     95        //this->controlled->moveFrontBack(way.length());     
    8596
    86        const Vector3 local=getPosition();
    87        const Vector3 target=Vector3(x,y,z);
    88 
    89        Vector3 way=target-local;
    90 
    91        
    92        this->controlled->lookAt(target);
    93 
    94         this->controlled->moveFrontBack(way.length());     
    95 
    96 
    97         /*orxout()<<x<<"  "<<y<<"  "<<z<<endl;*/
    98 
    99 
    100 
    101          
     97 
     98        /* This works fine */
     99        orxout()<<x<<"  "<<y<<"  "<<z<<endl;
    102100    }
    103101
  • code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h

    r10045 r10046  
    5050           
    5151            void set_luasrc(std::string);
    52 
    5352            void set_controlled(ControllableEntity*);
    5453
Note: See TracChangeset for help on using the changeset viewer.