Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/Script.cc @ 8072

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

multiple return values from lua functions are now supported

File size: 4.4 KB
RevLine 
[7975]1#include "Script.h"
2
3
4Script::Script()
5{
[8044]6  returnCount = argumentCount = 0;
7
[7975]8  luaState = lua_open();
9
10  luaopen_base(luaState);
11  luaopen_table(luaState);
12  luaopen_io(luaState);
13  luaopen_string(luaState);
14  luaopen_math(luaState);
15  luaopen_debug(luaState);
16
17}
18
19
20Script::~Script()
21{
22  lua_setgcthreshold(luaState, 0);  // collected garbage
23  lua_close(luaState);
24}
[8032]25
26
27bool Script::loadFile(std::string& filename)
28 {
[8044]29
[8045]30   if(currentFile.length() != 0)
31     printf("Could not load %s because an other file is already loaded: %s\n",filename.c_str(), currentFile.c_str());
32
[8032]33   int error = luaL_loadfile (luaState, filename.c_str());
34
35   if(error == 0)
36   {
[8044]37     error = lua_pcall(luaState, 0, 0, 0);
38
39     if(error == 0)
[8045]40     {
41      currentFile = filename;
[8044]42      return true;
[8045]43     }
[8044]44     else
45     {
46      reportError(error);
47     }
[8045]48
[8032]49   }
50   else
51   {
[8044]52     reportError(error);
[8032]53   }
54
[8044]55   return false;
[8032]56 }
57
[8044]58 bool Script::executeFile()
59 {
[8045]60   if(currentFile.length() != 0)
[8044]61   {
[8045]62    int error = lua_pcall(luaState, 0, 0, 0);
63    if( error == 0)
64      return true;
65     else
66      {
67       reportError(error);
68       return false;
69      }
[8044]70   }
[8045]71   return false;
[8044]72 }
[8032]73
[8045]74 bool Script::selectFunction(std::string& functionName, int retCount)
[8032]75 {
[8066]76   if(returnCount == 0) //no return values left on the stack
77   {
[8058]78   lua_pushlstring(luaState, functionName.c_str(), functionName.size() );
[8032]79   lua_gettable(luaState, LUA_GLOBALSINDEX);
[8045]80
[8058]81   if(lua_isfunction( luaState , -1))
[8045]82   {
[8058]83     returnCount = retCount;
84     argumentCount = 0;
85     currentFunction = functionName;
[8045]86     return true;
87   }
88   else
[8058]89    return false;
[8066]90   }
91   else
92     printf("there are unremoved return values on the stack. Please remove them first.\n");
[8032]93 }
94
95 //return number of returned values
[8045]96 bool Script::executeFunction()
[8032]97 {
[8045]98   if(currentFunction.length() != 0 )
99   {
100    int error = lua_pcall(luaState, argumentCount, returnCount,0);
101    if(error != 0)
102    {
103     reportError(error);
104     return false;
105    }
106    else
107    {
108      currentFunction.assign("");//a function gets unusable after beeing called for the first time
[8066]109      argumentCount = 0;
[8045]110      return true;
111    }
112   }
113   else
114     printf("Error: no function selected.\n");
[8032]115 }
116
117
118 //overload this function to add different types
119 bool Script::pushParam(int param, std::string& toFunction)
120 {
[8044]121   if(currentFunction.compare(toFunction) == 0)
[8032]122   {
[8044]123     lua_pushnumber(luaState, (lua_Number) param);
124     argumentCount++;
[8032]125    return true;
126   }
127   else
128   {
[8044]129    printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
130    return false;
[8032]131   }
132
133 }
134
135
136 bool Script::pushParam(float param, std::string& toFunction)
137 {
[8044]138   if(currentFunction.compare(toFunction) == 0)
[8032]139   {
[8044]140     lua_pushnumber(luaState,(lua_Number) param);
141     argumentCount++;
[8032]142     return true;
143   }
144   else
145   {
[8044]146     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
[8032]147     return false;
148   }
149
150 }
[8044]151
[8045]152 bool Script::pushParam(double param, std::string& toFunction)
153 {
154   if(currentFunction.compare(toFunction) == 0)
155   {
156     lua_pushnumber(luaState,(lua_Number) param);
157     argumentCount++;
158     return true;
159   }
160   else
161   {
162     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
163     return false;
164   }
165
166 }
167
[8058]168 int Script::getReturnedInt()
169 {
[8066]170   int returnValue;
[8058]171   if(returnCount > 0)
172   {
[8066]173     if(lua_isnumber(luaState, -1))
[8058]174     {
[8066]175       returnValue = (int)lua_tonumber(luaState, -1);
[8058]176       returnCount--;
[8072]177       lua_pop(luaState,1);
[8058]178     }
179   }
[8066]180   return returnValue;
[8058]181 }
182
183
[8066]184 bool Script::getReturnedBool()
185 {
186   bool returnValue;
187   if(returnCount > 0)
188   {
189     if(lua_isboolean(luaState, -1))
190     {
191       returnValue = (bool)lua_toboolean(luaState, -1);
192       returnCount--;
[8072]193       lua_pop(luaState,1);
[8066]194     }
195   }
196   return returnValue;
197 }
198
[8072]199float Script::getReturnedFloat()
200 {
201   float returnValue;
202   if(returnCount > 0)
203   {
204     if(lua_isnumber(luaState, -1))
205     {
206       returnValue = (float)lua_tonumber(luaState, -1);
207       returnCount--;
208       lua_pop(luaState,1);
209     }
210   }
211   return returnValue;
212 }
[8066]213
[8044]214 int Script::reportError(int error)
215 {
216 const char *msg = lua_tostring(luaState, -1);
217 if (msg == NULL) msg = "(error with no message)";
218 fprintf(stderr, "ERROR: %s\n", msg);
219 lua_pop(luaState, 1);
220 return error;
221 }
222
Note: See TracBrowser for help on using the repository browser.