aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/Script.h
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-20 22:18:54 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-20 22:18:54 +0100
commitda379a58033c0ef3c9c854326a3fca25d6e54319 (patch)
treed37fe99ce6abe3190d5f41fb017de2c55f076ab1 /src/crepe/api/Script.h
parentdff52c72a9199ceea4eaf67c270916ae11238b45 (diff)
add Script::subscribe
Diffstat (limited to 'src/crepe/api/Script.h')
-rw-r--r--src/crepe/api/Script.h36
1 files changed, 33 insertions, 3 deletions
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index ddb499c..c9eb211 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -4,6 +4,8 @@
#include "../types.h"
+#include "EventManager.h"
+
namespace crepe {
class ScriptSystem;
@@ -85,6 +87,14 @@ protected:
template <typename... Args>
void logf(Args &&... args);
+ /**
+ * \brief Subscribe to an event
+ *
+ * \see EventManager::subscribe
+ */
+ template <typename EventType>
+ void subscribe(const EventHandler<EventType> & callback, event_channel_t channel = EventManager::CHANNEL_ALL);
+
//! \}
protected:
@@ -93,17 +103,37 @@ protected:
Script() = default;
//! Only \c BehaviorScript instantiates Script
friend class BehaviorScript;
+public:
+ // std::unique_ptr destroys script
+ virtual ~Script();
+
+ Script(const Script &) = delete;
+ Script(Script &&) = delete;
+ Script & operator=(const Script &) = delete;
+ Script & operator=(Script &&) = delete;
private:
- // These references are set by BehaviorScript immediately after calling the constructor of
- // Script.
+ /**
+ * \name Late references
+ *
+ * These references are set by BehaviorScript immediately after calling the constructor of
+ * Script.
+ *
+ * \{
+ */
+ //! Game object ID of game object parent BehaviorScript is attached to
game_object_id_t game_object_id = -1;
+ //! Reference to component manager instance
ComponentManager * component_manager_ref = nullptr;
- // TODO: use OptionalRef instead of pointer
+ //! Reference to event manager instance
+ EventManager * event_manager_ref = nullptr;
+ //! \}
private:
//! Flag to indicate if \c init() has been called already
bool initialized = false;
+ //! List of subscribed events
+ std::vector<subscription_t> listeners;
};
} // namespace crepe