From 502fb8e8d1dcfe10f55fdef2cdfb71afec806204 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 21 Nov 2024 09:43:13 +0100 Subject: pull script/event changes from `loek/collision-system` --- src/crepe/api/Script.hpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src/crepe/api/Script.hpp') diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index 42c8f0b..8186bd4 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -22,7 +22,7 @@ template std::vector> Script::get_components() const { ComponentManager & mgr = *this->component_manager_ref; - return mgr.get_components_by_id(this->game_object_id); + return mgr.get_components_by_id(*this->game_object_id_ref); } template @@ -31,10 +31,24 @@ void Script::logf(Args &&... args) { } template -void Script::subscribe(const EventHandler & callback, event_channel_t channel) { +void Script::subscribe_internal(const EventHandler & callback, event_channel_t channel) { EventManager & mgr = *this->event_manager_ref; - subscription_t listener = mgr.subscribe(callback, channel); + subscription_t listener = mgr.subscribe([this, callback](const EventType & data) -> bool { + bool & active = *this->active_ref; + if (!active) return false; + return callback(data); + }, channel); this->listeners.push_back(listener); } +template +void Script::subscribe(const EventHandler & callback, event_channel_t channel) { + this->subscribe_internal(callback, channel); +} + +template +void Script::subscribe(const EventHandler & callback) { + this->subscribe_internal(callback, EventManager::CHANNEL_ALL); +} + } // namespace crepe -- cgit v1.2.3