Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 31, 2006, 9:24:11 PM (18 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/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
Note: See TracChangeset for help on using the changeset viewer.