Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/lib/script_engine/example.cc @ 10659

Last change on this file since 10659 was 8271, checked in by bensch, 19 years ago

merge

File size: 4.5 KB
Line 
1
2#include <iostream>
3
4#include "luaincl.h"
5#include "lunar.h"
6#include "script.h"
7
8// create classdummys for the scripts
9CREATE_SCRIPTABLE_CLASS(Account, CL_ACCOUNT)
10CREATE_SCRIPTABLE_CLASS(Object, CL_TEST_OBJECT)
11
12    class Account : public BaseObject {
13      lua_Number m_balance;
14    public:
15      static const char className[];
16      static Lunar<Account>::RegType methods[];
17
18      Account(lua_State *L)      { m_balance = luaL_checknumber(L, 1); }
19      Account(double balance=0)    : m_balance(balance) { }
20
21      void deposit (float value) { m_balance += value; };
22      void withdraw(float value) { m_balance -= value; };
23      float balance() { return m_balance; };
24
25      int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; }
26      int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; }
27      int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; }
28      ~Account() { printf("deleted Account (%p)\n", this); }
29    };
30
31    const char Account::className[] = "Account";
32
33    Lunar<Account>::RegType Account::methods[] = {
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}
38    };
39
40
41    class Object : public BaseObject {
42      public:
43        static const char className[];
44        static Lunar<Object>::RegType methods[];
45
46        Object(lua_State* L) {callCount = 0; }
47        Object(){callCount = 0;};
48        ~Object() { printf("deleted Object (%p)\n", this); }
49
50        //meber functions
51        void takeParam(int i)
52        {
53         printf("Lua passed %i to c++\n",i);
54        }
55
56        int printName(lua_State* L)
57        {
58          this->printName();
59          return 0;
60        }
61
62        void printName()
63        {
64          callCount ++;
65          printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount);
66        }
67
68        int getCallCount(){ return callCount; }
69
70      private:
71        int callCount;
72
73    };
74
75    const char Object::className[] = "Object";
76
77    Lunar<Object>::RegType Object::methods[] = {
78      {"printName", new ExecutorLua0<Object>(&Object::printName)},
79      {"getCallCount", new ExecutorLua0ret<Object, int>(&Object::getCallCount)},
80      {"takeParam", new ExecutorLua1<Object, int>(&Object::takeParam)},
81      {0,0}
82    };
83
84    int main(int argc, char *argv[])
85    {
86      std::string file("lunartest2.lua");
87     // Script script;
88      Script* script = ScriptManager::getInstance()->getScriptByFile(file);
89      printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
90
91      // Register classes with the script
92      // Lunar<Account>::Register(&script);
93      //Lunar<Object>::Register(&script);
94
95      //Object* obj= new Object();
96      //Account a;
97      //Account *b = new Account(30);
98
99      // Add instances to the script
100      //Lunar<Object>::insertObject(&script,obj,"Obj",true);
101      //Lunar<Account>::insertObject(&script,&a,"a",false);
102      //Lunar<Account>::insertObject(&script,b,"b",true);
103       //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));
104
105      //Load a file
106      //std::string file("lunartest2.lua");
107
108      //if(script.loadFile(file))
109        //printf("File %s succefully loaded\n", file.c_str());
110      //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));
111
112      //execute a function
113      printf("----------- main -----------\n");
114      std::string main("main");
115      if( script->selectFunction(main,3))
116       printf("function %s selected\n",main.c_str());
117
118      script->pushParam(3.14159,main);
119      script->executeFunction();
120
121      float retf = script->getReturnedFloat();
122      printf("main returned %f\n",retf);
123
124
125      if(script->getReturnedBool())
126       printf("main returned true\n");
127      else
128       printf("main returned false\n");
129
130      int ret = script->getReturnedInt();
131      printf("main returned %i\n",ret);
132
133      //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));
134      //execute a 2nd function
135      printf("----------- test -----------\n");
136      std::string test("test");
137      if( script->selectFunction(test,0))
138       printf("function %s selected\n",test.c_str());
139
140      script->executeFunction();
141
142
143      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
144      printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
145
146      return 0;
147    }
148
Note: See TracBrowser for help on using the repository browser.