Changeset 8045 in orxonox.OLD for branches/script_engine
- Timestamp:
- May 31, 2006, 10:20:16 PM (18 years ago)
- Location:
- branches/script_engine/src/lib/script_engine
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/script_engine/src/lib/script_engine/Script.cc
r8044 r8045 4 4 Script::Script() 5 5 { 6 7 6 returnCount = argumentCount = 0; 8 7 … … 15 14 luaopen_math(luaState); 16 15 luaopen_debug(luaState); 17 18 16 19 17 } … … 30 28 { 31 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 32 33 int error = luaL_loadfile (luaState, filename.c_str()); 33 34 … … 37 38 38 39 if(error == 0) 40 { 41 currentFile = filename; 39 42 return true; 43 } 40 44 else 41 45 { 42 46 reportError(error); 43 return false;44 47 } 48 45 49 } 46 50 else … … 54 58 bool Script::executeFile() 55 59 { 56 int error = lua_pcall(luaState, 0, LUA_MULTRET, 0); 57 if( error== 0) 58 return true; 59 else 60 if(currentFile.length() != 0) 60 61 { 61 reportError(error); 62 return false; 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 } 63 70 } 64 71 return false; 65 72 } 66 73 67 voidScript::selectFunction(std::string& functionName, int retCount)74 bool Script::selectFunction(std::string& functionName, int retCount) 68 75 { 76 69 77 returnCount = retCount; 70 78 argumentCount = 0; … … 72 80 lua_pushlstring(luaState, currentFunction.c_str(), currentFunction.size() ); 73 81 lua_gettable(luaState, LUA_GLOBALSINDEX); 82 83 /* if(lua_isfunction( luaState , -1)) 84 { 85 return true; 86 } 87 else 88 return false;*/ 89 return true; 74 90 } 75 91 76 92 //return number of returned values 77 voidScript::executeFunction()93 bool Script::executeFunction() 78 94 { 79 lua_pcall(luaState, argumentCount, returnCount,0); 95 if(currentFunction.length() != 0 ) 96 { 97 int error = lua_pcall(luaState, argumentCount, returnCount,0); 98 if(error != 0) 99 { 100 reportError(error); 101 return false; 102 } 103 else 104 { 105 currentFunction.assign("");//a function gets unusable after beeing called for the first time 106 returnCount = argumentCount = 0; 107 return true; 108 } 109 } 110 else 111 printf("Error: no function selected.\n"); 80 112 } 81 113 … … 115 147 } 116 148 149 bool Script::pushParam(double param, std::string& toFunction) 150 { 151 if(currentFunction.compare(toFunction) == 0) 152 { 153 lua_pushnumber(luaState,(lua_Number) param); 154 argumentCount++; 155 return true; 156 } 157 else 158 { 159 printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); 160 return false; 161 } 162 163 } 164 117 165 int Script::reportError(int error) 118 166 { -
branches/script_engine/src/lib/script_engine/Script.h
r8044 r8045 20 20 lua_State* getLuaState() const{return luaState;} 21 21 22 voidselectFunction(std::string& functionName, int retCount);23 void executeFunction(); //return number of returned values22 bool selectFunction(std::string& functionName, int retCount); 23 bool executeFunction(); 24 24 bool pushParam(int param, std::string& toFunction);//overload this function to add different types 25 25 bool pushParam(float param, std::string& toFunction); 26 bool pushParam(double param, std::string& toFunction); 26 27 27 28 … … 33 34 34 35 lua_State* luaState; //!< The lua_State that the Script works on 36 std::string currentFile; //!< The file that is loaded in the script 35 37 std::string currentFunction; //!< The name of the current active function (the one that gets the pushed parameter) 36 38 int argumentCount; //!< Number of arguments for the current function -
branches/script_engine/src/lib/script_engine/account.cc
r8044 r8045 66 66 { 67 67 Script script; 68 69 // Register classes with the script 68 70 Lunar<Account>::Register(&script); 69 71 Lunar<Object>::Register(&script); … … 73 75 Account *b = new Account(30); 74 76 77 // Add instances to the script 75 78 Lunar<Object>::insertObject(&script,obj,"Obj",true); 76 79 Lunar<Account>::insertObject(&script,&a,"a",false); … … 81 84 82 85 if(script.loadFile(file)) 83 printf(" file %s succefully loaded\n", file.c_str());86 printf("File %s succefully loaded\n", file.c_str()); 84 87 85 88 //script.executeFile(); 86 89 87 90 std::string main("main"); 88 script.selectFunction(main, 1);89 script.pushParam(3 ,main);91 script.selectFunction(main,0); 92 script.pushParam(3.14159,main); 90 93 script.executeFunction(); 91 94
Note: See TracChangeset
for help on using the changeset viewer.