Skip to content

Commit

Permalink
fix #3396 lua_function= doesn't work in [effect][filter]
Browse files Browse the repository at this point in the history
unfortunately this adds an annoying const_cast. The old code 'worked around'
this by getting a nonconst reference to the same unit via the unit map,
which i think is not better just hides the issue.
  • Loading branch information
gfgtdf committed Sep 10, 2018
1 parent 00e41b9 commit 08b7d1b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -4738,12 +4738,16 @@ bool game_lua_kernel::run_filter(char const *name, const map_location& l)
bool game_lua_kernel::run_filter(char const *name, const unit& u)
{
lua_State *L = mState;
unit_map::const_unit_iterator ui = units().find(u.get_location());
if (!ui.valid()) return false;
// Pass the unit as argument.
luaW_pushunit(L, ui->underlying_id());

return run_filter(name, 1);
lua_unit* lu = luaW_pushlocalunit(L, const_cast<unit&>(u));
// stack: unit
// put the unit to the stack twice to prevent gc.
lua_pushvalue(L, -1);
// stack: unit, unit
bool res = run_filter(name, 1);
// stack: unit
lu->clear_ref();
lua_pop(L, 1);
return res;
}
/**
* Runs a script from a filter.
Expand Down

0 comments on commit 08b7d1b

Please sign in to comment.