[Orxonox-commit 5383] r10046 - in code/branches/ScriptableController: data/gui/scripts src/orxonox/controllers
smerkli at orxonox.net
smerkli at orxonox.net
Wed May 7 14:03:19 CEST 2014
Author: smerkli
Date: 2014-05-07 14:03:18 +0200 (Wed, 07 May 2014)
New Revision: 10046
Modified:
code/branches/ScriptableController/data/gui/scripts/testscript.lua
code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc
code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc
code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h
Log:
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.
Modified: code/branches/ScriptableController/data/gui/scripts/testscript.lua
===================================================================
--- code/branches/ScriptableController/data/gui/scripts/testscript.lua 2014-05-06 14:02:07 UTC (rev 10045)
+++ code/branches/ScriptableController/data/gui/scripts/testscript.lua 2014-05-07 12:03:18 UTC (rev 10046)
@@ -8,8 +8,15 @@
local ctrl = orxonox.ScriptController:getScriptController()
---orxonox.ScriptController:moveToPosition(x, y, z)
+if ctrl ~= nil then
+ ctrl:moveToPosition_beta(x, y, z)
+end
+
--ctrl.moveToPosition(x,y,z)
+--
+if newctrlid ~= nil then
+ orxonox.execute("orxout message test " .. newctrlid)
+end
local docks = orxonox.Dock:getNumberOfActiveDocks()
local docklist = {}
Modified: code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc 2014-05-06 14:02:07 UTC (rev 10045)
+++ code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc 2014-05-07 12:03:18 UTC (rev 10046)
@@ -1,5 +1,7 @@
/*
-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.
+ * 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.
*/
#include "ControllerDirector.h"
@@ -19,43 +21,71 @@
ControllerDirector::ControllerDirector(Context* context) : ArtificialController(context)
{
- //Working
+ // Register the object with the framework
RegisterObject(ControllerDirector);
+
+ // output a message to ensure we know the constructor was run
orxout()<<"hello universe constructor"<< endl;
- this->player_=NULL;
- this->entity_=NULL;
- this->pTrigger_=NULL;
+ // Initialize member variables
+ this->player_ = NULL;
+ this->entity_ = NULL;
+ this->pTrigger_ = NULL;
}
-
void ControllerDirector::XMLPort(Element& xmlelement, XMLPort::Mode mode)
{
SUPER(ControllerDirector, XMLPort, xmlelement, mode);
-
- orxout()<<"ControllerDriector::XMLPort An instance of ControllerDirector has been created."<< endl;
+ orxout()<< "ControllerDirector::XMLPort "
+ << "An instance of ControllerDirector has been created." << endl;
}
void ControllerDirector::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
{
+ // Call the xmleventport functions of the classes we derive from
SUPER(ControllerDirector, XMLEventPort, xmlelement, mode);
- XMLPortEventSink(ControllerDirector, BaseObject, "takeControl", takeControl, xmlelement, mode);
-
+ // Add an event sink for a "takeControl" event, which leads to the
+ // function takeControl() being called.
+ XMLPortEventSink(ControllerDirector, BaseObject, "takeControl",
+ takeControl, xmlelement, mode);
}
- void ControllerDirector::takeControl(Controller * controller, BaseObject * trigger) {
+ void ControllerDirector::takeControl(Controller * controller, BaseObject * trigger)
+ {
+ /* Output a message confirming that the function was called */
+ orxout()<<"test takecontrol."<< endl;
+ /* First, we set up a new controller to attach to the unit that
+ * triggered our event.
+ */
+ static int ctrlid = 0;
// preparationTo(trigger);
// setNewController(controller);
- LuaState * test = new LuaState();
- orxout()<<"test takecontrol."<< endl;
- test->doFile("testscript.lua");
-
+
+ /* Set up a luastate to use for running the scripts */
+ LuaState * ls = new LuaState();
+
+ /* Assemble a string to define a controller id variable in lua space */
+ std::stringstream tmp;
+ tmp << "newctrlid = " << ctrlid;
+ std::string todo = tmp.str();
+
+ /* Run the string using the luastate created earlier */
+ ls->doString(todo);
+
+ /* Now run the script on this controller. This will still have the above
+ * variable "newctrlid" defined, which means it can make use of it.
+ */
+ ls->doFile("testscript.lua");
+
+ /* Increase the controller ID so we have a different one for
+ * the next time it is triggered */
+ ctrlid += 1;
}
Modified: code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc 2014-05-06 14:02:07 UTC (rev 10045)
+++ code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc 2014-05-07 12:03:18 UTC (rev 10046)
@@ -66,7 +66,10 @@
ScriptController* ScriptController::getScriptController()
{
+ /* Output a message that confirms this function was called */
orxout() << "Great success!" << std::endl;
+
+ /* Loop over all the scriptcontrollers currently present in the game */
for(ObjectList<ScriptController>::iterator it =
ObjectList<ScriptController>::begin();
it != ObjectList<ScriptController>::end(); ++it)
@@ -82,23 +85,18 @@
void ScriptController::moveToPosition_beta(float x, float y, float z )
{
-
- const Vector3 local=getPosition();
- const Vector3 target=Vector3(x,y,z);
-
- Vector3 way=target-local;
-
+ /* The section commented out here below throws segfaults */
+ //const Vector3 local=getPosition();
+ //const Vector3 target=Vector3(x,y,z);
+ //Vector3 way=target-local;
- this->controlled->lookAt(target);
+
+ //this->controlled->lookAt(target);
+ //this->controlled->moveFrontBack(way.length());
- this->controlled->moveFrontBack(way.length());
-
-
- /*orxout()<<x<<" "<<y<<" "<<z<<endl;*/
-
-
-
-
+
+ /* This works fine */
+ orxout()<<x<<" "<<y<<" "<<z<<endl;
}
/* TODO: hilfs(zwischen)funktionen um lua eingabe zu ermoeglichen: zb moveToPosition(float...) weil in LUA wohl
Modified: code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h 2014-05-06 14:02:07 UTC (rev 10045)
+++ code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h 2014-05-07 12:03:18 UTC (rev 10046)
@@ -49,7 +49,6 @@
void set_luasrc(std::string);
-
void set_controlled(ControllableEntity*);
More information about the Orxonox-commit
mailing list