Changeset 7656 in orxonox.OLD for branches/script_engine/src
- Timestamp:
- May 17, 2006, 8:36:25 PM (19 years ago)
- Location:
- branches/script_engine/src/lib/script_engine
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/script_engine/src/lib/script_engine/main.cc
r7653 r7656 38 38 //if() TODO: check if strObjName isn't "this" 39 39 if (virtualMachine.isOk ()) 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 40 { 41 /*create object table*/ 42 // Create a reference to the "object" table and set a name for the reference. Each reference is unique 43 44 lua_newtable (state); 45 int objRef = luaL_ref (state, LUA_REGISTRYINDEX); 46 lua_rawgeti (state, LUA_REGISTRYINDEX, objRef); 47 lua_setglobal (state, strObjName.c_str()); 48 49 50 // Save the "object" table to index 0 of the "object" table 51 LuaRestoreStack rs(virtualMachine); 52 lua_rawgeti (state, LUA_REGISTRYINDEX, objRef); 53 lua_pushlightuserdata (state, scriptable); 54 lua_rawseti (state, -2, 0); 55 56 57 58 return objRef; 59 } 60 60 61 61 } … … 76 76 77 77 int addFunctionToScriptable(int scriptableRef, const std::string& strFuncName, LuaScript& luaScript) 78 { 79 lua_State *state = (lua_State *) (luaScript.getVirtualMachine()); 80 LuaVirtualMachine& vm = luaScript.getVirtualMachine(); 81 int iMethodIdx = -1; 82 83 if (vm.isOk ()) 78 { 79 lua_State *state = (lua_State *) (luaScript.getVirtualMachine()); 80 LuaVirtualMachine& vm = luaScript.getVirtualMachine(); 81 int iMethodIdx = -1; 82 83 if (vm.isOk ()) 84 { 85 /* Add function */ 86 LuaRestoreStack rs (vm); 87 iMethodIdx = 0; 88 lua_rawgeti (state, LUA_REGISTRYINDEX, scriptableRef); 89 lua_pushstring (state, strFuncName.c_str()); 90 lua_pushnumber (state, (lua_Number) iMethodIdx); 91 lua_pushcclosure (state, luaCallback, 1); 92 lua_settable (state, -3); 93 } 94 95 return iMethodIdx; 96 } 97 98 class objectOne : public Scriptable 99 { 100 public: 101 objectOne () : Scriptable () 102 { 103 } 104 105 int scriptCalling (LuaVirtualMachine& vm, std::string functionName) 106 { 107 //switch (iFunctionNumber) //- methodBase) uncomment this when methodBase gets correclty set in the Scriptable class 108 if(functionName.compare("printNameOne")==0) 84 109 { 85 /* Add function */ 86 LuaRestoreStack rs (vm); 87 iMethodIdx = 0; 88 lua_rawgeti (state, LUA_REGISTRYINDEX, scriptableRef); 89 lua_pushstring (state, strFuncName.c_str()); 90 lua_pushnumber (state, (lua_Number) iMethodIdx); 91 lua_pushcclosure (state, luaCallback, 1); 92 lua_settable (state, -3); 110 //case 0: 111 return printNameOne (vm); 93 112 } 94 113 95 return iMethodIdx; 96 } 97 98 class objectOne : public Scriptable 99 { 100 public: 101 objectOne () : Scriptable () 114 return 0; 115 } 116 117 118 int printNameOne(LuaVirtualMachine& vm) 119 { 120 std::cout<<"Hi I'm Object 1 !"<<std::endl; 121 return 0; 122 } 123 124 void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc) 125 { 126 } 127 128 } 129 ; 130 131 class objectTwo : public Scriptable 132 { 133 public: 134 objectTwo () : Scriptable () 135 { 136 registerFunction(std::string("printNameTwo")); 137 // registerFunction(std::string("printHello")); 138 139 } 140 141 int scriptCalling (LuaVirtualMachine& vm, std::string functionName) 142 { 143 //switch (iFunctionNumber) //- m_iMethodBase) //TODO:find a way to determine the function number somehow: this works just incidentally 144 if(functionName.compare("printNameTwo")==0) 102 145 { 103 146 //case 0: 147 148 return printNameTwo (vm); 104 149 } 105 150 106 int scriptCalling (LuaVirtualMachine& vm, std::string functionName) 151 return 0; 152 } 153 154 155 int printNameTwo(LuaVirtualMachine& vm) 156 { 157 std::cout<<"Hi I'm Object 2 !\n"<<std::endl; 158 return 0; 159 } 160 161 int printHello(LuaVirtualMachine& vm) 162 { 163 std::cout<<"Nice to meet you!\n"<<std::endl; 164 return 0; 165 } 166 167 void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc) 168 { 169 } 170 171 } 172 ; 173 174 175 176 177 class CMyScript : public LuaScript 178 { 179 public: 180 CMyScript () : LuaScript () 181 { 182 // m_iMethodBase = registerFunction ("hello1"); 183 //registerFunction ("hello2"); 184 //registerFunction ("hello3"); 185 } 186 187 int scriptCalling (LuaVirtualMachine& vm, int iFunctionNumber) 188 { 189 /*switch (iFunctionNumber - m_iMethodBase) 107 190 { 108 //switch (iFunctionNumber) //- methodBase) uncomment this when methodBase gets correclty set in the Scriptable class 109 if(functionName.compare("printNameOne")==0) 110 { 111 //case 0: 112 return printNameOne (vm); 113 } 114 115 return 0; 191 case 0: 192 return Hello1 (vm); 193 case 1: 194 return Hello2 (vm); 195 case 2: 196 return Hello3 (vm); 116 197 } 117 198 118 119 int printNameOne(LuaVirtualMachine& vm) 199 return 0;*/ 200 } 201 202 int Hello1 (LuaVirtualMachine& vm) 203 { 204 printf ("Hellow (1)\n"); 205 return 0; 206 } 207 208 int Hello2 (LuaVirtualMachine& vm) 209 { 210 lua_State *state = (lua_State *) vm; 211 212 int iNumber = (int) lua_tonumber (state, -2); 213 int iNumber2 = (int) lua_tonumber (state, -1); 214 printf ("Hellow (2) -> %d\n", iNumber); 215 printf ("Second argument was %d\n",iNumber2); 216 return 0; 217 } 218 219 int Hello3 (LuaVirtualMachine& vm) 220 { 221 lua_State *state = (lua_State *) vm; 222 223 int iNumber = (int) lua_tonumber (state, -1); 224 printf ("Hello (3) -> %d\n", iNumber); 225 lua_pushnumber (state, iNumber + 2); 226 return 1; 227 } 228 229 230 void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc) 231 { 232 if (strcmp (strFunc.c_str(), "divideMe") == 0) 120 233 { 121 std::cout<<"Hi I'm Object 1 !"<<std::endl; 122 return 0; 234 // frames returns an answer of the stack 235 lua_State *state = (lua_State *) vm; 236 int itop = lua_gettop (state); 237 int iType = lua_type (state, -1); 238 const char *s = lua_typename (state, iType); 239 double fFrameCount = lua_tonumber (state, -1); 240 241 printf ("frame count = %f\n", fFrameCount); 123 242 } 124 125 void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc) 126 { 127 128 } 129 130 }; 131 132 class objectTwo : public Scriptable 133 { 134 public: 135 objectTwo () : Scriptable () 136 { 137 registerFunction(std::string("printNameTwo")); 138 // registerFunction(std::string("printHello")); 139 140 } 141 142 int scriptCalling (LuaVirtualMachine& vm, std::string functionName) 143 { 144 //switch (iFunctionNumber) //- m_iMethodBase) //TODO:find a way to determine the function number somehow: this works just incidentally 145 if(functionName.compare("printNameTwo")==0) 146 { 147 //case 0: 148 149 return printNameTwo (vm); 150 } 151 152 return 0; 153 } 154 155 156 int printNameTwo(LuaVirtualMachine& vm) 157 { 158 std::cout<<"Hi I'm Object 2 !\n"<<std::endl; 159 return 0; 160 } 161 162 int printHello(LuaVirtualMachine& vm) 163 { 164 std::cout<<"Nice to meet you!\n"<<std::endl; 165 return 0; 166 } 167 168 void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc) 169 { 170 171 } 172 173 }; 174 175 176 177 178 class CMyScript : public LuaScript 179 { 180 public: 181 CMyScript () : LuaScript () 182 { 183 // m_iMethodBase = registerFunction ("hello1"); 184 //registerFunction ("hello2"); 185 //registerFunction ("hello3"); 186 } 187 188 int scriptCalling (LuaVirtualMachine& vm, int iFunctionNumber) 189 { 190 /*switch (iFunctionNumber - m_iMethodBase) 191 { 192 case 0: 193 return Hello1 (vm); 194 case 1: 195 return Hello2 (vm); 196 case 2: 197 return Hello3 (vm); 198 } 199 200 return 0;*/ 201 } 202 203 int Hello1 (LuaVirtualMachine& vm) 204 { 205 printf ("Hellow (1)\n"); 206 return 0; 207 } 208 209 int Hello2 (LuaVirtualMachine& vm) 210 { 211 lua_State *state = (lua_State *) vm; 212 213 int iNumber = (int) lua_tonumber (state, -2); 214 int iNumber2 = (int) lua_tonumber (state, -1); 215 printf ("Hellow (2) -> %d\n", iNumber); 216 printf ("Second argument was %d\n",iNumber2); 217 return 0; 218 } 219 220 int Hello3 (LuaVirtualMachine& vm) 221 { 222 lua_State *state = (lua_State *) vm; 223 224 int iNumber = (int) lua_tonumber (state, -1); 225 printf ("Hello (3) -> %d\n", iNumber); 226 lua_pushnumber (state, iNumber + 2); 227 return 1; 228 } 229 230 231 void handleReturns (LuaVirtualMachine& vm, const std::string& strFunc) 232 { 233 if (strcmp (strFunc.c_str(), "divideMe") == 0) 234 { 235 // frames returns an answer of the stack 236 lua_State *state = (lua_State *) vm; 237 int itop = lua_gettop (state); 238 int iType = lua_type (state, -1); 239 const char *s = lua_typename (state, iType); 240 double fFrameCount = lua_tonumber (state, -1); 241 242 printf ("frame count = %f\n", fFrameCount); 243 } 244 } 245 246 protected: 247 int m_iMethodBase; 243 } 244 245 protected: 246 int m_iMethodBase; 248 247 }; 249 248 … … 252 251 { 253 252 254 std::cout << " lua-testing environment\n" << std::endl;253 std::cout << "== lua-testing environment ==\n" << std::endl; 255 254 256 255 … … 264 263 //addObjectToScript(&ob1, "obOne", ms, "printNameOne"); 265 264 int obTwoRef = ms.addScriptableToScript(&ob2, "object"); 266 // ms.addFunctionToScriptable("printNameTwo",obTwoRef,-1);265 // ms.addFunctionToScriptable("printNameTwo",obTwoRef,-1); 267 266 268 267 ms.compileFile ("luascript1.lua");
Note: See TracChangeset
for help on using the changeset viewer.