get the 4 returns of the update function

This commit is contained in:
Bluub
2021-06-07 11:19:23 +02:00
parent 2c3fbee797
commit 75f6b04e9a
4 changed files with 42 additions and 12 deletions
+3 -3
View File
@@ -1,7 +1,7 @@
function sum(a,b)
function Sum(a,b)
return a + b;
end
function update()
return 1;
function Update()
return 1, 1, false, false;
end
@@ -13,7 +13,7 @@ namespace BBM
: Component(entity), _scriptPath(scriptPath), state(luaL_newstate())
{
luaL_dofile(state, scriptPath.c_str());
lua_getglobal(state, "update");
lua_getglobal(state, "Update");
if (!lua_isfunction(state, -1))
std::cout << "No update function in the script" << std::endl;
}
@@ -13,21 +13,46 @@ namespace BBM
typeid(ControllableComponent)})
{ }
float IAControllableSystem::getReturnNumber(lua_State *state)
{
float res = 0;
if (lua_isnil(state, -1))
return res;
if (!lua_isnumber(state, -1))
return res;
res = lua_tonumber(state, -1);
lua_pop(state, 1);
return res;
}
bool IAControllableSystem::getReturnBool(lua_State *state)
{
bool res = false;
if (lua_isnil(state, -1))
return res;
if (!lua_isboolean(state, -1))
return res;
res = lua_toboolean(state, -1);
lua_pop(state, 1);
return res;
}
void IAControllableSystem::onFixedUpdate(WAL::Entity &entity)
{
auto &ia = entity.getComponent<IAControllableComponent>();
auto &controllable = entity.getComponent<ControllableComponent>();
lua_getglobal(ia.state, "update");
lua_getglobal(ia.state, "Update");
//push parameters
int nbParams = 0;
int nbReturn = 1;
int nbReturn = 4;
lua_pcall(ia.state, nbParams, nbReturn, 0);
if (lua_isnil(ia.state, -1))
return;
if (!lua_isnumber(ia.state, -1))
return;
controllable.move.y = lua_tonumber(ia.state, -1);
lua_pop(ia.state, 1);
controllable.bomb = getReturnBool(ia.state);
controllable.jump = getReturnBool(ia.state);
controllable.move.y = getReturnNumber(ia.state);
controllable.move.x = getReturnNumber(ia.state);
}
}
@@ -11,6 +11,11 @@ namespace BBM
//! @brief A system to handle keyboard entities.
class IAControllableSystem : public WAL::System
{
private:
//! @brief extract a number from the lua stack
float getReturnNumber(lua_State *state);
//! @brief extract a bool from the lua stack
bool getReturnBool(lua_State *state);
public:
//! @inherit
void onFixedUpdate(WAL::Entity &entity) override;