Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't call constructor of a derived class #1456

Open
jacodiego opened this issue Feb 17, 2023 · 1 comment
Open

Can't call constructor of a derived class #1456

jacodiego opened this issue Feb 17, 2023 · 1 comment

Comments

@jacodiego
Copy link

Hello, i am having a problem, when i try to call the constructor of a derived class, here the case in synthesis:

c++

class GameScreen
    {
    public:
        GameScreen();
        GameScreen(ScreenType);
    };

class MapScreen : public screen::GameScreen
    {
    public:
        MapScreen();
    };

sol::table screen_module = lua["screen"].get_or_create<sol::table>();
        screen_module.new_usertype<GameScreen>("GameScreen", sol::constructors<GameScreen(), GameScreen(ScreenType)>());

sol::table map_module = lua["map"].get_or_create<sol::table>();
        map_module.new_usertype<map::MapScreen>("MapScreen", sol::constructors<>(),
                                                sol::base_classes, sol::bases<screen::GameScreen>());

lua

-- This is OK
print("pointer to class: ", screen.MapScreen);

-- This fail
print("call constructor: ", screen.MapScreen:new());

Could i be forgetting something?

Regards

@Rochet2
Copy link

Rochet2 commented Feb 17, 2023

Maybe you should call screen.MapScreen.new(), so with . instead of :.
You are also using screen while the example code looks like it should use map.
Looks like your example code has some missing code. It is hard to know if some of it causes your problems.

I tested your example and made it work in godbolt, so you can try take a look and see how your code differs.

#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>

typedef int ScreenType;

class GameScreen {
   public:
    GameScreen() {}
    GameScreen(ScreenType) {}
};

class MapScreen : public GameScreen {
   public:
    MapScreen(){};
};

int main() {
    // Sol
    sol::state lua;
    lua.open_libraries(sol::lib::base, sol::lib::package);

    sol::table screen_module = lua["screen"].get_or_create<sol::table>();
    screen_module.new_usertype<GameScreen>(
        "GameScreen",
        sol::constructors<GameScreen(), GameScreen(ScreenType)>());

    sol::table map_module = lua["map"].get_or_create<sol::table>();
    map_module.new_usertype<MapScreen>("MapScreen", sol::constructors<MapScreen()>(),
                                       sol::base_classes,
                                       sol::bases<GameScreen>());

    lua.script("print('pointer to class: ', map.MapScreen);");
    lua.script("print('call constructor: ', map.MapScreen.new());");

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants