Changeset 8101 in orxonox.OLD for branches/script_engine/src/lib/script_engine/example.cc
- Timestamp:
- Jun 1, 2006, 7:24:22 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/script_engine/src/lib/script_engine/example.cc
r8093 r8101 6 6 #include "script.h" 7 7 8 class Account { 8 9 class Account : public BaseObject { 9 10 lua_Number m_balance; 10 11 public: … … 14 15 Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } 15 16 Account(double balance=0) : m_balance(balance) { } 17 18 void deposit (float value) { m_balance += value; }; 19 void withdraw(float value) { m_balance -= value; }; 20 float balance() { return m_balance; };;;;;;;;;;;;;; 21 16 22 int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; } 17 23 int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; } … … 26 32 27 33 Lunar<Account>::RegType Account::methods[] = { 28 method(Account, deposit),29 method(Account, withdraw),30 method(Account, balance),31 {0, 0}34 {"deposit", new ExecutorLua1<Account, float>(&Account::deposit)}, 35 {"withdraw", new ExecutorLua1<Account, float>(&Account::withdraw)}, 36 {"balance", new ExecutorLua0ret<Account, float>(&Account::balance)}, 37 {0,NULL} 32 38 }; 33 39 34 40 35 class Object {41 class Object : public BaseObject { 36 42 public: 37 43 static const char className[]; … … 47 53 { 48 54 int calls = getCallCount(); 49 lua_pushnumber(L,(lua_Number)calls); 55 lua_pushnumber(L,(lua_Number)calls); 50 56 return 1; // return number of values that the function wants to return to lua 51 57 } … … 53 59 //function that takes an argument from lua 54 60 int takeParam(lua_State* L) 55 { 61 { 56 62 int param = (int)lua_tonumber(L,-1); 57 63 takeParam(param); 58 return 0; 64 return 0; 59 65 } 60 66 … … 67 73 int printName(lua_State* L) 68 74 { 69 callCount ++; 70 printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount); 75 this->printName(); 71 76 return 0; 72 77 } 73 78 74 int getCallCount(){return callCount;} 79 void printName() 80 { 81 callCount ++; 82 printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount); 83 } 84 85 int getCallCount(){ return callCount; } 75 86 76 87 private: … … 82 93 83 94 Lunar<Object>::RegType Object::methods[] = { 84 method(Object, printName),85 method(Object, getCallCount),86 method(Object, takeParam),95 {"printName", new ExecutorLua0<Object>(&Object::printName)}, 96 {"getCallCount", new ExecutorLua0ret<Object, int>(&Object::getCallCount)}, 97 {"takeParam", new ExecutorLua1<Object, int>(&Object::takeParam)}, 87 98 {0,0} 88 99 }; … … 123 134 float retf = script.getReturnedFloat(); 124 135 printf("main returned %f\n",retf); 125 136 126 137 127 138 if(script.getReturnedBool()) … … 142 153 script.executeFunction(); 143 154 144 155 145 156 //if(argc>1) lua_dofile(script.getLuaState(), argv[1]); 146 157 printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));
Note: See TracChangeset
for help on using the changeset viewer.