From 63661a752c892c9a5b7d8cd1de2a0a6ab2507e44 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Mon, 19 Dec 2022 22:31:21 -0500 Subject: [PATCH] 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. --- src/lua/LuaEvents.h | 22 ++++++++++++++++++---- src/lua/LuaScriptInterface.cpp | 2 ++ src/simulation/Simulation.cpp | 4 ++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/lua/LuaEvents.h b/src/lua/LuaEvents.h index 9f31cfed6..c1436c161 100644 --- a/src/lua/LuaEvents.h +++ b/src/lua/LuaEvents.h @@ -104,19 +104,31 @@ public: int PushToStack(lua_State * l) override; }; -class TickEvent: public Event +class TickEvent : public Event { public: int PushToStack(lua_State *l) override { return 0; } }; -class BlurEvent: public Event +class BlurEvent : public Event { public: 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: int PushToStack(lua_State *l) override { return 0; } @@ -136,7 +148,9 @@ public: mousewheel, tick, blur, - close + close, + beforesim, + aftersim, }; static int RegisterEventHook(lua_State *l, ByteString eventName); diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index c3d2a16f4..0e2c8ba12 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -4077,6 +4077,8 @@ void LuaScriptInterface::initEventAPI() lua_pushinteger(l, LuaEvents::tick); lua_setfield(l, -2, "tick"); lua_pushinteger(l, LuaEvents::blur); lua_setfield(l, -2, "blur"); 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) diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index d8dc38741..a38e829d1 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -4988,6 +4988,8 @@ void Simulation::BeforeSim() { if (!sys_pause||framerender) { + luacon_ci->HandleEvent(LuaEvents::beforesim, new BeforeSimEvent()); + air->update_air(); if(aheat_enable) @@ -5186,6 +5188,8 @@ void Simulation::AfterSim() Element_EMP_Trigger(this, emp_trigger_count); emp_trigger_count = 0; } + + luacon_ci->HandleEvent(LuaEvents::aftersim, new AfterSimEvent()); } Simulation::~Simulation()