aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2019-10-01 08:37:05 -0400
committerClyne Sullivan <clyne@bitgloo.com>2019-10-01 08:37:05 -0400
commitad5a63db312d0029109e6ca0051feaa516419ad2 (patch)
treed0834bae2ab7ae06bfdc58b38ab32428cf1aeb50
parent5cceca5ec696e6626cea0ec07f9db1b28bb3b875 (diff)
cleaned up lua function bindings
-rw-r--r--src/engine.cpp10
-rw-r--r--src/script.cpp11
-rw-r--r--src/script.hpp15
3 files changed, 22 insertions, 14 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index b1d9a56..81e0272 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -59,13 +59,11 @@ int Engine::init(void)
// 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();
diff --git a/src/script.cpp b/src/script.cpp
index 6cda627..0f940d3 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -87,12 +87,6 @@ void ScriptSystem::doFile(void)
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,
@@ -137,8 +131,9 @@ void ScriptSystem::scriptExport(void)
"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)
diff --git a/src/script.hpp b/src/script.hpp
index 0ac9e63..24cc142 100644
--- a/src/script.hpp
+++ b/src/script.hpp
@@ -26,6 +26,21 @@
#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;