Add evt.beforesim and evt.aftersim for running code only when simulation advances

Only triggered when simulation is unpaused or simulating via subframe debugging. beforesim is the location where most vanilla sim handlers are run.
This commit is contained in:
jacob1 2022-12-19 22:31:21 -05:00
parent 299781bf13
commit 63661a752c
No known key found for this signature in database
GPG Key ID: 4E58A32D510E1995
3 changed files with 24 additions and 4 deletions

View File

@ -104,19 +104,31 @@ public:
int PushToStack(lua_State * l) override; int PushToStack(lua_State * l) override;
}; };
class TickEvent: public Event class TickEvent : public Event
{ {
public: public:
int PushToStack(lua_State *l) override { return 0; } int PushToStack(lua_State *l) override { return 0; }
}; };
class BlurEvent: public Event class BlurEvent : public Event
{ {
public: public:
int PushToStack(lua_State *l) override { return 0; } int PushToStack(lua_State *l) override { return 0; }
}; };
class CloseEvent: public Event class CloseEvent : public Event
{
public:
int PushToStack(lua_State *l) override { return 0; }
};
class BeforeSimEvent : public Event
{
public:
int PushToStack(lua_State *l) override { return 0; }
};
class AfterSimEvent : public Event
{ {
public: public:
int PushToStack(lua_State *l) override { return 0; } int PushToStack(lua_State *l) override { return 0; }
@ -136,7 +148,9 @@ public:
mousewheel, mousewheel,
tick, tick,
blur, blur,
close close,
beforesim,
aftersim,
}; };
static int RegisterEventHook(lua_State *l, ByteString eventName); static int RegisterEventHook(lua_State *l, ByteString eventName);

View File

@ -4077,6 +4077,8 @@ void LuaScriptInterface::initEventAPI()
lua_pushinteger(l, LuaEvents::tick); lua_setfield(l, -2, "tick"); lua_pushinteger(l, LuaEvents::tick); lua_setfield(l, -2, "tick");
lua_pushinteger(l, LuaEvents::blur); lua_setfield(l, -2, "blur"); lua_pushinteger(l, LuaEvents::blur); lua_setfield(l, -2, "blur");
lua_pushinteger(l, LuaEvents::close); lua_setfield(l, -2, "close"); lua_pushinteger(l, LuaEvents::close); lua_setfield(l, -2, "close");
lua_pushinteger(l, LuaEvents::beforesim); lua_setfield(l, -2, "beforesim");
lua_pushinteger(l, LuaEvents::aftersim); lua_setfield(l, -2, "aftersim");
} }
int LuaScriptInterface::event_register(lua_State * l) int LuaScriptInterface::event_register(lua_State * l)

View File

@ -4988,6 +4988,8 @@ void Simulation::BeforeSim()
{ {
if (!sys_pause||framerender) if (!sys_pause||framerender)
{ {
luacon_ci->HandleEvent(LuaEvents::beforesim, new BeforeSimEvent());
air->update_air(); air->update_air();
if(aheat_enable) if(aheat_enable)
@ -5186,6 +5188,8 @@ void Simulation::AfterSim()
Element_EMP_Trigger(this, emp_trigger_count); Element_EMP_Trigger(this, emp_trigger_count);
emp_trigger_count = 0; emp_trigger_count = 0;
} }
luacon_ci->HandleEvent(LuaEvents::aftersim, new AfterSimEvent());
} }
Simulation::~Simulation() Simulation::~Simulation()