aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/Script.h
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-16 16:46:18 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-16 16:46:18 +0100
commitfc5dfd2873279d5003f8a02187a71b05a44892fa (patch)
treee36d40a1b26e9d4900b582c5f06c4d1dc2858d34 /src/crepe/api/Script.h
parent572741fc845f430d225a4611a94aae7a2a544d12 (diff)
final touchups 2 for #26
Diffstat (limited to 'src/crepe/api/Script.h')
-rw-r--r--src/crepe/api/Script.h44
1 files changed, 24 insertions, 20 deletions
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index 12ec7f1..1435581 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -2,6 +2,8 @@
#include <vector>
+#include "../types.h"
+
namespace crepe {
class ScriptSystem;
@@ -11,32 +13,31 @@ class ComponentManager;
/**
* \brief Script interface
*
- * This class is used as a base class for user-defined scripts that can be
- * added to game objects using the \c BehaviorScript component.
+ * This class is used as a base class for user-defined scripts that can be added to game
+ * objects using the \c BehaviorScript component.
+ *
+ * \note Additional *events* (like Unity's OnDisable and OnEnable) should be implemented as
+ * member or lambda methods in derivative user script classes and registered in \c init().
*/
class Script {
- //! ScriptSystem calls \c update()
- friend class crepe::ScriptSystem;
-
protected:
/**
* \brief Script initialization function
*
* This function is called during the ScriptSystem::update() routine *before*
- * Script::update() if it (a) has not yet been called and (b) the \c
- * BehaviorScript component holding this script instance is active.
+ * Script::update() if it (a) has not yet been called and (b) the \c BehaviorScript component
+ * holding this script instance is active.
*/
virtual void init() {}
/**
* \brief Script update function
*
- * This function is called during the ScriptSystem::update() routine if the
- * \c BehaviorScript component holding this script instance is active.
+ * This function is called during the ScriptSystem::update() routine if the \c BehaviorScript
+ * component holding this script instance is active.
*/
virtual void update() {}
- // NOTE: additional *events* (like unity's OnDisable and OnEnable) should be implemented as
- // member methods in derivative user script classes and registered in init(), otherwise this
- // class will balloon in size with each added event.
+ //! ScriptSystem calls \c init() and \c update()
+ friend class crepe::ScriptSystem;
protected:
/**
@@ -46,8 +47,7 @@ protected:
*
* \returns Reference to component
*
- * \throws nullptr if this game object does not have a component matching
- * type \c T
+ * \throws nullptr if this game object does not have a component matching type \c T
*/
template <typename T>
T & get_component() const;
@@ -63,18 +63,22 @@ protected:
std::vector<std::reference_wrapper<T>> get_components() const;
protected:
- // NOTE: Script must have a constructor without arguments so the game
- // programmer doesn't need to manually add `using Script::Script` to their
- // concrete script class.
+ // NOTE: Script must have a constructor without arguments so the game programmer doesn't need
+ // to manually add `using Script::Script` to their concrete script class.
Script() = default;
//! Only \c BehaviorScript instantiates Script
friend class BehaviorScript;
private:
- // These references are set by BehaviorScript immediately after calling the
- // constructor of Script.
- BehaviorScript * parent_ref = nullptr;
+ // These references are set by BehaviorScript immediately after calling the constructor of
+ // Script.
+ game_object_id_t game_object_id = -1;
ComponentManager * component_manager_ref = nullptr;
+ // TODO: use OptionalRef instead of pointer
+
+private:
+ //! Flag to indicate if \c init() has been called already
+ bool initialized = false;
};
} // namespace crepe