// Load game script and entity data
auto* script = systems.system<ScriptSystem>().get();
script->addToGameNamespace("loadFont",
- [this](std::string name, std::string file, int size) {
- systems.system<TextSystem>().get()->loadFont(name, file, size);
- });
+ bindInstance(&TextSystem::loadFont,
+ systems.system<TextSystem>().get()));
script->addToGameNamespace("puts",
- [this](std::string name, float x, float y, std::string text) {
- systems.system<TextSystem>().get()->put(name, x, y, text);
- });
+ bindInstance(&TextSystem::put,
+ systems.system<TextSystem>().get()));
script->init();
void ScriptSystem::scriptExport(void)
{
- std::function<sol::table(sol::table)> entitySpawn =
- [this](sol::table t){ return spawn(t);};
-
- std::function<World* (sol::object)> worldRegister =
- [this](sol::object t){ return worldSystem.addWorld(t); };
-
lua.new_usertype<Position>("Position",
sol::constructors<Position(double x, double y), Position()>(),
"x", &Position::x,
"getSize", &World::getSize);
game = lua["game"].get_or_create<sol::table>();
- game.set_function("spawn", entitySpawn);
- game.set_function("worldRegister", worldRegister);
+ game.set_function("spawn", bindInstance(&ScriptSystem::spawn, this));
+ game.set_function("worldRegister", bindInstance(&WorldSystem::addWorld,
+ &worldSystem));
}
sol::table ScriptSystem::spawn(sol::object param)
#include "world.hpp"
+/**
+ * Utility for pairing class instances to their member function calls.
+ * This is useful for adding functions to the Lua game namespace.
+ *
+ * @param func The member function to call
+ * @param instance The instance to bind to
+ * @return A function that calls the member function using the given instance
+ */
+template<class C, typename R, typename... Args>
+auto bindInstance(R (C::* func)(Args...), C *instance)
+{
+ return [instance, func](Args... args) { (instance->*func)(args...); };
+}
+
+
struct EntitySpawnEvent
{
sol::object ref;