pub struct GameWorld { /* private fields */ }Expand description
A high-level facade over the internal ECS World and Assets registry.
GameWorld is the primary interface for game developers to create and
manage entities, components, and assets. It hides the raw types from
khora-data behind a clean, stable API surface.
§Examples
use khora_sdk::GameWorld;
use khora_sdk::prelude::ecs::{Camera, GlobalTransform, Name, Transform};
use khora_sdk::prelude::math::Vec3;
let mut world = GameWorld::new();
// Spawn a camera.
world.spawn_camera(Camera::new_perspective(
std::f32::consts::FRAC_PI_4, 16.0 / 9.0, 0.1, 1000.0,
));
// Spawn an entity from a component bundle.
let entity = world.spawn((
Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
GlobalTransform::default(),
Name::new("hero"),
));
assert!(world.get_transform(entity).is_some());Implementations§
Source§impl GameWorld
impl GameWorld
Sourcepub fn from_world(world: World) -> Self
pub fn from_world(world: World) -> Self
Creates a GameWorld from an existing ECS World.
Used for restoring a snapshot in play mode.
Sourcepub fn spawn<B: ComponentBundle>(&mut self, bundle: B) -> EntityId
pub fn spawn<B: ComponentBundle>(&mut self, bundle: B) -> EntityId
Spawns a new entity with the given component bundle.
Returns the EntityId of the newly created entity.
§Examples
use khora_sdk::GameWorld;
use khora_sdk::prelude::ecs::{GlobalTransform, Transform};
let mut world = GameWorld::new();
let entity = world.spawn((Transform::identity(), GlobalTransform::default()));
assert!(world.get_transform(entity).is_some());Sourcepub fn despawn(&mut self, entity: EntityId) -> bool
pub fn despawn(&mut self, entity: EntityId) -> bool
Removes an entity and all its components from the world.
Returns true if the entity existed and was removed.
Sourcepub fn spawn_camera(&mut self, camera: Camera) -> EntityId
pub fn spawn_camera(&mut self, camera: Camera) -> EntityId
Spawns a camera entity with a Camera component and an identity
GlobalTransform.
This is the recommended way to add a camera to the scene. The
RenderAgent will automatically discover cameras during its
extraction phase.
Returns the EntityId of the camera entity.
Sourcepub fn add_mesh(&mut self, mesh: Mesh) -> HandleComponent<Mesh>
pub fn add_mesh(&mut self, mesh: Mesh) -> HandleComponent<Mesh>
Adds a mesh to the asset registry and returns a handle component.
The returned HandleComponent<Mesh> can be attached to entities
to give them a visible mesh. The RenderAgent will automatically
upload the mesh to GPU when the entity is rendered.
§Arguments
mesh- The CPU-side mesh data.
§Returns
A HandleComponent<Mesh> that references the stored mesh.
§Examples
// `mesh` is CPU-side geometry you have built or loaded.
let handle = world.add_mesh(mesh);
let entity = world.spawn((Transform::identity(), handle));For the common case of built-in shapes, prefer the procedural helpers
spawn_plane, spawn_cube_at,
and spawn_sphere, which attach the mesh for you.
Sourcepub fn add_component<C: Component>(&mut self, entity: EntityId, component: C)
pub fn add_component<C: Component>(&mut self, entity: EntityId, component: C)
Adds a component to an existing entity.
If the entity already has a component of this type, the old value is replaced.
Sourcepub fn remove_component<C: Component>(&mut self, entity: EntityId)
pub fn remove_component<C: Component>(&mut self, entity: EntityId)
Removes a single component C from entity. Other components on
the same entity (in any domain) are preserved — this is a surgical
removal, not a domain wipe. Backed by World::remove_component.
No-op (silently logged) if the entity is dead or doesn’t carry C.
Sourcepub fn query<'a, Q: WorldQuery>(&'a self) -> Query<'a, Q>
pub fn query<'a, Q: WorldQuery>(&'a self) -> Query<'a, Q>
Creates a read-only query over the world.
§Examples
use khora_sdk::GameWorld;
use khora_sdk::prelude::ecs::{GlobalTransform, Name, Transform};
let mut world = GameWorld::new();
world.spawn((Transform::identity(), GlobalTransform::default(), Name::new("a")));
// Iterate every entity that has both a Transform and a Name.
for (transform, name) in world.query::<(&Transform, &Name)>() {
let _ = (transform.translation, &name.0);
}Sourcepub fn query_mut<'a, Q: WorldQuery>(&'a mut self) -> QueryMut<'a, Q>
pub fn query_mut<'a, Q: WorldQuery>(&'a mut self) -> QueryMut<'a, Q>
Creates a mutable query over the world.
§Examples
use khora_sdk::GameWorld;
use khora_sdk::prelude::ecs::{GlobalTransform, Transform};
use khora_sdk::prelude::math::Vec3;
let mut world = GameWorld::new();
world.spawn((Transform::identity(), GlobalTransform::default()));
// Nudge every transform up by one unit.
for (transform,) in world.query_mut::<(&mut Transform,)>() {
transform.translation = transform.translation + Vec3::Y;
}Sourcepub fn spawn_entity(&mut self, transform: &Transform) -> EntityId
pub fn spawn_entity(&mut self, transform: &Transform) -> EntityId
Spawns an entity with just a transform component.
Returns the EntityId of the newly created entity.
Sourcepub fn iter_entities(&self) -> impl Iterator<Item = EntityId> + '_
pub fn iter_entities(&self) -> impl Iterator<Item = EntityId> + '_
Returns an iterator over all entity IDs in the world.
Sourcepub fn get_transform_mut(&mut self, entity: EntityId) -> Option<&mut Transform>
pub fn get_transform_mut(&mut self, entity: EntityId) -> Option<&mut Transform>
Gets a mutable reference to a transform component.
Returns None if the entity doesn’t exist or has no transform.
Sourcepub fn get_transform(&self, entity: EntityId) -> Option<&Transform>
pub fn get_transform(&self, entity: EntityId) -> Option<&Transform>
Gets a reference to a transform component.
Returns None if the entity doesn’t exist or has no transform.
Sourcepub fn get_component_mut<C: Component>(
&mut self,
entity: EntityId,
) -> Option<&mut C>
pub fn get_component_mut<C: Component>( &mut self, entity: EntityId, ) -> Option<&mut C>
Gets a mutable reference to any component.
Returns None if the entity doesn’t exist or has no such component.
Sourcepub fn get_component<C: Component>(&self, entity: EntityId) -> Option<&C>
pub fn get_component<C: Component>(&self, entity: EntityId) -> Option<&C>
Gets a reference to any component.
Returns None if the entity doesn’t exist or has no such component.
Sourcepub fn sync_global_transform(&mut self, entity: EntityId)
pub fn sync_global_transform(&mut self, entity: EntityId)
Synchronizes the GlobalTransform component from the Transform component.
This should be called after modifying a Transform to ensure the changes are visible to the rendering system. This is a convenience method that copies the local transform to the global transform.
§Example
use khora_sdk::GameWorld;
use khora_sdk::prelude::ecs::{GlobalTransform, Transform};
use khora_sdk::prelude::math::Vec3;
let mut world = GameWorld::new();
let entity = world.spawn((Transform::identity(), GlobalTransform::default()));
// Move the entity, then sync so the renderer sees the new pose.
if let Some(transform) = world.get_transform_mut(entity) {
transform.translation = transform.translation + Vec3::Y;
}
world.sync_global_transform(entity);Sourcepub fn update_transform<F>(&mut self, entity: EntityId, f: F)
pub fn update_transform<F>(&mut self, entity: EntityId, f: F)
Updates an entity’s transform and immediately syncs it to GlobalTransform.
This is a convenience method that combines getting the transform, applying a modification function, and syncing to GlobalTransform.
§Example
use khora_sdk::GameWorld;
use khora_sdk::prelude::ecs::{GlobalTransform, Transform};
use khora_sdk::prelude::math::Vec3;
let mut world = GameWorld::new();
let entity = world.spawn((Transform::identity(), GlobalTransform::default()));
world.update_transform(entity, |t| {
t.translation = t.translation + Vec3::Y;
});Sourcepub fn set_parent(&mut self, child: EntityId, new_parent: Option<EntityId>)
pub fn set_parent(&mut self, child: EntityId, new_parent: Option<EntityId>)
Reparents child under new_parent, or detaches it (root) when
new_parent is None.
Maintains both the Parent component on child and the Children
list on the involved parents. Refuses cycles silently (a no-op).
Sourcepub fn add_material<M: Material>(&mut self, material: M) -> MaterialRef
pub fn add_material<M: Material>(&mut self, material: M) -> MaterialRef
Builds an authored, inline material reference to attach to entities.
The returned MaterialRef::Inline embeds
the material value directly in the scene. The resolver turns it into a
runtime material handle, which the GPU projection uploads. To reference a
.kmat asset instead, attach MaterialRef::Asset(uuid).
§Arguments
material- The CPU-side material data (e.g.,StandardMaterial).
§Returns
A MaterialRef::Inline referencing the given material.
Sourcepub fn inner_world(&self) -> &World
pub fn inner_world(&self) -> &World
Returns a shared reference to the underlying ECS World.
Useful for serialization and other low-level operations that need
direct access to the world outside the GameWorld API surface.
Sourcepub fn inner_world_mut(&mut self) -> &mut World
pub fn inner_world_mut(&mut self) -> &mut World
Returns an exclusive reference to the underlying ECS World.
Useful for serialization and other low-level operations that need
direct access to the world outside the GameWorld API surface.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for GameWorld
impl !RefUnwindSafe for GameWorld
impl Send for GameWorld
impl Sync for GameWorld
impl Unpin for GameWorld
impl UnsafeUnpin for GameWorld
impl !UnwindSafe for GameWorld
Blanket Implementations§
§impl<T> AppLifecycle for Twhere
T: ?Sized,
impl<T> AppLifecycle for Twhere
T: ?Sized,
§fn on_start(&mut self, ctx: &mut dyn AppContext)
fn on_start(&mut self, ctx: &mut dyn AppContext)
update. Apps install their theme / fonts here.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().§impl<F, T, S> SimdInto<T, S> for Fwhere
T: SimdFrom<F, S>,
S: Simd,
impl<F, T, S> SimdInto<T, S> for Fwhere
T: SimdFrom<F, S>,
S: Simd,
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.