Survival and economy
The Survival plugin's base building with a placement ghost, validation, costs and demolish, and the currency economy with wallets, shops and restocking.
The Survival & Economy plugin adds player base building and a trade economy. It depends on the Combat plugin, which it uses for inventories, crafting stations, utility nodes and zones. Enable both in the Plugins window. Engine/Plugins/Survival/Survival.mplugin
Buildables
A .buildable asset describes something a player can place.
Buildable "wooden_wall" {
schema_version: 1
name_text: "Wooden Wall"
category: "structure"
actor_class: "AStaticMeshActor"
mesh_type: "cube"
footprint: Vec3(1, 1.5, 0.15)
cell_size: 2
snap_mode: "grid"
max_slope: 20
Surface "floor" {}
Cost "wood" {
count: 10
}
}| Key | Meaning |
|---|---|
actor_class | The reflected actor class spawned on placement |
mesh_type, model_path | The mesh for the ghost and the placed object |
footprint | Half extents; rotated with the preview |
snap_mode, cell_size | grid, free or surface snapping |
max_slope | How far the floor may tilt |
Surface | Allowed surfaces: floor, wall, ceiling or any |
Cost | Items consumed on placement and partly refunded on demolish |
A buildable can also turn its placement into a utility node (utility_node and the utility_* keys), a crafting station (station_tag, station_radius, station_requires_power) or a zone (zone_id, zone_half_extents).
Build mode
FBuildingSystem drives a placement ghost along the player's aim, validates it every frame, and commits it against the builder's inventory.
| Function | Effect |
|---|---|
SetCatalog(ids) | The buildables offered; without one, every .buildable in the project |
EnterBuildMode(builder, id), ExitBuildMode() | Start and stop placing |
RotatePreview(steps), CycleBuildable(direction) | Turn the ghost in 90° steps, or move to the next affordable buildable |
CommitPlacement() | Place; returns the new entity |
EnterDemolishMode(builder), CommitDemolish() | Remove a placement, refunding SetRefundFraction of its cost (default half) |
Placement fails with a reason: aim (nothing hit), surface, slope, no_build (inside a no-build zone), overlap or cost. IsPreviewValid() and GetPreviewFailReason() report it, and OnBuildPreviewChanged fires when it changes, so the ghost and HUD can react. Costs are consumed exactly once and rolled back in full if anything fails. Placed objects carry a Building component and a PersistentState component, so they are saved with the world.
The template characters get build-mode input from the plugin through the actions Build, BuildRotate, BuildCycle, BuildPlace and BuildCancel. While a ghost is up, those inputs belong to build mode and firing is stood down.
From Visual Scripting, MBuildingStatics offers EnterBuildMode, ExitBuildMode, IsInBuildMode, GetActiveBuildable, IsPreviewValid and CountPlaced.
Economy
An Economy component is a wallet holding any number of currencies by id; primaryCurrency (default credits) is the one the HUD shows.
FEconomySystem moves money: Grant, Spend, Transfer, GetBalance, CanAfford, Buy and Sell. Every operation is validated in full before anything changes, so a transfer never debits one wallet without crediting the other, and a purchase never takes money for goods that will not fit. A refusal returns an EEconomyResult reason, such as InsufficientFunds, OutOfStock, NoInventorySpace or OutOfRange. Accepted moves are kept in a short transaction log on the wallet.
Shops
A vendor is a Shop component naming a .shop asset.
Shop "outpost" {
schema_version: 1
name_text: "Outpost Trader"
currency: "credits"
sell_back_fraction: 0.5
buys_from_player: true
Entry "lantern" {
stock: 2
price: 100
restock_seconds: 60
restock_amount: 1
max_stock: 2
}
Entry "rope" {
stock: -1
}
}A stock of -1 is unlimited. An entry with no price uses the item's value. The shop buys back at sell_back_fraction of the price when buys_from_player is on. In multiplayer, purchases go through the server.
From Visual Scripting, MEconomyStatics offers GetBalance, GetPrimaryBalance, CanAfford, Grant, Spend, GetShopStock, GetShopPrice, BuyFromNearestShop, SellToNearestShop and GetLastResult.
Related
Engine sources
This page describes behavior implemented in these files of the engine repository, checked against commit 5922e6e5e.
- Engine/Plugins/Survival/Survival.mplugin