Overload method


#1

Hi guys, I’ve read some topics here about this matter but I can’t get my code to work.

class sceneManager
{
public:
    sceneManager();
    sceneManager(std::string tag, uint duration);
    sceneManager(std::string tag, uint duration, scene _scene);
    virtual ~sceneManager();

    static void addScene(std::string tag, uint duration);

    static void addScene(std::string tag, uint duration, scene _scene);

    static void setCurrentScene(std::string tag);

    static void setCurrentScene(scene *_scene);

    static void draw(sf::RenderWindow *window);

    static void update();

    ///////////////////
    /// \brief all_scenes
    /// listagem de todas as scenes
    static std::map<std::string, scene> all_scenes;

    static scene* currentScene;
    //////////////////


};

I want to add the setCurrentScene(std::string tag)

My last attemp was

chai->add(chaiscript::fun<void, sceneManager, std::string >(&sceneManager::setCurrentScene), "setCurrentScene");

I can only add to chaiscript the function draw from sceneManager:
chai->add(chaiscript::fun(&sceneManager::draw), "drawScene");

ps: Sorry about my English, and I’m quite new to c++. Feel free to correct me.


#2

You have two options here:

The static_cast option:

chai->add(chaiscript::fun(static_cast<void (sceneManager::*)(std::string)>(&sceneManager::setCurrentScene)), "setCurrentScene");

And the lambda option:

chai->add(chaiscript::fun([](sceneManager *obj, std::string tag) { obj->setCurrentScene(tag); }), "setCurrentScene");

Both exist to try and convince the compiler which version of your overload you want to use.

-Jason


#3

Hi lefticus, thanks for the quick response.

Only the lambda option works, with the static cast I get

error: address of overloaded function 'setCurrentScene' cannot be static_cast to type 'void (sceneManager::*)(std::string)' chai->add(chaiscript::fun(static_cast<void (sceneManager::*)(std::string)>(&sceneManager::setCurrentScene)), "setCurrentScene");

Edit:

I’ve created a .chai file with

setCurrentScene("splashScreen");

but gives me an error on runtime:

libc++abi.dylib: terminating with uncaught exception of type chaiscript::exception::eval_error: Error: "Function dispatch arity mismatch with function 'setCurrentScene'"


#4

Since it’s a method you’re going to have to provide an object to call that method on, just like you would in C++.

//c++
sceneManager obj;
chai.add(chaiscript::var(&obj), "obj");
// ChaiScript
obj.setCurrentScene("spaceScreen");

#5

Of course, makes sense. noob :cold_sweat:

Thank you very much for you time :slight_smile: