Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/script_triggers/script_trigger.cc @ 10626

Last change on this file since 10626 was 10622, checked in by snellen, 18 years ago

merged scriptimprovements back to trunk

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