Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8044 in orxonox.OLD


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

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

Location:
branches/script_engine/src/lib/script_engine
Files:
5 edited

Legend:

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

    r8032 r8044  
    44Script::Script()
    55{
     6
     7  returnCount = argumentCount = 0;
    68
    79  luaState = lua_open();
     
    1315  luaopen_math(luaState);
    1416  luaopen_debug(luaState);
     17
    1518
    1619}
     
    2629bool Script::loadFile(std::string& filename)
    2730 {
     31
    2832   int error = luaL_loadfile (luaState, filename.c_str());
    2933
    3034   if(error == 0)
    3135   {
    32      return true;
     36     error = lua_pcall(luaState, 0, 0, 0);
     37
     38     if(error == 0)
     39      return true;
     40     else
     41     {
     42      reportError(error);
     43      return false;
     44     }
    3345   }
    3446   else
    3547   {
    36      switch(error)
    37      {
    38      case LUA_ERRSYNTAX :
    39        printf("Syntax error in file %s \n",filename.c_str() );
     48     reportError(error);
     49   }
    4050
    41      case LUA_ERRMEM :
    42        printf("Memory allocation error in file %s", filename.c_str() );
    43      }
     51   return false;
     52 }
     53
     54 bool Script::executeFile()
     55 {
     56   int error = lua_pcall(luaState, 0, LUA_MULTRET, 0);
     57   if( error== 0)
     58     return true;
     59   else
     60   {
     61     reportError(error);
     62     return false;
    4463   }
    4564
    4665 }
    4766
    48 
    49 
    5067 void Script::selectFunction(std::string& functionName, int retCount)
    5168 {
    5269   returnCount = retCount;
     70   argumentCount = 0;
    5371   currentFunction = functionName;
    5472   lua_pushlstring(luaState, currentFunction.c_str(), currentFunction.size() );
     
    5775
    5876 //return number of returned values
    59  int Script::executeFunction()
     77 void Script::executeFunction()
    6078 {
    61     lua_call(luaState, argumentCount, returnCount);
     79    lua_pcall(luaState, argumentCount, returnCount,0);
    6280 }
    6381
     
    6684 bool Script::pushParam(int param, std::string& toFunction)
    6785 {
    68    if(currentFunction.compare(toFunction) != 0)
     86   if(currentFunction.compare(toFunction) == 0)
    6987   {
    70     lua_pushnumber(luaState,  param);
     88     lua_pushnumber(luaState, (lua_Number) param);
     89     argumentCount++;
    7190    return true;
    7291   }
    7392   else
    7493   {
    75      printf("Couldn't add parameter because the wrong function is selected: %s instead of %s", currentFunction.c_str(), toFunction.c_str());
    76      return false;
     94    printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
     95    return false;
    7796   }
    7897
     
    82101 bool Script::pushParam(float param, std::string& toFunction)
    83102 {
    84    if(currentFunction.compare(toFunction) != 0)
     103   if(currentFunction.compare(toFunction) == 0)
    85104   {
    86      lua_pushnumber(luaState, param);
     105     lua_pushnumber(luaState,(lua_Number) param);
     106     argumentCount++;
    87107     return true;
    88108   }
    89109   else
    90110   {
    91      printf("Couldn't add parameter because the wrong function is selected: %s instead of %s", currentFunction.c_str(), toFunction.c_str());
     111     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
    92112     return false;
    93113   }
    94114
    95115 }
     116
     117 int Script::reportError(int error)
     118 {
     119 const char *msg = lua_tostring(luaState, -1);
     120 if (msg == NULL) msg = "(error with no message)";
     121 fprintf(stderr, "ERROR: %s\n", msg);
     122 lua_pop(luaState, 1);
     123 return error;
     124 }
     125
  • branches/script_engine/src/lib/script_engine/Script.h

    r8032 r8044  
    1616
    1717    bool loadFile(std::string& filename);
     18    bool executeFile();
    1819
    1920    lua_State* getLuaState() const{return luaState;}
    2021
    2122    void selectFunction(std::string& functionName, int retCount);
    22     int  executeFunction(); //return number of returned values
     23    void  executeFunction(); //return number of returned values
    2324    bool pushParam(int param, std::string& toFunction);//overload this function to add different types
    2425    bool pushParam(float param, std::string& toFunction);
     
    2627
    2728
     29
    2830  private:
     31
     32    int reportError(int error);
    2933
    3034    lua_State*         luaState;         //!< The lua_State that the Script works on
  • 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    }
  • branches/script_engine/src/lib/script_engine/lunar.h

    r8032 r8044  
    136136      }
    137137
     138
     139      static int insertObject(Script* script, T* obj, const std::string& name, bool gc=false)
     140      {
     141        if(script != NULL)
     142          return insertObject(script->getLuaState(), obj, name, gc);
     143
     144
     145      }
     146
    138147      // get userdata from Lua stack and return pointer to T object
    139148      static T *check(lua_State *L, int narg) {
  • branches/script_engine/src/lib/script_engine/lunartest.lua

    r8021 r8044  
    44  printf("Account balance = $%0.02f\n", self:balance())
    55end
     6
    67
    78o = Object()
     
    2122a:show() a:deposit(50.30) a:show() a:withdraw(25.10) a:show()
    2223
     24
     25
    2326parent = {}
    24 
    25 function parent:rob(amount)
     27 function parent:rob(amount)
    2628  amount = amount or self:balance()
    2729  self:withdraw(amount)
Note: See TracChangeset for help on using the changeset viewer.