BuilderComponent struct in Thread

"This entity can build." Free-form placement in the Green Hell / Valheim sense — no grid, no sockets. The system casts a ray along this entity's forward axis, shows a ghost of the blueprint where it lands, lets the player turn it, and on commit turns that ghost into a real, saved entity.

Put it on whatever the player aims with: the camera entity, or the character carrying it. A blueprint is just a .prefab — there is no separate asset type.

Properties

EnabledboolMaster switch.
Blueprintstd::stringThe .prefab to place. Empty means nothing is selected, which is also how a game "puts the hammer away".
Settings.MaxDistancefloatHow far the player can place from themselves.
Settings.MaxSlopeDegreesfloatSteeper than this is a hillside, not a building site.
Settings.AlignToSurfaceboolOff (default) keeps the object upright on sloped ground, the way a shelter does not lean downhill. On makes it hug the surface — a rug, a sign nailed to a rock.
Settings.YawSnapDegreesfloat0 is free rotation, which is the point of this model. Set e.g. 15 for a little order without a grid.
BlockRadiusfloatRadius of the "is this spot free" test. Roughly the footprint of what is being placed: too small and buildings interpenetrate, too large and the player cannot build next to anything.
RotateSpeedfloatDegrees per second while a rotate key is held.
PlacedCategorystd::stringWritten into the placed entity's PersistentComponent.
ToggleKey / RotateLeftKey / RotateRightKeyKeyDefault B, Q, E.
PlaceButtonMouseDefault left button.
Build mode, the ghost's id and the current yaw are runtime state and are deliberately not saved. A scene that loaded with build mode already on, holding a stale entity id, would start by pointing at nothing.

What the player sees

The ghost is the interface. It is an instance of the blueprint, stripped of everything physical so it cannot collide with the world or with itself, outlined green where the spot is legal and red where it is not.

A refused ghost keeps following the aim. One that freezes on the last legal spot reads as the game having stopped responding; one that follows and turns red reads as "not here".

Why a spot is refused

No surfaceAimed at open sky, or past everything.
Too far awaySomething is there, but out of reach.
Ground is too steepSlope exceeds MaxSlopeDegrees.
Something is in the wayThe clearance test found an obstruction.
The surface being aimed at is never the obstruction. Building on top of a crate is legal, so the thing the ray landed on is excluded from the clearance test. Reporting the ground under the cursor as "something is in the way" would be the most confusing possible refusal.

Placement rules are separate from the scene

The decision — is this spot legal, what transform does the object get, where to test for clearance — lives in BuildPlacement.h as free functions over plain structs, with no knowledge of a scene, physics or rendering. BuildingSystem is the half that needs a scene: the raycast, the overlap query, the ghost entity and the commit.

Two of those rules are decided before the solver is called, and both have an obvious wrong version that makes the whole feature look broken while every rule is correct:

The probe must overshoot the reach. Casting exactly MaxDistance makes "too far" unreachable — a wall past your reach returns no hit at all, so the player is told No surface while looking straight at it.

The clearance test must lift off the surface. Centred on the hit point it is always touching the floor, so every spot on the map reports Something is in the way.

Notes

Everything placed is tagged PersistentComponent, which is what makes a built wall survive quitting.

The pivot goes on the surface with no height offset, so blueprints should be authored with their origin at the base. A mesh whose origin is its centre will sink halfway in.

BuildingSystem runs only while playing. A ghost following the editor camera around in Edit mode would be noise, and worse, an entity appearing in the outliner that the author never made. BuildingSystem::Place and ClearGhost are public so a game can drive placement from its own input, a UI button or a script instead of the bindings above.

BuilderComponent struct w Thread

„Ta encja może budować.” Swobodne stawianie w duchu Green Hell / Valheim — bez siatki, bez gniazd. System rzuca promień wzdłuż osi „do przodu” tej encji, pokazuje ducha projektu tam, gdzie trafi, pozwala go obracać, a po zatwierdzeniu zamienia ducha w prawdziwą, zapisywaną encję.

Umieść go na tym, czym gracz celuje: na encji kamery albo na postaci, która ją niesie. Projekt (blueprint) to po prostu .prefab — nie ma osobnego typu assetu.

Właściwości

EnabledboolGłówny przełącznik.
Blueprintstd::string.prefab do postawienia. Pusty znaczy, że nic nie wybrano — tak też gra „odkłada młotek”.
Settings.MaxDistancefloatJak daleko od siebie gracz może stawiać.
Settings.MaxSlopeDegreesfloatBardziej stromo to zbocze, nie plac budowy.
Settings.AlignToSurfaceboolWyłączone (domyślnie) trzyma obiekt pionowo na pochyłym gruncie — szałas nie pochyla się w dół zbocza. Włączone przykleja go do powierzchni — dywan, tabliczka przybita do skały.
Settings.YawSnapDegreesfloat0 to swobodny obrót, o który w tym modelu chodzi. Ustaw np. 15 dla odrobiny porządku bez siatki.
BlockRadiusfloatPromień testu „czy to miejsce jest wolne”. Mniej więcej obrys stawianego obiektu: za mały — budynki się przenikają, za duży — nie da się budować obok czegokolwiek.
RotateSpeedfloatStopnie na sekundę przy trzymaniu klawisza obrotu.
PlacedCategorystd::stringWpisywana do PersistentComponent postawionej encji.
ToggleKey / RotateLeftKey / RotateRightKeyKeyDomyślnie B, Q, E.
PlaceButtonMouseDomyślnie lewy przycisk.
Tryb budowania, id ducha i bieżący obrót to stan czasu działania i celowo nie są zapisywane. Scena wczytana z włączonym trybem budowania i nieaktualnym id encji zaczynałaby od wskazywania na nic.

Co widzi gracz

Duch jest interfejsem. To instancja projektu pozbawiona wszystkiego, co fizyczne, żeby nie kolidowała ze światem ani sama ze sobą, z obrysem zielonym, gdzie miejsce jest dozwolone, i czerwonym, gdzie nie.

Odrzucony duch dalej podąża za celownikiem. Duch, który zamarza w ostatnim dozwolonym miejscu, wygląda jak gra, która przestała reagować; taki, który podąża i robi się czerwony, mówi „nie tutaj”.

Dlaczego miejsce jest odrzucone

Brak powierzchniCelowanie w niebo albo poza wszystko.
Za dalekoCoś tam jest, ale poza zasięgiem.
Grunt zbyt stromyNachylenie przekracza MaxSlopeDegrees.
Coś przeszkadzaTest prześwitu znalazł przeszkodę.
Powierzchnia, w którą celujesz, nigdy nie jest przeszkodą. Budowanie na skrzyni jest dozwolone, więc to, w co trafił promień, jest wyłączone z testu prześwitu. Zgłoszenie ziemi pod kursorem jako „coś przeszkadza” byłoby najbardziej mylącą możliwą odmową.

Reguły stawiania są oddzielone od sceny

Decyzja — czy miejsce jest dozwolone, jaką transformację dostaje obiekt, gdzie testować prześwit — żyje w BuildPlacement.h jako wolne funkcje na zwykłych strukturach, bez wiedzy o scenie, fizyce czy renderowaniu. BuildingSystem to część, która potrzebuje sceny: raycast, zapytanie o nakładanie, encja ducha i zatwierdzenie.

Dwie z tych reguł rozstrzyga się przed wywołaniem solvera, i obie mają oczywistą błędną wersję, przez którą cała funkcja wygląda na zepsutą, choć każda reguła jest poprawna:

Promień musi sięgać dalej niż zasięg. Rzut na dokładnie MaxDistance czyni „za daleko” nieosiągalnym — ściana poza zasięgiem w ogóle nie daje trafienia, więc gracz słyszy Brak powierzchni, patrząc prosto na nią.

Test prześwitu musi być uniesiony nad powierzchnię. Wyśrodkowany w punkcie trafienia zawsze dotyka podłogi, więc każde miejsce na mapie zgłasza Coś przeszkadza.

Uwagi

Wszystko postawione dostaje PersistentComponent — dlatego postawiona ściana przetrwa wyjście z gry.

Pivot trafia na powierzchnię bez przesunięcia w pionie, więc projekty powinny mieć początek układu u podstawy. Siatka z początkiem w środku zapadnie się do połowy.

BuildingSystem działa tylko w trakcie gry. Duch chodzący za kamerą edytora w trybie edycji byłby szumem, a co gorsza encją w outlinerze, której autor nigdy nie zrobił. BuildingSystem::Place i ClearGhost są publiczne, więc gra może sterować stawianiem z własnego wejścia, przycisku UI albo skryptu zamiast powyższych skrótów.