Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Spawn Something

To have anything in your game that actually exists and does things, you need an Entity. Entities are simply an index that you can assign various components to, which are the basic bits of data we need to describe the entity.

For instance, let's create a function to spawn a player character.

One thing we need to know is where the player is in the environment? We use the built-in Transform component:

fn spawn_player() {
      let transform = &Transform::default();
}

We don't have to define anything further than that, but you may very well want to be more explicit with how you spawn them:

let transform = &Transform{
    position: Vec2::new(50.0, -20.0), // Where in the world the transform is.
    scale: Vec2::splat(1.), // Change the size of the entity and everything on it.
    skew: Vec2::ZERO, // Skew the entity
    pivot: Vec2::ZERO, // Change the rotation pivot point.
    rotation: 2.0, // Rotation of the object (in radians?)
    layer: 10.0 // What order for rendering - higher shows up in front of lower.
    _padding: ?? // what is this for
}

You don't need to be so verbose, though, you can just specify what you want to actually change:

let transform = &Transform{
    layer: 10.,
    ..Default::default()
}

Every Transform component comes with a Color component automatically, as well. You can change it if you'd like:

fn spawn_player() {

    let transform = &Transform{
        layer: 10.,
        ..Default::default()
    };

    let color = &Color::ORANGE;
}

Now you have the concept of a player - something that gets rendered on layer 10 and is orange, but how do we add it to the world? We bundle those components together and tell the engine to spawn them, it's as simple as:

Engine::spawn(bundle!(transform, color));

So putting that all together, let's put this into our lib.rs, don't forget to add the #[system_once] macro to it and we should see our orange entity, right?

srclib.rs
use engine::prelude::*;
mod ffi;

#[system_once]
fn spawn_player() {

    let transform = &Transform{
        layer: 10.,
        ..Default::default()
    };

    let color = &Color::ORANGE;


    Engine::spawn(bundle!(transform, color));
}

Go ahead and run your game and get excited to see... nothing.

What we've written only puts an entity in the world at layer 10 with the concept of the color orange attached to it. In order to see our entity, we need to add some kind of render component.