Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/world_entities/script_trigger.cc @ 8296

Last change on this file since 8296 was 8289, checked in by snellen, 19 years ago

added some debug output, script gets called

File size: 5.8 KB
Line 
1//for testing
2#include "luaincl.h"
3#include "script_trigger.h"
4#include "class_list.h"
5#include "script.h"
6
7#include "state.h"
8 
9 
10 
11ScriptTrigger::ScriptTrigger(const TiXmlElement* root)
12{
13  this->setClassID(CL_SCRIPT_TRIGGER, "ScriptTrigger");
14  this->toList(OM_COMMON);
15 
16  scriptCalled = false;
17  scriptIsOk = false;
18  loadParams(root);
19
20}
21
22ScriptTrigger::~ScriptTrigger()
23{
24
25}
26
27void ScriptTrigger::loadParams(const TiXmlElement* root)
28{
29  if(root != NULL)
30  {
31    WorldEntity ::loadParams(root);
32
33    LoadParam(root, "file", this, ScriptTrigger, setScript)
34        .describe("the fileName of the script, that should be triggered by this script trigger")
35        .defaultValues("");
36    LoadParam(root, "function", this, ScriptTrigger, setFunction)
37        .describe("the function of the script, that should be triggered by this script trigger")
38        .defaultValues("");
39    LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor)
40        .describe("where this script trigger should be located")
41        .defaultValues("");
42    LoadParam(root, "radius", this, ScriptTrigger, setRadius)
43        .describe("the fileName of the script, that should be triggered by this script trigger")
44        .defaultValues(0);
45    LoadParam(root, "delay", this, ScriptTrigger, setDelay)
46        .describe("the delay after which the funtion sould be triggered")
47        .defaultValues(0);
48    LoadParam(root, "worldentity", this, ScriptTrigger, setTarget)
49        .describe("The name of the target as it is in the *.oxw file")
50        .defaultValues("");
51    LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent)
52        .describe("The name of the parent as it is in the *.oxw file")
53        .defaultValues("");
54    LoadParam(root, "callonce", this, ScriptTrigger, setCallOnce)
55        .describe("True if the script shoul only be called once")
56        .defaultValues("");
57  }
58
59}
60
61
62void ScriptTrigger::setTarget(const std::string& target)
63{
64  BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY);
65
66  if (targetEntity != NULL)
67  {
68    this->setTarget(dynamic_cast<WorldEntity*>(targetEntity));
69  }
70  else
71  {
72    PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName());
73  }
74}
75
76void ScriptTrigger::setTriggerParent(const std::string& parent)
77{
78  BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY);
79
80  if (parentEntity != NULL)
81  {
82    this->setParent(dynamic_cast<WorldEntity*>(parentEntity));
83    this->setParentMode(PNODE_MOVEMENT);
84  }
85  else
86  {
87    PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassName(), this->getName());
88  }
89}
90
91void ScriptTrigger::tick(float timestep)
92{
93 
94  if( this->distance(target) < radius)
95 {
96  if(!callOnce)
97   {
98    executeAction();
99   }
100  else if(callOnce && !scriptCalled)
101  {
102   executeAction();
103   scriptCalled = true;
104  }
105 }
106 //else
107   //printf("SCRIPTTRIGGER: target out of range\n");
108
109}
110
111
112void ScriptTrigger::executeAction()
113{
114     if(scriptIsOk)
115     {
116       testScriptingFramework();
117     /*if(!(script->selectFunction(this->functionName,0)) )
118       printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
119     if( !(script->executeFunction()) )
120       printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());*/
121     }
122}
123
124
125void ScriptTrigger::setScript(const std::string& file)
126{
127 
128  ScriptManager* scriptManager = State::getScriptManager();
129  if (scriptManager != NULL)
130  {
131   
132    script = scriptManager->getScriptByFile(file);
133    if(script != NULL)
134    {
135      scriptIsOk = true;
136    }
137  }
138}
139
140
141 void ScriptTrigger::testScriptingFramework()
142 {
143   std::string file("lunartest2.lua");
144     // Script script;
145   Script* script = State::getScriptManager()->getScriptByFile(file);
146   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
147
148      // Register classes with the script
149      // Lunar<Account>::Register(&script);
150      //Lunar<Object>::Register(&script);
151
152      //Object* obj= new Object();
153      //Account a;
154      //Account *b = new Account(30);
155
156      // Add instances to the script
157      //Lunar<Object>::insertObject(&script,obj,"Obj",true);
158      //Lunar<Account>::insertObject(&script,&a,"a",false);
159      //Lunar<Account>::insertObject(&script,b,"b",true);
160       //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));
161
162      //Load a file
163      //std::string file("lunartest2.lua");
164
165      //if(script.loadFile(file))
166        //printf("File %s succefully loaded\n", file.c_str());
167      //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));
168
169      //execute a function
170   printf("----------- main -----------\n");
171   std::string main("main");
172   if( script->selectFunction(main,3))
173     printf("function %s selected\n",main.c_str());
174
175   script->pushParam(3.14159,main);
176   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
177   script->executeFunction();
178
179   float retf = script->getReturnedFloat();
180   printf("main returned %f\n",retf);
181
182
183   if(script->getReturnedBool())
184     printf("main returned true\n");
185   else
186     printf("main returned false\n");
187
188   int ret = script->getReturnedInt();
189   printf("main returned %i\n",ret);
190
191   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
192      //execute a 2nd function
193   printf("----------- test -----------\n");
194   std::string test("test");
195   if( script->selectFunction(test,0))
196     printf("function %s selected\n",test.c_str());
197
198   script->executeFunction();
199
200
201      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
202   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
203
204 }
Note: See TracBrowser for help on using the repository browser.