Changeset 8044 in orxonox.OLD for branches/script_engine/src
- Timestamp:
- May 31, 2006, 9:24:11 PM (19 years ago)
- Location:
- branches/script_engine/src/lib/script_engine
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/script_engine/src/lib/script_engine/Script.cc
r8032 r8044 4 4 Script::Script() 5 5 { 6 7 returnCount = argumentCount = 0; 6 8 7 9 luaState = lua_open(); … … 13 15 luaopen_math(luaState); 14 16 luaopen_debug(luaState); 17 15 18 16 19 } … … 26 29 bool Script::loadFile(std::string& filename) 27 30 { 31 28 32 int error = luaL_loadfile (luaState, filename.c_str()); 29 33 30 34 if(error == 0) 31 35 { 32 return true; 36 error = lua_pcall(luaState, 0, 0, 0); 37 38 if(error == 0) 39 return true; 40 else 41 { 42 reportError(error); 43 return false; 44 } 33 45 } 34 46 else 35 47 { 36 switch(error) 37 { 38 case LUA_ERRSYNTAX : 39 printf("Syntax error in file %s \n",filename.c_str() ); 48 reportError(error); 49 } 40 50 41 case LUA_ERRMEM : 42 printf("Memory allocation error in file %s", filename.c_str() ); 43 } 51 return false; 52 } 53 54 bool Script::executeFile() 55 { 56 int error = lua_pcall(luaState, 0, LUA_MULTRET, 0); 57 if( error== 0) 58 return true; 59 else 60 { 61 reportError(error); 62 return false; 44 63 } 45 64 46 65 } 47 66 48 49 50 67 void Script::selectFunction(std::string& functionName, int retCount) 51 68 { 52 69 returnCount = retCount; 70 argumentCount = 0; 53 71 currentFunction = functionName; 54 72 lua_pushlstring(luaState, currentFunction.c_str(), currentFunction.size() ); … … 57 75 58 76 //return number of returned values 59 intScript::executeFunction()77 void Script::executeFunction() 60 78 { 61 lua_ call(luaState, argumentCount, returnCount);79 lua_pcall(luaState, argumentCount, returnCount,0); 62 80 } 63 81 … … 66 84 bool Script::pushParam(int param, std::string& toFunction) 67 85 { 68 if(currentFunction.compare(toFunction) != 0)86 if(currentFunction.compare(toFunction) == 0) 69 87 { 70 lua_pushnumber(luaState, param); 88 lua_pushnumber(luaState, (lua_Number) param); 89 argumentCount++; 71 90 return true; 72 91 } 73 92 else 74 93 { 75 printf("Couldn't add parameter because the wrong function is selected: %s instead of %s", currentFunction.c_str(), toFunction.c_str());76 94 printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); 95 return false; 77 96 } 78 97 … … 82 101 bool Script::pushParam(float param, std::string& toFunction) 83 102 { 84 if(currentFunction.compare(toFunction) != 0)103 if(currentFunction.compare(toFunction) == 0) 85 104 { 86 lua_pushnumber(luaState, param); 105 lua_pushnumber(luaState,(lua_Number) param); 106 argumentCount++; 87 107 return true; 88 108 } 89 109 else 90 110 { 91 printf("Couldn't add parameter because the wrong function is selected: %s instead of %s ", currentFunction.c_str(), toFunction.c_str());111 printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); 92 112 return false; 93 113 } 94 114 95 115 } 116 117 int Script::reportError(int error) 118 { 119 const char *msg = lua_tostring(luaState, -1); 120 if (msg == NULL) msg = "(error with no message)"; 121 fprintf(stderr, "ERROR: %s\n", msg); 122 lua_pop(luaState, 1); 123 return error; 124 } 125 -
branches/script_engine/src/lib/script_engine/Script.h
r8032 r8044 16 16 17 17 bool loadFile(std::string& filename); 18 bool executeFile(); 18 19 19 20 lua_State* getLuaState() const{return luaState;} 20 21 21 22 void selectFunction(std::string& functionName, int retCount); 22 intexecuteFunction(); //return number of returned values23 void executeFunction(); //return number of returned values 23 24 bool pushParam(int param, std::string& toFunction);//overload this function to add different types 24 25 bool pushParam(float param, std::string& toFunction); … … 26 27 27 28 29 28 30 private: 31 32 int reportError(int error); 29 33 30 34 lua_State* luaState; //!< The lua_State that the Script works on -
branches/script_engine/src/lib/script_engine/account.cc
r7975 r8044 1 #include "luaincl.h"2 #include <iostream>3 1 4 #include "lunar.h" 2 #include <iostream> 3 4 #include "luaincl.h" 5 #include "lunar.h" 6 #include "Script.h" 5 7 6 8 class Account { … … 11 13 12 14 Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } 15 Account(double balance=0) : m_balance(balance) { } 13 16 int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; } 14 17 int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; } … … 36 39 37 40 Object(lua_State* L) {callCount = 0; } 41 Object(){callCount = 0;}; 38 42 ~Object() { printf("deleted Object (%p)\n", this); } 43 39 44 40 45 int printName(lua_State* L) 41 46 { 47 callCount ++; 42 48 printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount); 43 callCount ++;44 49 return 0; 45 50 } … … 60 65 int main(int argc, char *argv[]) 61 66 { 62 lua_State *L = lua_open(); 67 Script script; 68 Lunar<Account>::Register(&script); 69 Lunar<Object>::Register(&script); 63 70 64 luaopen_base(L); 65 luaopen_table(L); 66 luaopen_io(L); 67 luaopen_string(L); 68 luaopen_math(L); 69 luaopen_debug(L); 71 Object* obj= new Object(); 72 Account a; 73 Account *b = new Account(30); 70 74 71 Lunar<Account>::Register(L); 72 Lunar<Object>::Register(L); 75 Lunar<Object>::insertObject(&script,obj,"Obj",true); 76 Lunar<Account>::insertObject(&script,&a,"a",false); 77 Lunar<Account>::insertObject(&script,b,"b",true); 73 78 74 Object* obj= new Object(L);75 Lunar<Object>::insertObject(L,obj,"Obj",false);76 //delete obj;77 79 78 if(argc>1) lua_dofile(L,argv[1]);80 std::string file(argv[1]); 79 81 80 lua_setgcthreshold(L, 0); // collected garbage 81 lua_close(L); 82 if(script.loadFile(file)) 83 printf("file %s succefully loaded\n", file.c_str()); 84 85 //script.executeFile(); 86 87 std::string main("main"); 88 script.selectFunction(main,1); 89 script.pushParam(3,main); 90 script.executeFunction(); 91 92 //if(argc>1) lua_dofile(script.getLuaState(), argv[1]); 93 82 94 return 0; 83 95 } -
branches/script_engine/src/lib/script_engine/lunar.h
r8032 r8044 136 136 } 137 137 138 139 static int insertObject(Script* script, T* obj, const std::string& name, bool gc=false) 140 { 141 if(script != NULL) 142 return insertObject(script->getLuaState(), obj, name, gc); 143 144 145 } 146 138 147 // get userdata from Lua stack and return pointer to T object 139 148 static T *check(lua_State *L, int narg) { -
branches/script_engine/src/lib/script_engine/lunartest.lua
r8021 r8044 4 4 printf("Account balance = $%0.02f\n", self:balance()) 5 5 end 6 6 7 7 8 o = Object() … … 21 22 a:show() a:deposit(50.30) a:show() a:withdraw(25.10) a:show() 22 23 24 25 23 26 parent = {} 24 25 function parent:rob(amount) 27 function parent:rob(amount) 26 28 amount = amount or self:balance() 27 29 self:withdraw(amount)
Note: See TracChangeset
for help on using the changeset viewer.