Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411485 Posts in 69371 Topics- by 58427 Members - Latest Member: shelton786

April 24, 2024, 04:40:10 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Lua and C++ - expose class with contructor that takes parameters to Lua
Pages: [1]
Print
Author Topic: Lua and C++ - expose class with contructor that takes parameters to Lua  (Read 580 times)
makerimages
Level 2
**


Makerimages Studios


View Profile WWW
« on: January 26, 2016, 11:29:12 AM »

Hi,

So. i've decided to do a project with C++ this year, to gain some more knowledge in how it works. Right now I'm working on implementing Lua integration to it. I've already gotten simple C++ functions to be called from Lua, when I can't find a good tutorial on, however is extending classes to Lua, specifically - classes that have constructors that take parameters. Say something like this.

Code:
SpecterApp::SpecterApp(std::string title, int width, int height, bool vsync, int frames)

How should I go about extending these(and any other class, for that matter), to Lua?

Solution
« Last Edit: January 27, 2016, 01:34:00 AM by makerimages » Logged

Makerimages-Its in the pixel
makerimages
Level 2
**


Makerimages Studios


View Profile WWW
« Reply #1 on: January 26, 2016, 12:27:16 PM »

Figured it out already.
Logged

Makerimages-Its in the pixel
oahda
Level 10
*****



View Profile
« Reply #2 on: January 26, 2016, 12:31:42 PM »

Then you should post your solution in case somebody else with the same problem finds this thread.
Logged

InfiniteStateMachine
Level 10
*****



View Profile
« Reply #3 on: January 26, 2016, 12:45:35 PM »

Then you should post your solution in case somebody else with the same problem finds this thread.

Required :

 
Logged

oahda
Level 10
*****



View Profile
« Reply #4 on: January 26, 2016, 01:09:19 PM »

Yep. D:
Logged

makerimages
Level 2
**


Makerimages Studios


View Profile WWW
« Reply #5 on: January 26, 2016, 01:14:31 PM »

Will do as soon as I get the code cleaned up a bit and/or am more awake than at 11:30PM.
Logged

Makerimages-Its in the pixel
makerimages
Level 2
**


Makerimages Studios


View Profile WWW
« Reply #6 on: January 27, 2016, 01:33:28 AM »

OK, here goes the solution

This actually proved to be simpler than expected.

I've got a class with a constructor like this:

Code:
SpecterApp::SpecterApp(std::string title, int width, int height, bool vsync, int frames)

Registering this is simple, at least with Lua 5.1. Every function to be registered needs a Lua equivalent in C++.
Like so, for the constructor.

Code:
int l_SpecterApp_constructor(lua_State * l)
{
    const char * name = luaL_checkstring(l, 1);
    int width = luaL_checkint(l, 2);
    int height = luaL_checkint(l,3);
    bool vsync = lua_toboolean(l,4);
    int frames = luaL_checkint(l,5);


    SpecterApp** udata = (SpecterApp **)lua_newuserdata(l, sizeof(SpecterApp *));
    *udata = new SpecterApp(name,width,height,vsync, frames);
    luaL_getmetatable(l, "luaL_SpecterApp");
    lua_setmetatable(l, -2);
    return 1;
}


And, in some other function that initializes the Lua stack.

Code:
 luaL_Reg sSpecterAppRegs[] =
    {
        { "create", l_SpecterApp_constructor },
        { NULL, NULL }
    };

    luaL_newmetatable(L, "luaL_SpecterApp");
    luaL_register(L, NULL, sSpecterAppRegs);
    lua_pushvalue(L, -1);
    lua_setfield(L, -1, "__index");
    lua_setglobal(L, "SpecterApp");


With this, A SpecterApp can be created from Lua as such:

Code:
local app = SpecterApp.create("An app",1280,720,true,60)
Logged

Makerimages-Its in the pixel
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic