Suppose I have a code like this in C++
// Game.cpp
Entity* hero;
Entity* witch;
... // entity initialization
if(/player near witch/) {
if(hero->get<HealthComponent>()->getHealth() < 100) {
say("Let me help you");
witch->get<GraphicsComponent>()->setAnimation("witch.heal");
hero->get<HealthComponent>()->setHealth(100);
}
else {
say("Go away!");
}
}
Suppose I want to put this code into Lua script, so I make an "interact" function in C++
void interact(Entity* first, Entity* second);
And call it like this:
interact(witch, hero);
Then I can call lua script, which gets first and second entity IDs and does something like this:
-- Script.lua
function interact(firstId, secondId)
if(getHealth(secondId) < 100) then
say("Let me help you")
setAnimation(firstId, "witch.heal")
setHealth(secondId, 100)
else
say("Go away")
end
end
And then I can define wrapper functions in C++ like this
void setAnimation(int firstId, const std::string& animationName) {
Entity* e = entities[firstId];
e->get<GraphicsComponent>()->setAnimation(animationName);
}
// etc.
and call it from Lua. But this gets quite troublesome as I need to write lots of binding and wrapping(like "setAnimation") functions. Is there any other methods to control entitity behaviour using Lua?