Skip to main content

GameWorld

Struct GameWorld 

Source
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

Source

pub fn new() -> Self

Creates a new GameWorld with an empty world and asset registry.

Source

pub fn from_world(world: World) -> Self

Creates a GameWorld from an existing ECS World. Used for restoring a snapshot in play mode.

Source

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());
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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);
}
Source

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;
}
Source

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.

Source

pub fn iter_entities(&self) -> impl Iterator<Item = EntityId> + '_

Returns an iterator over all entity IDs in the world.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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);
Source

pub fn update_transform<F>(&mut self, entity: EntityId, f: F)
where F: FnOnce(&mut Transform),

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;
});
Source

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).

Source

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.

Source

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.

Source

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§

Source§

impl Default for GameWorld

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AppLifecycle for T
where T: ?Sized,

§

fn on_start(&mut self, ctx: &mut dyn AppContext)

Called once after the backend is up but before the first update. Apps install their theme / fonts here.
§

fn on_exit(&mut self)

Called once when the window is closing. Persist state here.
§

impl<T> AsAny for T
where T: Any,

§

fn as_any(&self) -> &(dyn Any + 'static)

Returns a reference to the inner value as &dyn Any.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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 T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts 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>

Converts 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)

Converts &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)

Converts &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
where T: Any + Send,

§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 F
where T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(value: T, _simd: S) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,