NativeScriptComponent struct in Thread
Runs ScriptableEntity subclasses on this entity — any number of them, one slot each. Shown in the inspector as Native Script. Each slot stores which script and what it was authored with; the instances themselves are created when the scene starts playing and destroyed when it stops.
Properties
| Scripts | std::vector<ScriptSlot> | Every script on the entity, in order. Adding a name that is already there is a no-op: two instances of one script would share one field map and fight over it. |
ScriptSlot
| Name | std::string | The registered class name, e.g. "Player". This is what the scene file stores and what binds the slot to a script. Empty for a script bound by type in code. |
| Fields | std::map<std::string, ScriptFieldValue> | Values authored for this script on this entity — see ScriptFields. Serialized with the scene. Entries the script no longer declares are kept, so renaming a field in code does not delete the authored value. |
| Instance runtime | ScriptableEntity* | The live object during Play. Null in Edit mode. |
| InstantiateScript / DestroyScript runtime | function pointers | How to build and tear down the instance. A hot reload clears them and rebuilds them from Name. |
Methods
| Bind(name) | void | Attach a script by registered name (appends a slot). |
| Bind<T>() | void | Attach a script by type, for code-built entities. |
| AddScript(name) | int | Add a slot without binding it; returns its index (the existing one for a duplicate). |
| IndexOf(name) | int | Slot index of a script, or -1. |
| RemoveScript(index) | void | Destroy one slot's instance and remove it. Always use this rather than erasing from Scripts: it renumbers the live instances that follow, which read their fields through their slot index. |
| HasAnyScript() / FirstName() | bool / const std::string& | For code that means "the script on this entity" — reads slot 0, safe on an empty component. |
Name and Fields are. Every member comes back default-constructed on
load, which is exactly why serialized fields exist.Binding a script
In the editor: Add Component ▸ Scripting ▸ Native Script, then pick the script — and pick again to add a second one. Dragging a script header from the Content Browser onto an entity does the same thing.
In code, the name is the binding:
auto& nsc = entity.AddComponent<Thread::NativeScriptComponent>();
nsc.Bind("Player");
nsc.Bind("Inventory"); // a second script on the same entity
See also
NativeScriptComponent struct w Thread
Uruchamia na tej encji podklasy ScriptableEntity — dowolnie wiele, każdą w osobnym slocie. W inspektorze widoczny jako Native Script. Każdy slot pamięta, który to skrypt i z jakimi wartościami go ustawiono; same instancje powstają, gdy scena zaczyna grać, i znikają, gdy przestaje.
Właściwości
| Scripts | std::vector<ScriptSlot> | Wszystkie skrypty encji, po kolei. Dodanie nazwy, która już jest, nic nie robi: dwie instancje jednego skryptu dzieliłyby jedną mapę pól i walczyły o nią. |
ScriptSlot
| Name | std::string | Zarejestrowana nazwa klasy, np. "Player". To ją zapisuje plik sceny i to ona wiąże slot ze skryptem. Pusta dla skryptu podpiętego w kodzie przez typ. |
| Fields | std::map<std::string, ScriptFieldValue> | Wartości ustawione dla tego skryptu na tej encji — zob. ScriptFields. Zapisywane ze sceną. Wpisy, których skrypt już nie deklaruje, zostają, więc zmiana nazwy pola w kodzie nie kasuje ustawionej wartości. |
| Instance runtime | ScriptableEntity* | Żywy obiekt podczas Play. Null w trybie edycji. |
| InstantiateScript / DestroyScript runtime | wskaźniki funkcji | Jak utworzyć i zniszczyć instancję. Hot reload je czyści i odbudowuje z Name. |
Metody
| Bind(name) | void | Podpina skrypt po zarejestrowanej nazwie (dodaje slot). |
| Bind<T>() | void | Podpina skrypt po typie — dla encji budowanych w kodzie. |
| AddScript(name) | int | Dodaje slot bez podpinania; zwraca jego indeks (istniejący przy duplikacie). |
| IndexOf(name) | int | Indeks slotu skryptu albo -1. |
| RemoveScript(index) | void | Niszczy instancję slotu i go usuwa. Zawsze używaj tego zamiast usuwania z Scripts: przenumerowuje kolejne żywe instancje, które czytają swoje pola przez indeks slotu. |
| HasAnyScript() / FirstName() | bool / const std::string& | Dla kodu, który ma na myśli „skrypt tej encji” — czyta slot 0, bezpieczne na pustym komponencie. |
Name i Fields każdego slotu. Każde pole wraca po wczytaniu z wartością
domyślną i właśnie dlatego istnieją pola serializowane.Podpinanie skryptu
W edytorze: Add Component ▸ Scripting ▸ Native Script, potem wybierz skrypt — i wybierz jeszcze raz, żeby dodać drugi. Przeciągnięcie nagłówka skryptu z Content Browsera na encję robi to samo.
W kodzie wiązaniem jest nazwa:
auto& nsc = entity.AddComponent<Thread::NativeScriptComponent>();
nsc.Bind("Player");
nsc.Bind("Inventory"); // drugi skrypt na tej samej encji
Zobacz też
ScriptableEntity · ScriptFields · Przewodnik po skryptach natywnych