Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8032 in orxonox.OLD for branches


Ignore:
Timestamp:
May 31, 2006, 4:14:55 PM (19 years ago)
Author:
snellen
Message:

finished Script.cc

Location:
branches/script_engine/src/lib
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/script_engine/src/lib/script_engine/Makefile.am

    r7975 r8032  
     1MAINSRCDIR=../..
     2include $(MAINSRCDIR)/defs/include_paths.am
    13
    24INCLUDES= -I../../../extern_libs
  • branches/script_engine/src/lib/script_engine/Script.cc

    r7975 r8032  
    44Script::Script()
    55{
     6
    67  luaState = lua_open();
    78
     
    2122  lua_close(luaState);
    2223}
     24
     25
     26bool Script::loadFile(std::string& filename)
     27 {
     28   int error = luaL_loadfile (luaState, filename.c_str());
     29
     30   if(error == 0)
     31   {
     32     return true;
     33   }
     34   else
     35   {
     36     switch(error)
     37     {
     38     case LUA_ERRSYNTAX :
     39       printf("Syntax error in file %s \n",filename.c_str() );
     40
     41     case LUA_ERRMEM :
     42       printf("Memory allocation error in file %s", filename.c_str() );
     43     }
     44   }
     45
     46 }
     47
     48
     49
     50 void Script::selectFunction(std::string& functionName, int retCount)
     51 {
     52   returnCount = retCount;
     53   currentFunction = functionName;
     54   lua_pushlstring(luaState, currentFunction.c_str(), currentFunction.size() );
     55   lua_gettable(luaState, LUA_GLOBALSINDEX);
     56 }
     57
     58 //return number of returned values
     59 int  Script::executeFunction()
     60 {
     61    lua_call(luaState, argumentCount, returnCount);
     62 }
     63
     64
     65 //overload this function to add different types
     66 bool Script::pushParam(int param, std::string& toFunction)
     67 {
     68   if(currentFunction.compare(toFunction) != 0)
     69   {
     70    lua_pushnumber(luaState,  param);
     71    return true;
     72   }
     73   else
     74   {
     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;
     77   }
     78
     79 }
     80
     81
     82 bool Script::pushParam(float param, std::string& toFunction)
     83 {
     84   if(currentFunction.compare(toFunction) != 0)
     85   {
     86     lua_pushnumber(luaState, param);
     87     return true;
     88   }
     89   else
     90   {
     91     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s", currentFunction.c_str(), toFunction.c_str());
     92     return false;
     93   }
     94
     95 }
  • branches/script_engine/src/lib/script_engine/Script.h

    r8021 r8032  
     1#ifndef _SCRIPT_H
     2#define _SCRIPT_H
     3
     4
    15#include <string>
     6#include "base_object.h"
    27
    38#include "luaincl.h"
    4 #include "lunar.h"
    59
    610
     
    1115    ~Script();
    1216
    13     void setFile();
     17    bool loadFile(std::string& filename);
    1418
    15     void insertObject();
    16     void registerClass();
     19    lua_State* getLuaState() const{return luaState;}
    1720
    18     void executeLuaFunction();
    19     void addParam();
     21    void selectFunction(std::string& functionName, int retCount);
     22    int  executeFunction(); //return number of returned values
     23    bool pushParam(int param, std::string& toFunction);//overload this function to add different types
     24    bool pushParam(float param, std::string& toFunction);
    2025
    2126
     
    2328  private:
    2429
    25     lua_State*         luaState; //!< The lua_State that the Script works on
    26 
     30    lua_State*         luaState;         //!< The lua_State that the Script works on
     31    std::string        currentFunction;  //!< The name of the current active function (the one that gets the pushed parameter)
     32    int                argumentCount;    //!< Number of arguments for the current function
     33    int                returnCount;      //!< Number of return values of the current function
    2734
    2835
     
    3037
    3138};
     39#endif /* _SCRIPT_H */
  • branches/script_engine/src/lib/script_engine/lunar.h

    r7975 r8032  
     1#ifndef _LUNAR_H
     2#define _LUNAR_H
     3
    14#include <string>
    2 
     5#include "Script.h"
    36
    47#include "luaincl.h"
     8
    59
    610
     
    1014      typedef int (T::*mfp)(lua_State *L);
    1115      typedef struct { const char *name; mfp mfunc; } RegType;
     16
     17
     18      static void Register(Script* script)
     19      {
     20        if(script != NULL)
     21          Register(script->getLuaState());
     22      }
    1223
    1324      static void Register(lua_State *L) {
     
    221232    };
    222233
    223 
     234#endif /* _LUNAR_H */
     235
Note: See TracChangeset for help on using the changeset viewer.