Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/scriptimprovements/src/world_entities/script_triggers/script_trigger.cc @ 10609

Last change on this file since 10609 was 10609, checked in by snellen, 17 years ago

added tick trigger which always calles the script

File size: 7.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Silvan Nellen
13   co-programmer: ...
14*/
15
16
17#include "script_trigger.h"
18#include "script.h"
19
20#include "state.h"
21#include "debug.h"
22ObjectListDefinition(ScriptTrigger);
23
24// CREATE_SCRIPTABLE_CLASS(ScriptTrigger,
25//             // Coordinates
26//                      addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
27//                          ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
28//                          ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
29//                          ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
30//             //Properties
31//                          ->addMethod("setName", Executor1<BaseObject, lua_State*, const std::string&>(&BaseObject::setName))
32//                          ->addMethod("setTarget", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setTarget))
33//                          ->addMethod("setTriggerParent", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setTriggerParent))
34//                          ->addMethod("setTriggerRemains", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setTriggerRemains))
35//                          ->addMethod("setActiveOnCreation", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setActiveOnCreation))
36//                          ->addMethod("setInvert", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setInvert))
37//                          ->addMethod("setRadius", Executor1<ScriptTrigger, lua_State*, float>(&ScriptTrigger::setRadius))
38//                          ->addMethod("setScript", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setScript))
39//                          ->addMethod("setFunction", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setFunction))
40//                          ->addMethod("setDebugDraw", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setDebugDraw))
41//                          ->addMethod("setAddToScript", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setAddToScript))
42//              );
43
44
45/**
46 * Constructs a new ScriptTrigger.
47 * @param root the xml element to load the parameters from.
48 *
49 */
50ScriptTrigger::ScriptTrigger(const TiXmlElement* root)
51{
52  this->registerObject(this, ScriptTrigger::_objectList);
53  this->toList(OM_COMMON);
54
55  returnCount = 1;
56  scriptFinished = false;
57  doDebugDraw = false;
58
59  scriptCalled = false;
60  scriptIsOk = false;
61  executionStopped = false; // true when something goes wrong and the trigger has to be stopped
62  addToScript = false;
63
64
65  if(root != NULL)
66  {
67
68    loadParams(root);
69
70    if(addToScript && scriptIsOk)
71    {
72      script->addObject( "ScriptTrigger", this->getName());
73    }
74
75  }
76
77}
78
79/**
80 * Deletes the ScriptTrigger.
81 *
82 */
83ScriptTrigger::~ScriptTrigger()
84{
85
86}
87
88/**
89 * Reads the values from the tml element and sets them.
90 * @param root the xml element to load the parameters from.
91 *
92 */
93void ScriptTrigger::loadParams(const TiXmlElement* root)
94{
95
96  WorldEntity ::loadParams(root);
97
98  LoadParam(root, "file", this, ScriptTrigger, setScript)
99      .describe("the fileName of the script, that should be triggered by this script trigger")
100      .defaultValues("");
101  LoadParam(root, "function", this, ScriptTrigger, setFunction)
102      .describe("the function of the script, that should be triggered by this script trigger")
103      .defaultValues("");
104  LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor)
105      .describe("where this script trigger should be located")
106      .defaultValues("");
107  LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent)
108      .describe("The name of the parent as it is in the *.oxw file")
109      .defaultValues("");
110  LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw)
111      .describe("")
112      .defaultValues(false);
113  LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript)
114      .describe("True if this scripttrigger should be aviable in the script")
115      .defaultValues(false);
116}
117
118
119/**
120 * Sets the parent of the trigger.
121 * @param parent The parent.
122 */
123void ScriptTrigger::setTriggerParent(const std::string& parent)
124{
125  WorldEntity* parentEntity = WorldEntity::objectList().getObject(parent);
126
127  if (parentEntity != NULL)
128  {
129    this->setParent(parentEntity);
130    this->setParentMode(PNODE_MOVEMENT);
131  }
132  else
133  {
134    PRINTF(2)("ERROR SCRTIPTTRIGGER : Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassCName(), this->getCName());
135  }
136}
137
138void ScriptTrigger::tick(float timestep)
139{
140
141}
142
143
144void ScriptTrigger::executeAction(float timestep)
145{
146  if(executionStopped && scriptIsOk) // If the script has been loaded correctly but something is wrong with the settings of the trigger
147  {
148   PRINT(1)("ERROR SCRTIPTTRIGGER: Something went wrong while executing %s in %s . Execution stopped! \n", functionName.c_str(), script->getFileName().c_str());
149   return;
150  }
151
152  if(scriptIsOk)
153  {
154       //testScriptingFramework();
155    if(!(script->selectFunction(this->functionName,returnCount)) )
156     {
157      PRINT(1)("ERROR SCRTIPTTRIGGER : Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
158      executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again.
159      return;
160     }
161
162    if (! (script->pushParam( timestep, this->functionName)) )
163     {
164      executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again.
165      return;
166     }
167    if( !(script->executeFunction()) )
168     {
169      PRINT(1)("ERROR SCRTIPTTRIGGER : Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
170      executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again.
171      return;
172     }
173    scriptFinished = script->getReturnedBool();
174  }
175  else
176  printf("ERROR SCRTIPTTRIGGER : Script could not be executed !\n");
177
178
179}
180
181
182void ScriptTrigger::setScript(const std::string& file)
183{
184  ScriptManager* scriptManager = State::getScriptManager();
185  if (scriptManager != NULL)
186  {
187
188    script = scriptManager->getScriptByFile(file);
189    if(script != NULL)
190    {
191      scriptIsOk = true;
192    }
193    else
194    printf("ERROR SCRTIPTTRIGGER : Could not find the wrapperobject of %s , the script won't be executed ! \n", file.c_str());
195  }
196}
197
198/*
199 void ScriptTrigger::testScriptingFramework()
200{
201   std::string file("lunartest2.lua");
202   //get script
203   Script* script = State::getScriptManager()->getScriptByFile(file);
204   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
205
206      //execute a function
207   printf("----------- main -----------\n");
208   std::string main("main");
209   if( script->selectFunction(main,3))
210     printf("function %s selected\n",main.c_str());
211
212   script->pushParam(3.14159,main);
213   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
214   script->executeFunction();
215
216   int ret = script->getReturnedInt();
217   printf("main returned %i\n",ret);
218
219   if(script->getReturnedBool())
220     printf("main returned true\n");
221   else
222     printf("main returned false\n");
223
224   float retf = script->getReturnedFloat();
225   printf("main returned %f\n",retf);
226
227
228   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
229      //execute a 2nd function
230   printf("----------- test -----------\n");
231   std::string test("test");
232   if( script->selectFunction(test,0))
233     printf("function %s selected\n",test.c_str());
234
235   script->executeFunction();
236
237
238      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
239   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
240
241}*/
Note: See TracBrowser for help on using the repository browser.