Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

space trigger and tick trigger are now scriptable

File size: 6.0 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
25/**
26 * Constructs a new ScriptTrigger.
27 * @param root the xml element to load the parameters from.
28 *
29 */
30ScriptTrigger::ScriptTrigger(const TiXmlElement* root)
31{
32  this->registerObject(this, ScriptTrigger::_objectList);
33  this->toList(OM_COMMON);
34
35  returnCount = 1;
36  scriptFinished = false;
37  doDebugDraw = false;
38
39  scriptCalled = false;
40  scriptIsOk = false;
41  executionStopped = false; // true when something goes wrong and the trigger has to be stopped
42  addToScript = false;
43
44
45  if(root != NULL)
46  {
47
48    loadParams(root);
49
50    if(addToScript && scriptIsOk)
51    {
52      script->addObject( "ScriptTrigger", this->getName());
53    }
54
55  }
56
57}
58
59/**
60 * Deletes the ScriptTrigger.
61 *
62 */
63ScriptTrigger::~ScriptTrigger()
64{
65
66}
67
68/**
69 * Reads the values from the tml element and sets them.
70 * @param root the xml element to load the parameters from.
71 *
72 */
73void ScriptTrigger::loadParams(const TiXmlElement* root)
74{
75
76  WorldEntity ::loadParams(root);
77
78  LoadParam(root, "file", this, ScriptTrigger, setScript)
79      .describe("the fileName of the script, that should be triggered by this script trigger")
80      .defaultValues("");
81  LoadParam(root, "function", this, ScriptTrigger, setFunction)
82      .describe("the function of the script, that should be triggered by this script trigger")
83      .defaultValues("");
84  LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor)
85      .describe("where this script trigger should be located")
86      .defaultValues("");
87  LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent)
88      .describe("The name of the parent as it is in the *.oxw file")
89      .defaultValues("");
90  LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw)
91      .describe("")
92      .defaultValues(false);
93  LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript)
94      .describe("True if this scripttrigger should be aviable in the script")
95      .defaultValues(false);
96}
97
98
99/**
100 * Sets the parent of the trigger.
101 * @param parent The parent.
102 */
103void ScriptTrigger::setTriggerParent(const std::string& parent)
104{
105  WorldEntity* parentEntity = WorldEntity::objectList().getObject(parent);
106
107  if (parentEntity != NULL)
108  {
109    this->setParent(parentEntity);
110    this->setParentMode(PNODE_MOVEMENT);
111  }
112  else
113  {
114    PRINTF(2)("ERROR SCRTIPTTRIGGER : Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassCName(), this->getCName());
115  }
116}
117
118
119void ScriptTrigger::executeAction(float timestep)
120{
121  if(executionStopped && scriptIsOk) // If the script has been loaded correctly but something is wrong with the settings of the trigger
122  {
123   PRINT(1)("ERROR SCRTIPTTRIGGER: Something went wrong while executing %s in %s . Execution stopped! \n", functionName.c_str(), script->getFileName().c_str());
124   return;
125  }
126
127  if(scriptIsOk)
128  {
129       //testScriptingFramework();
130    if(!(script->selectFunction(this->functionName,returnCount)) )
131     {
132      PRINT(1)("ERROR SCRTIPTTRIGGER : Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
133      executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again.
134      return;
135     }
136
137    if (! (script->pushParam( timestep, this->functionName)) )
138     {
139      executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again.
140      return;
141     }
142    if( !(script->executeFunction()) )
143     {
144      PRINT(1)("ERROR SCRTIPTTRIGGER : Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
145      executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again.
146      return;
147     }
148    scriptFinished = script->getReturnedBool();
149  }
150  else
151  printf("ERROR SCRTIPTTRIGGER : Script could not be executed !\n");
152
153
154}
155
156
157void ScriptTrigger::setScript(const std::string& file)
158{
159  ScriptManager* scriptManager = State::getScriptManager();
160  if (scriptManager != NULL)
161  {
162
163    script = scriptManager->getScriptByFile(file);
164    if(script != NULL)
165    {
166      scriptIsOk = true;
167    }
168    else
169    printf("ERROR SCRTIPTTRIGGER : Could not find the wrapperobject of %s , the script won't be executed ! \n", file.c_str());
170  }
171}
172
173/*
174 void ScriptTrigger::testScriptingFramework()
175{
176   std::string file("lunartest2.lua");
177   //get script
178   Script* script = State::getScriptManager()->getScriptByFile(file);
179   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
180
181      //execute a function
182   printf("----------- main -----------\n");
183   std::string main("main");
184   if( script->selectFunction(main,3))
185     printf("function %s selected\n",main.c_str());
186
187   script->pushParam(3.14159,main);
188   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
189   script->executeFunction();
190
191   int ret = script->getReturnedInt();
192   printf("main returned %i\n",ret);
193
194   if(script->getReturnedBool())
195     printf("main returned true\n");
196   else
197     printf("main returned false\n");
198
199   float retf = script->getReturnedFloat();
200   printf("main returned %f\n",retf);
201
202
203   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
204      //execute a 2nd function
205   printf("----------- test -----------\n");
206   std::string test("test");
207   if( script->selectFunction(test,0))
208     printf("function %s selected\n",test.c_str());
209
210   script->executeFunction();
211
212
213      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
214   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
215
216}*/
Note: See TracBrowser for help on using the repository browser.