mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-09 04:49:39 -10:00
The current scripting hook is pretty simple and straight forward but it makes a couple of scripting applications harder to implement than needed. For example, a lot of scripts need to do some work every frame for all or some objects. The script needs to manage an internal list of all the objects that are affected by that list which includes removing invalid objects to conserve resources. This effectively mirrors what the engine already does and adds unnecessary overhead. In these changes I added a generic event system which can be used by any part of the code for providing callbacks when a specific event occurs. As an example, I added hooks for the pre- and post-move events to the object class which are called before and after physics are applied to an object. I integrated that feature into the scripting system so now a script can simply register a function as a callback which will be called whenever the target event is executed.
49 lines
1.0 KiB
C++
49 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include "object/object.h"
|
|
#include "scripting/lua/LuaFunction.h"
|
|
|
|
namespace scripting {
|
|
namespace api {
|
|
namespace util {
|
|
|
|
void convert_arg(lua_State* L, luacpp::LuaValueList& out, object* objp);
|
|
}
|
|
|
|
template <typename Ret, typename... Args>
|
|
class LuaEventCallback {
|
|
luacpp::LuaFunction _func;
|
|
|
|
void convert_args(lua_State* /*L*/, luacpp::LuaValueList& /*out*/) {}
|
|
|
|
template <typename T, typename... A>
|
|
void convert_args(lua_State* L, luacpp::LuaValueList& out, T t, A... args)
|
|
{
|
|
util::convert_arg(L, out, t);
|
|
|
|
convert_args(L, out, args...);
|
|
}
|
|
|
|
public:
|
|
explicit LuaEventCallback(const luacpp::LuaFunction& func) : _func(func) {}
|
|
|
|
void operator()(Args... args)
|
|
{
|
|
using namespace luacpp;
|
|
|
|
LuaValueList lua_args;
|
|
convert_args(_func.getLuaState(), lua_args, args...);
|
|
|
|
_func(lua_args);
|
|
}
|
|
};
|
|
|
|
template <typename Ret, typename... Args>
|
|
std::function<Ret(Args...)> make_lua_callback(const luacpp::LuaFunction& func)
|
|
{
|
|
return std::function<Ret(Args...)>(LuaEventCallback<Ret, Args...>(func));
|
|
}
|
|
|
|
} // namespace api
|
|
} // namespace scripting
|