而是在lua中创建一个对象,如何让lua直接将C ++对象高高地启动一个方法? [英] Rather then create a object in lua, how let lua directly tall the C++ object to launch a method?

查看:115
本文介绍了而是在lua中创建一个对象,如何让lua直接将C ++对象高高地启动一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Luabind.

I'm using Luabind.

我的头衔可能还不清楚,我会尽力解释我想问的问题.

My title might be kind of unclear, I will try the best i can to explain what i want to ask.

我的问题是:
我该如何直接调高C ++对象的方法,该方法可以访问对象的值(尤其是指针),而不是在Luabind中创建另一个对象.

My question is:
How do i directly tall C++ object's method that can access the object's values(especially pointers) rather then creating Another object in Luabind.

如果您不知道我在问什么,可以继续阅读.

If you don't know what I'm asking, You can continue reading.

例如,我有三个类:MainTest_StageTest_Class

For example, i have three classes: Main, Test_Stage, Test_Class

Lua仅在Test_class中创建.

我有一个变量x,仅出于测试目的而创建.它由其构造函数从MainTest_StageTest_Class一直传递.这样Test_ClassTest_Stage都具有一个全局值,这在我实际制作游戏时会需要.
更重要的是Test_Class拥有Test_Stage的指针,这样我就可以执行create_a_bulletcreate_damage之类的事情.

I have a variable x ,created just for testing purpose. It is passed all the way from the Main to Test_Stage to Test_Class by their constructor. So that both Test_Class and Test_Stage have a global value which I'll need when I'm actually making the game.
More important thing is that Test_Class holds a pointer of Test_Stage, so that i can do such things as create_a_bullet or create_damage.

从此教程中获悉,我试图使在Test_Class中创建的luaobject调用方法shoot_a_bullet,该方法将使'Test_Stage'对象高大,以打印"Runned"<< 全球价值".而且,在C ++中没有语法错误的情况下,IT部门并未打印任何内容.我该如何解决?

Learned from this Tutorial, I had tried to make The luaobject created in Test_Class call the method shoot_a_bullet which will tall 'Test_Stage' object to print "Runned " << 'the global value'. And without a syntax error in C++, IT didn't print anything. How can i fix this?

代码在这里(实际上我在使用包含类的正向操作时遇到了麻烦,因此"Runned"<< x在Test_Class中.至少我可以测试它是否将读取已通过此处的全局值.)

Codes are here(actually i had trouble working with forward including using class, so "Runned" << x is in Test_Class. At least I can test if it will read the global value had passed here.)

几小时无所事事之后,我有了另一种解决方案.有没有一种方法可以传递可以在lua中创建的对象中使用的指针,或者可以在构造函数中使用的指针?

Just after few hours of doing nothing, I had though about another solution. Is there a way i can pass the pointer that can be used in the object that is created in lua, or maybe used in constructer?

代码(如果您来回答这个问题,您可以考虑跳过main和Test_Stage:

Codes(you might considering skip the main and Test_Stage if you come for answering the question :

主文件启动程序:

#include <iostream>
#include "test_stage.h"

using namespace std;

int x;

int main() {

    cin >> x;
    Test_Stage stage = Test_Stage(x);
}

Test_Stage的标题:

#ifndef TEST_STAGE_H
#define TEST_STAGE_H
#include <iostream>
class Test_Class;// to avoid circular include error, i used forward referancing
                 // i will include the file in the CPP file
                 // a class Test_Class which define the class is enough in header
using namespace std;

class Test_Stage
{
    public:
        int x;
        Test_Stage(int num);

        void create_bullet(int damage, string name, int x); /*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文件:

The cpp file of Test_Stage:

#include"test_stage.h"
#include "test_class.h"// and as you see i included both files(i just learned it few secs ago)

Test_Stage::Test_Stage()
{

}

Test_Stage::Test_Stage(int num)
{
    create_class(num);
}

void Test_Stage::create_bullet(int damage, string name, int x)
{
    cout << "created damage: " << damage << "to" << x ;
}

void Test_Stage::create_class(int num)
{
    Test_Class t_class = Test_Class(num, this);
}

Test_Stage::~Test_Stage()
{

}

Test_Class的标头:

The header of Test_Class:

#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(int num, Test_Stage* stage);
        void shoot_a_bullet(Test_Class* o, int damage);

        Test_Stage *stage;
        ~Test_Class();
    private:

};

#endif TEST_CLASS_H

最后是Test_Class的cpp(给我带来很多麻烦):

And finally the cpp of Test_Class(causing me lot's trouble):

#include "test_class.h"
#include"test_stage.h"

void Test_Class::shoot_a_bullet(Test_Class* o, int damage)
{
    cout << "Runned";
    stage->create_bullet(damage, "wowo", x);
}

Test_Class::Test_Class()
{

}

Test_Class::Test_Class(int num, Test_Stage* stg)
{
    stage = stg;
    x = num;
    // 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::class_<Test_Class>("Test_Class")
            .def("shoot_a_bullet", &Test_Class::shoot_a_bullet)
    ];

    /*followed the tutorial codes
    class_<A>("A")
        .def("plus", &plus)*/

    cout << "im here";//just to check how far did the program go
    luaL_dostring(
        myLuaState,
        "shoot_a_bullet(134)\n"
    );
    cout << "I passed it";
    cin.get();
    cin.get();//To pause the program before it closes
    // if you have the time, can you also explain 
    // why do i need two cin.get() to pause the program.
}

Test_Class::~Test_Class()
{

}    

推荐答案

经过无数次的研究,我终于找到了解决方案,尽管我不确定它的效率如何,实际上它的工作方式是希望如此.
所以最终的解决方案是,注册从我需要转换为lua的指针转换的intptr_t(有关intptr_t的更多信息,

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

这是修改后的代码:

Test_Class.cpp中,之后:

luabind::module(myLuaState)[
    .def("shoot_a_bullet", &shoot_a_bullet)
];// since the functions are static now it will not need the namespace

我已经注册了intptr_t版本的指针:

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");
}

所以现在带有lua的代码将非常简单: Shoot_a_bullet(stage,134);

So now the Code with lua will be really simple: shoot_a_bullet(stage, 134);

我们完成了!生活很好!这些东西花了我3个星期.

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

最后,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()
{

}    

玩得开心!

这篇关于而是在lua中创建一个对象,如何让lua直接将C ++对象高高地启动一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆