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

Scriptsstd::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

Namestd::stringThe 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.
Fieldsstd::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 ScriptableEntity*The live object during Play. Null in Edit mode.
InstantiateScript / DestroyScript function pointersHow to build and tear down the instance. A hot reload clears them and rebuilds them from Name.

Methods

Bind(name)voidAttach a script by registered name (appends a slot).
Bind<T>()voidAttach a script by type, for code-built entities.
AddScript(name)intAdd a slot without binding it; returns its index (the existing one for a duplicate).
IndexOf(name)intSlot index of a script, or -1.
RemoveScript(index)voidDestroy 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.
A script's own member variables are never serialized — only each slot's 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
If the named script is not in the compiled module, the card says so and the entity runs nothing at Play. Press F6 to rebuild scripts — see the Scripts & IDE workflow.

See also

ScriptableEntity · ScriptFields · Native Scripting guide

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

Scriptsstd::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

Namestd::stringZarejestrowana 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.
Fieldsstd::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 ScriptableEntity*Żywy obiekt podczas Play. Null w trybie edycji.
InstantiateScript / DestroyScript wskaźniki funkcjiJak utworzyć i zniszczyć instancję. Hot reload je czyści i odbudowuje z Name.

Metody

Bind(name)voidPodpina skrypt po zarejestrowanej nazwie (dodaje slot).
Bind<T>()voidPodpina skrypt po typie — dla encji budowanych w kodzie.
AddScript(name)intDodaje slot bez podpinania; zwraca jego indeks (istniejący przy duplikacie).
IndexOf(name)intIndeks slotu skryptu albo -1.
RemoveScript(index)voidNiszczy 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.
Własne zmienne skryptu nigdy nie są zapisywane — tylko 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
Jeśli skryptu o tej nazwie nie ma w skompilowanym module, karta to pokazuje, a encja podczas Play nic nie uruchamia. Naciśnij F6, żeby przebudować skrypty — zob. Skrypty i IDE.

Zobacz też

ScriptableEntity · ScriptFields · Przewodnik po skryptach natywnych