Instance System
The foundation of all game objects in StarForge.
Related API Classes
Understanding Instances
In StarForge, everything is an Instance. Parts, UI elements, scripts, systems - they all inherit from the base Instance class. This creates a unified, tree-structured hierarchy that's easy to work with and reason about.
The Instance Tree
Instances are organized in a parent-child relationship, forming a tree:
Engine ├── World │ ├── Part (Floor) │ ├── Part (Wall) │ └── Player │ ├── Part (Body) │ └── Script (Controller) ├── InputSystem ├── UILayer │ └── UIFrame (HUD) └── ...
Core Properties
Every Instance has these fundamental properties:
Name- A string identifier (not necessarily unique)Parent- Reference to the parent InstanceChildren- Array of child InstancesId- A unique identifier assigned by the engineArchivable- Whether the instance can be saved to files
Working with Instances
-- Create a new instance
local part = new("Part")
part.Name = "MyPart"
-- Set parent (adds to tree)
part.Parent = world
-- Access children
for _, child in ipairs(world.Children) do
print(child.Name)
end
-- Find by name
local floor = world:FindFirstChild("Floor")
-- Destroy (removes from tree)
part:Destroy()Events
Instances fire events when their state changes:
Changed(propertyName)- When any property changesChildAdded(child)- When a child is addedChildRemoved(child)- When a child is removedDescendantAdded(child, parent)- When any descendant is addedDescendantRemoved(child, parent)- When any descendant is removed
-- Listen for property changes
part.Changed:Connect(function(propName)
print(propName .. " changed!")
end)
-- Listen for new children
world.ChildAdded:Connect(function(child)
print("New object: " .. child.Name)
end)