Module
@ember/entities
Entities a script spawns. They live until removed, or until the instance closes; they are never saved. Every option typed Shared may be a value or a function, and the host redraws only that part when what it read changes.
local entities = require("@ember/entities")Functions
The entities this script spawned that are still alive.
local commands = require("@ember/commands")
local entities = require("@ember/entities")
-- Every entity this script spawned that is still alive, whatever spawned it. A dev command that
-- clears them all, handy while testing.
commands.register("clearentities", {
description = "Remove every entity the script spawned",
dev = true,
run = function(player)
local removed = 0
for _, entity in entities.all() do
entity:remove()
removed += 1
end
player:send(`<gray>Removed {removed} entities.`)
end,
})entities.block(options: entities.BlockOptions) -> types.EntitySpawns a block display.
local entities = require("@ember/entities")
local ui = require("@ember/ui")
local world = require("@ember/world")
-- A block display draws a block without placing one: nothing can break it or walk into it. Here a
-- glowing beacon block marks the objective, and its colour shows who holds it.
local holder = ui.value("none" :: "none" | "red" | "blue")
entities.block({
at = world.markers.single("objective").position,
block = "minecraft:beacon",
scale = 0.5,
glow = function()
local team = holder:get()
return if team == "red" then 0xFF5555 elseif team == "blue" then 0x5555FF else nil
end,
})
world.regions.single("red_capture").entered:connect(function()
holder:set("red")
end)
world.regions.single("blue_capture").entered:connect(function()
holder:set("blue")
end)entities.hitbox(options: entities.HitboxOptions) -> types.EntitySpawns an invisible box players can click.
local entities = require("@ember/entities")
local world = require("@ember/world")
-- An invisible click zone, here around a statue that is part of the map. Right-clicking it fires
-- `clicked`; hitting it fires `attacked`.
local statue = entities.hitbox({ at = world.markers.single("statue").position, width = 1.5, height = 3 })
statue.clicked:connect(function(event)
event.player:send("<light_purple>The statue whispers: the key is under the bridge.")
end)
statue.attacked:connect(function(event)
event.player:send("<gray>Ouch. Statues don't like that.")
end)entities.item(options: entities.ItemOptions) -> types.EntitySpawns an item display: an item, or a content.model drawn on its own.
local entities = require("@ember/entities")
local items = require("@ember/items")
local world = require("@ember/world")
-- A coin floating over every "coin" marker. Clicking it collects it; it comes back 10 seconds later.
local function placeCoin(position: vector)
local coin = entities.item({
at = position,
item = "coin",
scale = 0.8,
billboard = "vertical",
glow = 0xFFD700,
hitbox = { width = 0.8, height = 0.8 },
})
coin.clicked:connect(function(event)
-- Two players can click it in the same tick; only the first gets it.
if not coin.alive then
return
end
coin:remove()
event.player.inventory:add(items.create("coin"))
task.delay(10, placeCoin, position)
end)
end
for _, marker in world.markers.get("coin") do
placeCoin(marker.position)
endentities.spawn(mob: string | types.MobType, options: entities.SpawnOptions) -> types.EntitySpawns a mob declared with content.mob and persistent = false; raises for any other.
local entities = require("@ember/entities")
local world = require("@ember/world")
-- A shopkeeper NPC. In content.luau:
--
-- content.mob("shopkeeper", { look = "minecraft:villager", name = "<gold>Shopkeeper", persistent = false })
--
-- Scripts can only spawn mobs declared `persistent = false`. Mobs have no AI: they stand still
-- until moved with `teleport` or `follow`.
local marker = world.markers.single("shop")
local keeper = entities.spawn("shopkeeper", { at = marker.position, look = { yaw = marker.yaw or 0, pitch = 0 } })
keeper.clicked:connect(function(event)
event.player:send("<gold>Shopkeeper: <white>Fresh apples, one coin each!")
end)entities.text(options: entities.TextOptions) -> types.EntitySpawns a text display.
local data = require("@ember/data")
local entities = require("@ember/entities")
local ui = require("@ember/ui")
local world = require("@ember/world")
-- A floating leaderboard over the "board" marker. `text` may be a value, or a function: the
-- display redraws when the value it read changes.
local wins = data.leaderboard("wins")
local lines = ui.value("<gray>Loading...")
entities.text({
at = world.markers.single("board").position,
text = lines,
background = true,
})
-- Refresh it every half minute.
task.spawn(function()
while true do
local text = "<gold><bold>Top Winners</bold>"
for _, entry in wins:top(5) do
text ..= `\n<yellow>#{entry.rank} <white>{entry.name} <gray>{entry.score}`
end
lines:set(text)
task.wait(30)
end
end)Types
- entities.BillboardHow a display turns toward the viewer: not at all, about the vertical axis only, about the horizontal axis only, or fully.
- entities.BlockOptionsA block display.
- entities.HitboxAn invisible box at
at, centred on x and z and rising from it, that makes a display clickable. - entities.HitboxOptionsAn invisible click zone.
- entities.ItemOptionsAn item display.
- entities.MobTypeA mob type from
content.mob. - entities.SharedA constant, a value, or a function that runs again when what it read changes.
- entities.SpawnOptionsWhere a mob spawns.
- entities.TextOptionsA text display.