VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
scriptComponent.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers 02.01.2026
5//
6#include "scriptComponent.h"
7#include <framework/entity.h>
9
10namespace visutwin::canvas
11{
12 Script* ScriptComponent::create(const std::string& name, const ScriptCreateOptions& options)
13 {
14 // 1. Enforce uniqueness (one instance per type)
15 if (_scriptsIndex.contains(name)) {
16 return nullptr;
17 }
18
19 // 2. Create an instance via registry
20 auto* engine = _entity ? _entity->engine() : nullptr;
21 if (!engine || !engine->scripts()) {
22 return nullptr;
23 }
24
25 auto instance = engine->scripts()->create(name);
26 if (!instance) {
27 return nullptr;
28 }
29
30 Script* script = instance.get();
31
32 // 3. Bind ownership context
33 script->_entity = _entity;
34 script->_enabled = options.enabled;
35
36 // 4. Store the script instance
37 _scripts.push_back({ name, std::move(instance) });
38 _scriptsIndex[name] = _scripts.size() - 1;
39
40 // 5. Initialize attributes / reflected fields (hook point)
41 // initializeAttributes(raw);
42
43 // 6. Initialize lifecycle for non-preloading path only.
44 if (!options.preloading) {
45 initializeScriptInstance(script);
46 }
47
48 return script;
49 }
50
51 void ScriptComponent::setEnabled(const bool value)
52 {
53 const bool oldValue = _enabled;
54
55 // Call base to store value and fire onSetEnabled lifecycle.
57
58 // Mirror JS behavior: transitioning to enabled should initialize scripts
59 // that were created disabled/preloading and now became active.
60 if (!oldValue && value) {
61 for (auto& entry : _scripts) {
62 initializeScriptInstance(entry.instance.get());
63 }
64 }
65 }
66
67 void ScriptComponent::initializeScriptInstance(Script* script)
68 {
69 if (!script) {
70 return;
71 }
72
73 if (!_enabled || !script->enabled()) {
74 return;
75 }
76
77 if (!script->_initialized) {
78 script->_initialized = true;
79 script->initialize();
80 }
81
82 if (!script->_postInitialized) {
83 script->_postInitialized = true;
84 script->postInitialize();
85 }
86 }
87
88 void ScriptComponent::fixedUpdateScripts(const float fixedDt)
89 {
90 if (!_enabled) {
91 return;
92 }
93
94 for (auto& entry : _scripts) {
95 auto* script = entry.instance.get();
96 if (!script || !script->_initialized || !script->enabled()) {
97 continue;
98 }
99 script->fixedUpdate(fixedDt);
100 }
101 }
102
104 {
105 if (!_enabled) {
106 return;
107 }
108
109 for (auto& entry : _scripts) {
110 auto* script = entry.instance.get();
111 if (!script || !script->_initialized || !script->enabled()) {
112 continue;
113 }
114 script->update(dt);
115 }
116 }
117
119 {
120 if (!_enabled) {
121 return;
122 }
123
124 for (auto& entry : _scripts) {
125 auto* script = entry.instance.get();
126 if (!script || !script->_initialized || !script->enabled()) {
127 continue;
128 }
129 script->postUpdate(dt);
130 }
131 }
132}
virtual void setEnabled(bool value)
Definition component.cpp:21
void setEnabled(bool value) override
virtual void fixedUpdate(float fixedDt)
Definition script.h:56
virtual void postUpdate(float dt)
Definition script.h:66
virtual void postInitialize()
Definition script.h:50
bool enabled() const
Definition script.cpp:13
virtual void initialize()
Definition script.h:44
virtual void update(float dt)
Definition script.h:61