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
Line 
1#include "Script.h"
2
3
4Script::Script()
5{
6  returnCount = argumentCount = 0;
7
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}
25
26
27bool Script::loadFile(std::string& filename)
28 {
29
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
33   int error = luaL_loadfile (luaState, filename.c_str());
34
35   if(error == 0)
36   {
37     error = lua_pcall(luaState, 0, 0, 0);
38
39     if(error == 0)
40     {
41      currentFile = filename;
42      return true;
43     }
44     else
45     {
46      reportError(error);
47     }
48
49   }
50   else
51   {
52     reportError(error);
53   }
54
55   return false;
56 }
57
58 bool Script::executeFile()
59 {
60   if(currentFile.length() != 0)
61   {
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      }
70   }
71   return false;
72 }
73
74 bool Script::selectFunction(std::string& functionName, int retCount)
75 {
76   if(returnCount == 0) //no return values left on the stack
77   {
78   lua_pushlstring(luaState, functionName.c_str(), functionName.size() );
79   lua_gettable(luaState, LUA_GLOBALSINDEX);
80
81   if(lua_isfunction( luaState , -1))
82   {
83     returnCount = retCount;
84     argumentCount = 0;
85     currentFunction = functionName;
86     return true;
87   }
88   else
89    return false;
90   }
91   else
92     printf("there are unremoved return values on the stack. Please remove them first.\n");
93 }
94
95 //return number of returned values
96 bool Script::executeFunction()
97 {
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
109      argumentCount = 0;
110      return true;
111    }
112   }
113   else
114     printf("Error: no function selected.\n");
115 }
116
117
118 //overload this function to add different types
119 bool Script::pushParam(int param, std::string& toFunction)
120 {
121   if(currentFunction.compare(toFunction) == 0)
122   {
123     lua_pushnumber(luaState, (lua_Number) param);
124     argumentCount++;
125    return true;
126   }
127   else
128   {
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;
131   }
132
133 }
134
135
136 bool Script::pushParam(float param, std::string& toFunction)
137 {
138   if(currentFunction.compare(toFunction) == 0)
139   {
140     lua_pushnumber(luaState,(lua_Number) param);
141     argumentCount++;
142     return true;
143   }
144   else
145   {
146     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
147     return false;
148   }
149
150 }
151
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
168 int Script::getReturnedInt()
169 {
170   int returnValue;
171   if(returnCount > 0)
172   {
173     if(lua_isnumber(luaState, -1))
174     {
175       returnValue = (int)lua_tonumber(luaState, -1);
176       returnCount--;
177       lua_pop(luaState,1);
178     }
179   }
180   return returnValue;
181 }
182
183
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--;
193       lua_pop(luaState,1);
194     }
195   }
196   return returnValue;
197 }
198
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 }
213
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.