Changeset 8044 in orxonox.OLD for branches/script_engine/src/lib/script_engine/Script.cc
- Timestamp:
- May 31, 2006, 9:24:11 PM (18 years ago)
- File:
-
- 1 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
Note: See TracChangeset
for help on using the changeset viewer.