Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/script_engine/account.cc @ 8076

Last change on this file since 8076 was 8061, checked in by bensch, 18 years ago

merged the scriptengine back to the trunk

File size: 2.5 KB
RevLine 
[7821]1
[8044]2#include <iostream>
[7821]3
[8044]4#include "luaincl.h"
5#include "lunar.h"
6#include "Script.h"
7
[7821]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); }
[8044]15      Account(double balance=0)    : m_balance(balance) { }
[7821]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
[7962]35    class Object {
36      public:
37        static const char className[];
38        static Lunar<Object>::RegType methods[];
39
[7975]40        Object(lua_State* L) {callCount = 0; }
[8044]41        Object(){callCount = 0;};
[7962]42        ~Object() { printf("deleted Object (%p)\n", this); }
43
[8044]44
[7975]45        int printName(lua_State* L)
46        {
[8044]47          callCount ++;
[7975]48          printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount);
49          return 0;
50        }
51
52
53      private:
54        int callCount;
55
[7962]56    };
57
58    const char Object::className[] = "Object";
59
60    Lunar<Object>::RegType Object::methods[] = {
61      method(Object, printName),
62      {0,0}
63    };
64
[7821]65    int main(int argc, char *argv[])
66    {
[8044]67      Script script;
[8045]68
69      // Register classes with the script
[8044]70      Lunar<Account>::Register(&script);
71      Lunar<Object>::Register(&script);
[7821]72
[8044]73      Object* obj= new Object();
74      Account a;
75      Account *b = new Account(30);
[7821]76
[8045]77      // Add instances to the script
[8044]78      Lunar<Object>::insertObject(&script,obj,"Obj",true);
79      Lunar<Account>::insertObject(&script,&a,"a",false);
80      Lunar<Account>::insertObject(&script,b,"b",true);
[7821]81
[7962]82
[8044]83      std::string file(argv[1]);
[7821]84
[8044]85      if(script.loadFile(file))
[8045]86        printf("File %s succefully loaded\n", file.c_str());
[8044]87
88      //script.executeFile();
89
90      std::string main("main");
[8060]91      if( script.selectFunction(main,1))
[8058]92       printf("function %s selected\n",main.c_str());
93
[8045]94      script.pushParam(3.14159,main);
[8044]95      script.executeFunction();
96
[8058]97      int ret = script.getReturnedInt();
98      printf("main returned %i\n",ret);
99
[8044]100      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
101
[7821]102      return 0;
103    }
104
Note: See TracBrowser for help on using the repository browser.