create global objects

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

create global objects

chrislu
hi,
i am fairly new to luabind. what i want to do is to create an global
lua object from an c++ instance of that object, so that this
particular object is shared between lua an c++.

for example:

class test_class
{
public:
    test_class(const std::string& name) : _name(name) {}
    std::string         _name;
};

test_class plah_instance("plah_instance");

this is registered with:

void register_lua_bindings(lua_State* l_state)
{
    using namespace luabind;

    module(l_state)
    [
        class_<test_class>("test_class")
            .def(constructor<const std::string&>())
            .def_readwrite("_name", &test_class::_name)
    ];
}

now i am searching for a way to create an global variable and to bind
and reference to plah_instance to it.

a workaround could be the following:

test_class& get_inst() {
    return plah_instance;
}

and to register this also to lua, but this way i would have to call
get_inst() everytime or bind the return value to a variable in lua.

is there a way to maybe do something like:

module(l_state)
[
    def_global("global_name", global_ref or global_ptr)
]

i think with the lua api it is possible to create a global variable
and bind some userdata to it, what maybe the way to go....

thx

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: create global objects

Josh Green-3
The way this is done (at least the way I do it) is as follows:

luabind::object = luabind::globals(lua);
globals["pah_instance"] = pah_instance;

--Josh

----Original Message Follows----
From: "Christopher Lux" <[hidden email]>
Reply-To: [hidden email]
To: [hidden email]
Subject: [luabind] create global objects

hi,
i am fairly new to luabind. what i want to do is to create an global
lua object from an c++ instance of that object, so that this
particular object is shared between lua an c++.

for example:

class test_class
{
public:
     test_class(const std::string& name) : _name(name) {}
     std::string         _name;
};

test_class plah_instance("plah_instance");

this is registered with:

void register_lua_bindings(lua_State* l_state)
{
     using namespace luabind;

     module(l_state)
     [
         class_<test_class>("test_class")
             .def(constructor<const std::string&>())
             .def_readwrite("_name", &test_class::_name)
     ];
}

now i am searching for a way to create an global variable and to bind
and reference to plah_instance to it.

a workaround could be the following:

test_class& get_inst() {
     return plah_instance;
}

and to register this also to lua, but this way i would have to call
get_inst() everytime or bind the return value to a variable in lua.

is there a way to maybe do something like:

module(l_state)
[
     def_global("global_name", global_ref or global_ptr)
]

i think with the lua api it is possible to create a global variable
and bind some userdata to it, what maybe the way to go....

thx

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job
easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: create global objects

chrislu
Josh Green <[hidden email]> wrote:

> luabind::object = luabind::globals(lua);
> globals["pah_instance"] = pah_instance;

Thanks, thats beautifully simple ;)

-chris

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: create global objects

chrislu
In reply to this post by Josh Green-3
On 8/24/06, Josh Green <[hidden email]> wrote:

> The way this is done (at least the way I do it) is as follows:
>
> luabind::object = luabind::globals(lua);
> globals["pah_instance"] = pah_instance;

Hi,
i have to follow up on this. I was just yet able to test this. There
is one _big_ problem with this. The instances on the lua and C++ side
are different. I added a method to the class to test this:

[...]
    void inst() { std::cout << this << std::endl; }
[...]

I also exported it to lua. Now the output of this method is different
called from lua as when called from c++. Note that i thought that i
bound the same instance of my class to lua which exists in c++.

The problem now is, when i change an attribute of this class in either
c++ or lua at runtime the other won't realize this change which is a
big problem now for me...

Is there a solution to this?

Regards
-chris

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: create global objects

Josh Green-3
Thats no good, it appears to work great for me,

I use it in this way:

        Settings *settings = (Settings*)app->getSystem(SYSTEM_SETTINGS_ID);
        ResourceSystem *resources =
(ResourceSystem*)app->getSystem(SYSTEM_RESOURCESYSTEM_ID);
        InputSystem *input = (InputSystem*)app->getSystem(SYSTEM_INPUT_ID);
        SceneSystem *scenes = (SceneSystem*)app->getSystem(SYSTEM_SCENES_ID);

        //Add all the system objects to the base:
        (*globals)["engine"] = (Engine*)app;
        (*globals)["resources"] = resources;
        (*globals)["input"] = input;
        (*globals)["scenes"] = scenes;
        (*globals)["settings"] = &settings->values;
        (*globals)["console"] = ((Engine*)app)->getConsole();
        (*globals)["scripting"] = (ScriptSystem*)this;

This all works great and I can modify them in either lua or C++ and the
other side will notice the change.

You may want to make sure that you Cast it to the type that you want lua to
use it as?

>The problem now is, when i change an attribute of this class in either
>c++ or lua at runtime the other won't realize this change which is a
>big problem now for me...

Works for me, perhaps you should post some example code of how you've done
this?

--Josh



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: create global objects

Josh Green-3
Just realised I didn't show you how I got "globals"
in the initialisation of the class (and when I initialise lua) I do the
following:

luabind::object *globals = new luabind::object(luabind::globals(lua));


Thats no good, it appears to work great for me,

I use it in this way:

        Settings *settings = (Settings*)app->getSystem(SYSTEM_SETTINGS_ID);
        ResourceSystem *resources =
(ResourceSystem*)app->getSystem(SYSTEM_RESOURCESYSTEM_ID);
        InputSystem *input = (InputSystem*)app->getSystem(SYSTEM_INPUT_ID);
        SceneSystem *scenes = (SceneSystem*)app->getSystem(SYSTEM_SCENES_ID);

        //Add all the system objects to the base:
        (*globals)["engine"] = (Engine*)app;
        (*globals)["resources"] = resources;
        (*globals)["input"] = input;
        (*globals)["scenes"] = scenes;
        (*globals)["settings"] = &settings->values;
        (*globals)["console"] = ((Engine*)app)->getConsole();
        (*globals)["scripting"] = (ScriptSystem*)this;

This all works great and I can modify them in either lua or C++ and the
other side will notice the change.

You may want to make sure that you Cast it to the type that you want lua to
use it as?

 >The problem now is, when i change an attribute of this class in either
 >c++ or lua at runtime the other won't realize this change which is a
 >big problem now for me...

Works for me, perhaps you should post some example code of how you've done
this?

--Josh



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job
easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: create global objects

chrislu
In reply to this post by Josh Green-3
Josh Green <[hidden email]> wrote:

> Thats no good, it appears to work great for me,
>
> I use it in this way:
>
> [...]
>
> This all works great and I can modify them in either lua or C++ and the
> other side will notice the change.
>
> You may want to make sure that you Cast it to the type that you want lua  
> to use it as?

Oh Thanks!
I found my Error, i did the following. My error was to assign the global  
reference, by what i assume a copy of the object was made. With assigning  
a pointer to the reference all works as expected.

Thanks again.

Now the test code:

class test_lua
{
public:
     test_lua() :_val(0.0f) {}
     void test_ssw(float a) {
         std::cout << a << std::endl;
     }

     void inst() { std::cout << this << std::endl; }

     void set_value(float v) { _val = v; }
     float get_value() const { return _val; }

private:
     float _val;
};

test_lua& g_inst = test_lua();

void register_lua_bindings(lua_State* l_state)
{
     using namespace luabind;

     module(l_state)
     [
         class_<test_lua>("test_lua")
             .def("test_ssw", &test_lua::test_ssw)
             .def("inst", &test_lua::inst)
             .def("set_value", &test_lua::set_value)
             .def("get_value", &test_lua::get_value),

         def("exit", &stop_interpreter)
     ];

     globals(l_state)["g_inst"] = g_inst;
}


--
Christopher Lux |
                 | Bauhaus University Weimar
                 | Faculty of Media - Virtual Reality Systems Group

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user