Scene¶
fr::Scene is the central orchestrator of Freyr. It provides the full entity lifecycle API, component operations, iteration methods, event helpers, and the main update loop.
Obtain an instance from the service provider:
class MyApp : public skr::IApplication {
public:
explicit MyApp(const Ref<skr::ServiceProvider>& sp) : IApplication(sp) {
mScene = sp->GetService<fr::Scene>();
}
};
Entity management¶
CreateEntity¶
// Create with components, returns entity ID
Entity CreateEntity(const Ts&... components);
// Create with components and receive the ID via callback
void CreateEntity(TFunc&& callback, const Ts&... components);
// No components
fr::Entity e = scene->CreateEntity();
// With initial components
fr::Entity e = scene->CreateEntity(Position { .x = 10.f }, Velocity {});
// Via callback
scene->CreateEntity([](fr::Entity e) {
// e is available here immediately
}, Position {}, Velocity {});
DestroyEntity¶
Destruction is deferred — the entity is added to a pending set and removed at the end of the current Update call.
Component operations¶
AddComponent<T>¶
Adds a single component to an existing entity. If the entity does not yet belong to an archetype that includes T, it is migrated to the correct one.
AddComponents<Ts...>¶
Adds multiple components in one call. More efficient than calling AddComponent repeatedly.
RemoveComponent<T>¶
Removes a component and migrates the entity to the appropriate archetype.
HasComponent<T> / HasComponents<Ts...>¶
bool HasComponent<T>(const Entity& entity) const;
bool HasComponents<Ts...>(const Entity& entity) const;
TryGetComponents<Ts...>¶
Invokes callback(Ts&...) only if the entity has all requested components. Returns true on success.
bool found = scene->TryGetComponents<Position, Health>(entity,
[](Position& pos, Health& hp) {
pos.x += 1.f;
hp.current -= 5;
});
Queries¶
CreateQuery¶
Ref<Query> CreateQuery() const;
template <typename TQuery> requires(std::is_base_of_v<Query, TQuery>)
Ref<TQuery> CreateQuery();
Creates a new Query instance for entity searching. Query instances should not be stored long-term as they hold references to ComponentManager.
auto query = scene->CreateQuery();
auto players = query->Including<PlayerTag, Health>()
->EntitiesWith<PlayerTag, Health>();
See the Query reference for the full fluent query API.
Event helpers¶
AddEventListener<T>¶
Subscribes to event type T. The subscription is alive as long as the returned ListenerHandle is kept alive.
SendEvent<T>¶
Publishes an event immediately to all active subscribers.
Update loop¶
Update(deltaTime)¶
Drives the full update cycle:
PreUpdate→ all systems across all pipelinesExecuteTasks()— flush pending async tasksDestroyEntities()— process deferred destruction queueUpdate→ all systems across all pipelines (ForEach / ForEachAsync live here)ExecuteTasks()— flush pending async tasksDestroyEntities()— process deferred destruction queuePostUpdate→ all systems across all pipelinesExecuteTasks()— flush pending async tasksDestroyEntities()— process deferred destruction queue
ExecuteTasks()¶
Blocks until all tasks dispatched via ForEachAsync have completed. Called automatically at the end of Update, but can be called manually to create explicit sync points.
Archetype builder¶
Returns an ArchetypeBuilder for efficient bulk entity creation. See the ArchetypeBuilder reference.
Profiling¶
void BeginProfiling();
void EndProfiling() const; // flushes trace to disk
void BeginTrace(const char* label);
void EndTrace();
See the Profiling guide.