Skip to content
jacobdufault edited this page Jan 17, 2014 · 1 revision

I want to lookup an arbitrary IEntity or ITemplate

You can use the EntityIndex or the TemplateIndex to lookup entities or templates (respectively) by their ids.

[JsonObject(MemberSerialization.OptIn)]
public class YourSystem: BaseSystem, Trigger.GlobalInput {
    public Type[] InputTypes {
        get { return new[] { typeof(SomeInputType) }; }
    }

    public void OnGlobalInput(IGameInput input) {
        // a fictitious input type that supplies us with an entity UniqueId and a TemplateId to lookup
        SomeInputType command = (SomeInputType)input;

        int entityId = command.EntityId;
        int templateId = command.TemplateId;

        // here's how you retrieve entities and templates by id
        IEntity entity = EntityIndex[entityId];
        ITemplate template = TemplateIndex[templateId];

        // do something with entity and template
    }
}

I need to use Floats!

You can use the Real type (in Forge.Utitlies) to simulate floating-point operations while maintaining determinism.

How do I get data instances from IQueryableEntity types (aka IEntity and ITemplate)?

IEntity entity = /* get an entity from somewhere, eg, an update method (Trigger.Update) in a system */;

// get the previous position; only compiles if position data is versioned and has a previous state
entity.Previous<PositionData>();

// get the current position
entity.Current<PositionData>();

// get a reference to a PositionData instance that you can modify
entity.Modify<PositionData>();

These are actually extension methods and not part of the core IQueryableEntity API. The API is defined in terms of DataAccessors. Without the extension methods, getting the current data is still simple; it's just entity.Current(new DataAccessor(typeof(PositionData))

I need to make sure one system executes before/after another system

See BallCollisionSystem.GetExecutionOrdering in the sample.