Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/script_engine/example.cc @ 9700

Last change on this file since 9700 was 8271, checked in by bensch, 18 years ago

merge

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