Hi,
I'm learning lua with BMAX
I'm interrested to learn how to interface my classe, method and member variables of my bmax class with lua.
i have see an example in cpp. I would like to know how to make the same with bmax
how to make the equivalent in blitzmax for using lua the following function
void RegisterAnimalWithLua(lua_State* pLua)
{
module(pLua)
[
class_<Animal>("Animal")
.def(constructor<string, int>())
.def("Speak", &Animal::Speak)
.def("NumLegs", &Animal::NumLegs)
];
}
Example of lua class :
note this sample come from excellent book
Programming Game AI by Example
by Mat Buckland ISBN:1556220782
Wordware Publishing © 2005 (495 pages)
I'm learning lua with BMAX
I'm interrested to learn how to interface my classe, method and member variables of my bmax class with lua.
i have see an example in cpp. I would like to know how to make the same with bmax
how to make the equivalent in blitzmax for using lua the following function
void RegisterAnimalWithLua(lua_State* pLua)
{
module(pLua)
[
class_<Animal>("Animal")
.def(constructor<string, int>())
.def("Speak", &Animal::Speak)
.def("NumLegs", &Animal::NumLegs)
];
}
Example of lua class :
-- ExposingCPPClassesToLua.lua --create an animal object and call its methods cat = Animal("Meow", 4); print ("\n[Lua]: A cat has "..cat:NumLegs().. " legs."); cat:Speak(); print ("\n\n----------------------------------------------------"); MyPet = Pet("Scooter", "Meow", 4); print ("\n[Lua]: My pet is called "..MyPet:GetName()); MyPet:Speak(); [\code] how to define in bmax who are a constructor and animal methods cat=Animal("Meow", 4); cat:NumLegs() cat:Speak(); cpp code : animal.h #ifndef ANIMAL_H #define ANIMAL_H #include <string> #include <iostream> ---------------------- pet.h #ifndef PET_H #define PET_H #include <string> #include <iostream> #include "Animal.h" class Pet : public Animal { private: std::string m_Name; public: Pet(std::string name, std::string noise, int NumLegs):Animal(noise, NumLegs), m_Name(name) {} std::string GetName()const{return m_Name;} }; #endif ---------------------- main.cpp //include the libraries #pragma comment(lib, "lua.lib") #pragma comment(lib, "lualib.lib") #pragma warning (disable : 4786) extern "C" { #include <lua.h> #include <lualib.h> #include <lauxlib.h> } #include <string> #include <iostream> using namespace std; //include the luabind headers. Make sure you have the paths set correctly //to the lua, luabind and Boost files. #include <luabind/luabind.hpp> using namespace luabind; #include "LuaHelperFunctions.h" #include "Animal.h" #include "Pet.h" void RegisterAnimalWithLua(lua_State* pLua) { module(pLua) [ class_<Animal>("Animal") .def(constructor<string, int>()) .def("Speak", &Animal::Speak) .def("NumLegs", &Animal::NumLegs) ]; } void RegisterPetWithLua(lua_State* pLua) { module(pLua) [ class_<Pet, bases<Animal> >("Pet") .def(constructor<string, string, int>()) .def("GetName", &Pet::GetName) ]; } int main() { //create a lua state lua_State* pLua = lua_open(); //open the libraries OpenLuaLibraries(pLua); //open luabind open(pLua); RegisterAnimalWithLua(pLua); RegisterPetWithLua(pLua); //load and run the script RunLuaScript(pLua, "ExposingCPPClassesToLua.lua"); lua_close(pLua); return 0; } class Animal { private: int m_iNumLegs; std::string m_NoiseEmitted; public: Animal(std::string NoiseEmitted, int NumLegs):m_iNumLegs(NumLegs), m_NoiseEmitted(NoiseEmitted) {} virtual ~Animal(){} virtual void Speak()const {std::cout << "\n[C++]: " << m_NoiseEmitted << std::endl;} int NumLegs()const{return m_iNumLegs;} }; #endif
note this sample come from excellent book
Programming Game AI by Example
by Mat Buckland ISBN:1556220782
Wordware Publishing © 2005 (495 pages)