Entity properties and components¶
An entity keeps its data in components, and its properties read and write
that data. The components decide what an entity is: a part has transform,
mesh and collision components, a light has Light, a sound has Sound, and
an on-screen control has 2D layout components. See
Entity for every member.
-- Server script in a part.
local lamp = World.Spawn({})
print(lamp.Light) -- prints nil
lamp:Add(Component.Light)
local light = lamp.Light
if light then
light.Intensity = 2
light.Range = 20
end
lamp.Parent = self
print(lamp:Has(Component.Light)) -- prints true
print(lamp.Light and lamp.Light.Intensity) -- prints 2
World.Spawn accepts an initial component table; set components and properties before Parent.
Reading and writing components¶
- Index an entity with a component name such as
lamp.Lightorpart.Bodyto read or write that component's view, ornilwhen the entity does not have the component. See Component views for every view and field. - Core entity properties (
Name,Parent,Visible,Active,LocalPosition,LocalRotation, andMaterial) and single-value component fields (Position,Rotation,Size,Color, andShape) read and write directly on the entity; the optional component fields returnnilwhen the entity lacks that component. - Use
Entity:Findto look up child entities by name andEntity:Hasto test whether an entity has a component. - Call
Entity:AddorWorld.Spawnto add components to an entity. A 3D transform (Position,Rotation,Size) requiresComponent.Position(orComponent.Pivoton a model), and 2D interface layout requiresComponent.Box. - Call
Entity:Removeto remove a component or tag. Assigningnilto an optional field clears that field without removing its component.
Component names¶
Built-in component handles are in the Component namespace, such as
Component.Body, Component.Light, or Component.Lifetime, and are passed to
World.Query, Entity:Add, Entity:Has, and Entity:Remove.
3D world and 2D user interface¶
An entity is in either the 3D world or the 2D user interface, never both.
In the world, Position and Size are Vector3 values in units. In the interface, under World.Viewport.Screen or inside interface containers,
layout uses entity.Box (Position, Size), entity.UIOrder (ZIndex,
LayoutOrder), and interface views such as entity.UITextSource,
entity.UITextStyle, and entity.Background. Mixing world and interface
components on one entity fails with one of the
interface layout errors.