Changeset 8032 in orxonox.OLD for branches/script_engine/src
- Timestamp:
- May 31, 2006, 4:14:55 PM (19 years ago)
- Location:
- branches/script_engine/src/lib
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/script_engine/src/lib/script_engine/Makefile.am
r7975 r8032 1 MAINSRCDIR=../.. 2 include $(MAINSRCDIR)/defs/include_paths.am 1 3 2 4 INCLUDES= -I../../../extern_libs -
branches/script_engine/src/lib/script_engine/Script.cc
r7975 r8032 4 4 Script::Script() 5 5 { 6 6 7 luaState = lua_open(); 7 8 … … 21 22 lua_close(luaState); 22 23 } 24 25 26 bool Script::loadFile(std::string& filename) 27 { 28 int error = luaL_loadfile (luaState, filename.c_str()); 29 30 if(error == 0) 31 { 32 return true; 33 } 34 else 35 { 36 switch(error) 37 { 38 case LUA_ERRSYNTAX : 39 printf("Syntax error in file %s \n",filename.c_str() ); 40 41 case LUA_ERRMEM : 42 printf("Memory allocation error in file %s", filename.c_str() ); 43 } 44 } 45 46 } 47 48 49 50 void Script::selectFunction(std::string& functionName, int retCount) 51 { 52 returnCount = retCount; 53 currentFunction = functionName; 54 lua_pushlstring(luaState, currentFunction.c_str(), currentFunction.size() ); 55 lua_gettable(luaState, LUA_GLOBALSINDEX); 56 } 57 58 //return number of returned values 59 int Script::executeFunction() 60 { 61 lua_call(luaState, argumentCount, returnCount); 62 } 63 64 65 //overload this function to add different types 66 bool Script::pushParam(int param, std::string& toFunction) 67 { 68 if(currentFunction.compare(toFunction) != 0) 69 { 70 lua_pushnumber(luaState, param); 71 return true; 72 } 73 else 74 { 75 printf("Couldn't add parameter because the wrong function is selected: %s instead of %s", currentFunction.c_str(), toFunction.c_str()); 76 return false; 77 } 78 79 } 80 81 82 bool Script::pushParam(float param, std::string& toFunction) 83 { 84 if(currentFunction.compare(toFunction) != 0) 85 { 86 lua_pushnumber(luaState, param); 87 return true; 88 } 89 else 90 { 91 printf("Couldn't add parameter because the wrong function is selected: %s instead of %s", currentFunction.c_str(), toFunction.c_str()); 92 return false; 93 } 94 95 } -
branches/script_engine/src/lib/script_engine/Script.h
r8021 r8032 1 #ifndef _SCRIPT_H 2 #define _SCRIPT_H 3 4 1 5 #include <string> 6 #include "base_object.h" 2 7 3 8 #include "luaincl.h" 4 #include "lunar.h"5 9 6 10 … … 11 15 ~Script(); 12 16 13 void setFile();17 bool loadFile(std::string& filename); 14 18 15 void insertObject(); 16 void registerClass(); 19 lua_State* getLuaState() const{return luaState;} 17 20 18 void executeLuaFunction(); 19 void addParam(); 21 void selectFunction(std::string& functionName, int retCount); 22 int executeFunction(); //return number of returned values 23 bool pushParam(int param, std::string& toFunction);//overload this function to add different types 24 bool pushParam(float param, std::string& toFunction); 20 25 21 26 … … 23 28 private: 24 29 25 lua_State* luaState; //!< The lua_State that the Script works on 26 30 lua_State* luaState; //!< The lua_State that the Script works on 31 std::string currentFunction; //!< The name of the current active function (the one that gets the pushed parameter) 32 int argumentCount; //!< Number of arguments for the current function 33 int returnCount; //!< Number of return values of the current function 27 34 28 35 … … 30 37 31 38 }; 39 #endif /* _SCRIPT_H */ -
branches/script_engine/src/lib/script_engine/lunar.h
r7975 r8032 1 #ifndef _LUNAR_H 2 #define _LUNAR_H 3 1 4 #include <string> 2 5 #include "Script.h" 3 6 4 7 #include "luaincl.h" 8 5 9 6 10 … … 10 14 typedef int (T::*mfp)(lua_State *L); 11 15 typedef struct { const char *name; mfp mfunc; } RegType; 16 17 18 static void Register(Script* script) 19 { 20 if(script != NULL) 21 Register(script->getLuaState()); 22 } 12 23 13 24 static void Register(lua_State *L) { … … 221 232 }; 222 233 223 234 #endif /* _LUNAR_H */ 235
Note: See TracChangeset
for help on using the changeset viewer.