Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/account.cc @ 8078

Last change on this file since 8078 was 8078, checked in by snellen, 19 years ago

added example how to return values to lua

File size: 3.0 KB
Line 
1
2#include <iostream>
3
4#include "luaincl.h"
5#include "lunar.h"
6#include "script.h"
7
8    class Account {
9      lua_Number m_balance;
10    public:
11      static const char className[];
12      static Lunar<Account>::RegType methods[];
13
14      Account(lua_State *L)      { m_balance = luaL_checknumber(L, 1); }
15      Account(double balance=0)    : m_balance(balance) { }
16      int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; }
17      int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; }
18      int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; }
19      ~Account() { printf("deleted Account (%p)\n", this); }
20    };
21
22    const char Account::className[] = "Account";
23
24
25    #define method(class, name) {#name, &class::name}
26
27    Lunar<Account>::RegType Account::methods[] = {
28      method(Account, deposit),
29      method(Account, withdraw),
30      method(Account, balance),
31      {0,0}
32    };
33
34
35    class Object {
36      public:
37        static const char className[];
38        static Lunar<Object>::RegType methods[];
39
40        Object(lua_State* L) {callCount = 0; }
41        Object(){callCount = 0;};
42        ~Object() { printf("deleted Object (%p)\n", this); }
43
44        //lua Interface
45        int getCallCount(lua_State* L){int calls = getCallCount(); lua_pushnumber(L,(lua_Number)calls);return 1;}
46
47
48        int printName(lua_State* L)
49        {
50          callCount ++;
51          printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount);
52          return 0;
53        }
54
55        int getCallCount(){return callCount;}
56
57      private:
58        int callCount;
59
60    };
61
62    const char Object::className[] = "Object";
63
64    Lunar<Object>::RegType Object::methods[] = {
65      method(Object, printName),
66      method(Object, getCallCount),
67      {0,0}
68    };
69
70    int main(int argc, char *argv[])
71    {
72      Script script;
73
74      // Register classes with the script
75      Lunar<Account>::Register(&script);
76      Lunar<Object>::Register(&script);
77
78      Object* obj= new Object();
79      Account a;
80      Account *b = new Account(30);
81
82      // Add instances to the script
83      Lunar<Object>::insertObject(&script,obj,"Obj",true);
84      Lunar<Account>::insertObject(&script,&a,"a",false);
85      Lunar<Account>::insertObject(&script,b,"b",true);
86
87
88      std::string file(argv[1]);
89
90      if(script.loadFile(file))
91        printf("File %s succefully loaded\n", file.c_str());
92
93      //script.executeFile();
94
95      std::string main("main");
96      if( script.selectFunction(main,3))
97       printf("function %s selected\n",main.c_str());
98
99      script.pushParam(3.14159,main);
100      script.executeFunction();
101
102      float retf = script.getReturnedFloat();
103      printf("main returned %f\n",retf);
104     
105
106      if(script.getReturnedBool())
107       printf("main returned true\n");
108      else
109       printf("main returned false\n");
110
111      int ret = script.getReturnedInt();
112      printf("main returned %i\n",ret);
113     
114      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
115
116      return 0;
117    }
118
Note: See TracBrowser for help on using the repository browser.