After tons, and tons of researches, I had finally figured out an solution although I'm not sure about how efficient it is, it's actually working the way i wanted it to be.
So the final solution is, to register an intptr_t
converted from the pointer i needed to lua,(for more information about intptr_t
, click here(mainly for safety reason, you can see it as a kind of int design just for pointers)) and pass it to the static function when I'm using it
Here is the Code after modification:
In Test_Class.cpp
, after:
luabind::module(myLuaState)[
.def("shoot_a_bullet", &shoot_a_bullet)
];// since the functions are static now it will not need the namespace
I had registered the intptr_t
version of pointer:
//first convert pointer to intptr_t
intptr_t stage = (intptr_t)stg;//stg is the pointer
//then register it to lua
//if you try to register a pointer it will give you runtime error
luabind::globals(myLuaState)["stage"] = stage;
We also need to change the function a little bit:
First, we need to make the function static, so in the header file:
//change the line defined the function to
void static shoot_a_bullet(intptr_t stg, int damage);
Now we have to do things with the function it self:
void Test_Class::shoot_a_bullet(intptr_t stg, int damage)
{
//we need to convert it back to pointer first, so
Test_Stage* stage = (Test_Stage*)stg;//this is the part I'm not sure about efficiency
stage->create_bullet(damage, "wowo");
}
So now the Code with lua will be really simple: shoot_a_bullet(stage, 134);
We are done! Life is good! This stuff has costed me 3 week.
In chance you might not understanding what i said, here is the complete test code i had writen:
main
.cpp:
#include <iostream>
#include "test_stage.h"
using namespace std;
int x;
int main() {
cin >> x;
cin.get();// clean the \n(when you press enter) after the number
Test_Stage stage = Test_Stage(x);
}
Test_Stage
.h:
class Test_Class;
#ifndef TEST_STAGE_H
#define TEST_STAGE_H
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include ".\luabind\luabind.hpp"
#include <iostream>
#include "test_class.h"
using namespace std;
class Test_Stage
{
public:
int x;
Test_Stage();
Test_Stage(int num);
void create_bullet(int damage, string name); /*This is currently useless
before i have understand how to include each other using foward referance*/
void create_class(int num);
Test_Class t_class;
~Test_Stage();
private:
};
#endif
Test_Stage
cpp:
#include"test_stage.h"
#include "test_class.h"// and as you see i included both files
Test_Stage::Test_Stage()
{
}
Test_Stage::Test_Stage(int num)
{
x = num;// a variable specific in this object(to test pointer)
create_class(num);
}
void Test_Stage::create_bullet(int damage, string name)
{
cout << "created damage: " << damage << " to " << x << endl;/*using the value
created in this object to see if pointer is actually working*/
}
void Test_Stage::create_class(int num)
{
Test_Class t_class = Test_Class(this, num);
}
Test_Stage::~Test_Stage()
{
}
Test_Class
.h:
#ifndef TEST_CLASS_H
#define TEST_CLASS_H
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include ".\luabind\luabind.hpp"
#include <iostream>
using namespace std;
class Test_Stage;
class Test_Class
{
public:
int x;
Test_Class();
Test_Class(Test_Stage* stage, int num);
void static shoot_a_bullet(intptr_t stg, int damage);
~Test_Class();
private:
};
#endif TEST_CLASS_H
And finally,Test_Class
.cpp:
#include "test_class.h"
#include"test_stage.h"
void Test_Class::shoot_a_bullet(intptr_t stg, int damage)
{
Test_Stage* stage = (Test_Stage*)stg; // intptr_t back to pointer
stage->create_bullet(damage, "wowo"); // use pointer
}
Test_Class::Test_Class()
{
}
Test_Class::Test_Class(Test_Stage* stg, int num)
{
intptr_t stage = (intptr_t)stg;
// Create a new lua state
lua_State *myLuaState = luaL_newstate();
// Connect LuaBind to this lua state
luabind::open(myLuaState);
luaL_openlibs(myLuaState);
luabind::module(myLuaState)[
luabind::def("shoot_a_bullet", &shoot_a_bullet)//again this is static now
];
luabind::globals(myLuaState)["stage"] = stage;
cout << "I'm here " << endl;//just to check how far did the program go
luaL_dostring(
myLuaState,
"shoot_a_bullet(stage, 134)\n"
);
cout << "I passed it"<< endl;
cin.get();
}
Test_Class::~Test_Class()
{
}
Have fun!