Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 31, 2006, 9:24:11 PM (19 years ago)
Author:
snellen
Message:

calling scriptfunctions from c++ works now (includeing adding parameters)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/script_engine/src/lib/script_engine/account.cc

    r7975 r8044  
    1     #include "luaincl.h"
    2     #include <iostream>
    31
    4     #include "lunar.h"
     2#include <iostream>
     3
     4#include "luaincl.h"
     5#include "lunar.h"
     6#include "Script.h"
    57
    68    class Account {
     
    1113
    1214      Account(lua_State *L)      { m_balance = luaL_checknumber(L, 1); }
     15      Account(double balance=0)    : m_balance(balance) { }
    1316      int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; }
    1417      int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; }
     
    3639
    3740        Object(lua_State* L) {callCount = 0; }
     41        Object(){callCount = 0;};
    3842        ~Object() { printf("deleted Object (%p)\n", this); }
     43
    3944
    4045        int printName(lua_State* L)
    4146        {
     47          callCount ++;
    4248          printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount);
    43           callCount ++;
    4449          return 0;
    4550        }
     
    6065    int main(int argc, char *argv[])
    6166    {
    62       lua_State *L = lua_open();
     67      Script script;
     68      Lunar<Account>::Register(&script);
     69      Lunar<Object>::Register(&script);
    6370
    64       luaopen_base(L);
    65       luaopen_table(L);
    66       luaopen_io(L);
    67       luaopen_string(L);
    68       luaopen_math(L);
    69       luaopen_debug(L);
     71      Object* obj= new Object();
     72      Account a;
     73      Account *b = new Account(30);
    7074
    71       Lunar<Account>::Register(L);
    72       Lunar<Object>::Register(L);
     75      Lunar<Object>::insertObject(&script,obj,"Obj",true);
     76      Lunar<Account>::insertObject(&script,&a,"a",false);
     77      Lunar<Account>::insertObject(&script,b,"b",true);
    7378
    74       Object* obj= new Object(L);
    75       Lunar<Object>::insertObject(L,obj,"Obj",false);
    76       //delete obj;
    7779
    78       if(argc>1) lua_dofile(L, argv[1]);
     80      std::string file(argv[1]);
    7981
    80       lua_setgcthreshold(L, 0);  // collected garbage
    81       lua_close(L);
     82      if(script.loadFile(file))
     83        printf("file %s succefully loaded\n", file.c_str());
     84
     85      //script.executeFile();
     86
     87      std::string main("main");
     88      script.selectFunction(main,1);
     89      script.pushParam(3,main);
     90      script.executeFunction();
     91
     92      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
     93
    8294      return 0;
    8395    }
Note: See TracChangeset for help on using the changeset viewer.