Reading time: ~30 minutes
Table of Contents
- What are Scenes?
- Scene from Roblox Hierarchy
- Scene Structure
- Creating Scenes Programmatically
- Loading Scenes
- Scene Serialization
- Best Practices
What are Scenes?
A scene is a container for GameObjects and represents a level, menu, or any other distinct state in your game. The recommended approach is to define scenes using Roblox Studio’s instance hierarchy, though programmatic definition is also supported.Key Features
- Roblox Hierarchy: Build scenes directly in Studio using Actors and Attributes (recommended)
- Declarative Structure: Define your game world in code or JSON
- Serialization: Save and load scenes
- Hierarchy Support: Organize GameObjects in parent-child relationships
- Component Configuration: Pre-configure component properties
- Runtime Flexibility: Load scenes dynamically
Scene from Roblox Hierarchy
The primary and recommended way to create scenes is using Roblox Studio’s instance hierarchy. CORP can parse Actor instances in the workspace and convert them to GameObjects with components.Setting Up a Scene in Studio
- Create Actors: Each Actor in Workspace becomes a GameObject
- Add Attributes: Configure component properties using Roblox attributes
- Use Folders: Organize your scene structure
- Add Components: Use ModuleScripts or StringValues to specify components
Example Studio Structure
Loading from Workspace
Component Mapping in Hierarchy
Components are specified using attributes or child instances:Note: Component names should usesnake_case. Components prefixed with$are core CORP components (e.g.,$network_object).
Scene Structure
When defining scenes programmatically, they follow theSceneDescription interface:
Creating Scenes Programmatically
Basic Scene
Create a simple scene with a few GameObjects:Scene with Hierarchy
Create GameObjects with parent-child relationships:Scene with Transforms
Specify initial positions for GameObjects:Scene with Network Objects
Create a scene with networked GameObjects:Loading Scenes
Using SceneManager
TheSceneManager class handles scene loading and management:
Starting with CORP
UseCORP.start() to initialize your game with a starting scene:
Scene Events
Listen for scene changes:Scene Serialization
Exporting Scenes
Generate an Instance representation of the current scene:Importing from JSON
Scenes can be stored as JSON and loaded at runtime:Component Path Format
Component paths use theExportPath type:
Note:ExportPathis defined as[string, string] | [string], representing either[file path, export name]or[file path]for default exports.
Configuring Components
Pass initial data to components in scenes:Best Practices
1. Use Roblox Hierarchy (Recommended)
Prefer defining scenes in Roblox Studio using the instance hierarchy. This approach provides:- Visual editing in Studio
- Easier iteration and testing
- Natural integration with Roblox’s tooling
- Faster prototyping
- Procedural generation
- Dynamic content
- Runtime scene construction
2. Organize Scenes by Purpose
2. Use Scene Hierarchy
Group related GameObjects:3. Separate Configuration
Keep configuration separate from scene structure:4. Choose the Right Format
For most cases, use Roblox hierarchy in Studio. Use TypeScript/JSON for:- Procedural content generation
- Runtime dynamic scenes
- Complex scripted setups
- Get type safety and IntelliSense
- Enable refactoring support
- Use import statements for organization
5. Scene Transitions
Handle scene transitions gracefully:6. Dynamic Scene Loading
Load scenes based on game state:Common Patterns
Procedural Scene Generation
Generate scenes programmatically:Scene Prefabs
Create reusable GameObject descriptions:Next Steps
- Decorators: Learn about component decorators
- Advanced Topics: Explore advanced patterns
- Examples: See complete scene examples
Master scene management to organize your game world effectively! 🎬