### System Implementation Example Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Shows how a system can implement the IUpdate interface to perform actions, such as creating entities, during its update cycle. ```csharp public class SpawningSystem : WorldSystem, IUpdate { public void Update() { var entity = World.CreateEntity(); } } ``` -------------------------------- ### Implement ICopyable for Managed Components (Basic Copy) Source: https://github.com/nilpunch/massive-ecs/wiki/Rollbacks Implement the ICopyable interface for managed components to ensure correct rollback behavior. This basic example demonstrates creating a new list for each copy. ```csharp public struct Inventory : ICopyable { public List Items; public void CopyTo(ref Inventory other) { other.Items = new List(Items); } } ``` -------------------------------- ### Implement ICopyable for Managed Components (Efficient Copy) Source: https://github.com/nilpunch/massive-ecs/wiki/Rollbacks Implement the ICopyable interface for managed components to ensure correct rollback behavior. This example shows an efficient CopyTo method that reuses and clears a list. ```csharp public struct Inventory : ICopyable { public List Items; public void CopyTo(ref Inventory other) { other.Items ??= new List(); other.Items.Clear(); other.Items.AddRange(Items); } } ``` -------------------------------- ### Get Entifier from Entity ID Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Retrieve the permanent Entifier associated with an entity ID. An Entifier includes an ID and a version for uniqueness. ```cs Entifier entifier = world.GetEntifier(entityId); ``` -------------------------------- ### Entity and Component Creation Source: https://github.com/nilpunch/massive-ecs/blob/master/README.md Demonstrates how to create a world, entities (empty or with components), and add/set components to entities. ```csharp struct Player { } struct Position { public float X; public float Y; } class Velocity { public float Magnitude; } // Classes work just fine. interface IDontEvenAsk { } // Create a world. var world = new World(); // Create entities. var enemy = world.Create(); // Empty entity. var player = world.Create(); // With a component. // Add components. world.Add(player); // Adds component without initializing data. world.Get(player) = new Velocity() { Magnitude = 10f }; world.Set(enemy, new Velocity()); // Adds component and sets its data. // Or use feature-rich entity handle. var npc = world.CreateEntity(); npc.Add(); if (npc.Has()) { npc.Destroy(); } // Get full entity identifier from player ID. // Useful for persistent storage of entities. Entifier playerEntifier = world.GetEntifier(player); ``` -------------------------------- ### Instantiate Systems Class Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Create an instance of the Systems class to manage and organize your game's logic. ```cs var systems = new Systems(); ``` -------------------------------- ### Create and Destroy Entities Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Demonstrates how to create new entities and destroy existing ones using their IDs. Note that entity IDs are reused after destruction. ```cs // Creates a new entity with no components and returns its ID. int entityId = world.Create(); // Destroys an entity and all its components. world.Destroy(entityId); ``` -------------------------------- ### Build and Rebuild Systems Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Call the Build() method to prepare registered systems for execution. This method can be called multiple times to update systems after registration changes. ```cs systems.Build(world); ``` -------------------------------- ### Running System Methods Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Demonstrates how to invoke defined system methods like Update and Cleanup on all systems that implement the respective interfaces. ```csharp var deltaTime = 1f / 60; systems.Run(deltaTime); systems.Run(); ``` -------------------------------- ### Custom System Interface Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Illustrates implementing the base ISystem interface directly if not using the provided WorldSystem base class. This requires manual implementation of the Build method. ```csharp public class MovementSystem : ISystem { void ISystem.Build(int id, Allocator allocator) { } } ``` -------------------------------- ### Register Systems with the Systems Class Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Register systems for execution using New, Instance, or a factory delegate. Systems can be created via default constructor, provided instance, or a lambda. ```cs systems.New(); // Creates a system using the default constructor. systems.Instance(new SpawningSystem()); // Adds a concrete system instance. systems.New(() => new SpawningSystem()); // Registers a system using a factory delegate. ``` -------------------------------- ### Initialize MassiveWorld Source: https://github.com/nilpunch/massive-ecs/wiki/Rollbacks Instantiate a MassiveWorld to enable rollback capabilities. ```csharp var world = new MassiveWorld(); ``` -------------------------------- ### Register Systems with Order Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Register systems with a specified order to control their execution sequence. Systems with the same order are built in registration order. ```cs systems.New(order: 0); systems.Instance(new SpawningSystem(), order: -1); systems.New(() => new SpawningSystem(), order: 0); ``` -------------------------------- ### Iterating with ForEach Queries Source: https://github.com/nilpunch/massive-ecs/blob/master/README.md Shows how to iterate over entities using the ForEach method with different query configurations, including passing arguments and filtering. ```csharp var deltaTime = 1f / 60f; // Iterate using lightweight queries. // ForEach will select only those entities that has all the necessary components. world.ForEach((Entity entity, ref Position position, ref Velocity velocity) => { position.Y += velocity.Magnitude * deltaTime; if (position.Y > 5f) { // Create and destroy any amount of entities during iteration. entity.Destroy(); } }); // Pass arguments to avoid boxing. world.ForEach((world, deltaTime), (ref Position position, ref Velocity velocity, (World World, float DeltaTime) args) => { // ... }); // Filter entities right in place. // You don't have to cache anything. world.Include().Exclude() .ForEach((ref Position position) => { // ... }); ``` -------------------------------- ### Define a Basic WorldSystem Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Create a custom system by inheriting from the WorldSystem class. This allows interaction with the ECS world. ```cs public class SpawningSystem : WorldSystem { public void DoSomething() { var entity = World.CreateEntity(); } } ``` -------------------------------- ### Iterating with DataSet and Rich Entities Source: https://github.com/nilpunch/massive-ecs/blob/master/README.md Demonstrates iterating over entities using DataSet for performance and using rich entities for simpler access. ```csharp // Iterate over rich entities. (simpler) foreach (var entity in world.Include().Entities) { ref Position position = ref entity.Get(); // ... } // Iterate using foreach with data set. (faster) var positions = world.DataSet(); foreach (var entityId in world.Include()) { ref Position position = ref positions.Get(entityId); // ... } ``` -------------------------------- ### Full Foreach for Rich and ID Iteration Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Demonstrates iterating over entities using both rich entity objects and entity IDs with associated data sets. Choose based on whether you need full entity access or faster ID-based processing. ```cs // Iterate over rich entities. (simpler) foreach (var entity in world.Include().Exclude().Entities) { ref var position = ref entity.Get(); // ... } // Iterate over IDs with data sets. (faster) var positions = world.DataSet(); foreach (var entityId in world.Include().Exclude()) { ref var position = ref positions.Get(entityId); // ... } ``` -------------------------------- ### Fill Collection with Matching Entities Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Use the Fill() extension method to populate a collection with entities that match the query. ```cs var entifiers = new List(); world.Fill(entifiers); ``` -------------------------------- ### Complex Component Queries Source: https://github.com/nilpunch/massive-ecs/blob/master/README.md Illustrates how to chain multiple component types (including AND conditions) in queries and reuse them for different iteration patterns. ```csharp // Chain any number of components in queries. var query = world .Include>() .Exclude>(); // Reuse the same query to iterate over different components. query.ForEach((ref int n, ref bool b) => { }); query.ForEach((ref string str) => { }); ``` -------------------------------- ### Instantiate a World Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Create a new instance of the World class, which manages entities and components. ```cs var world = new World(); ``` -------------------------------- ### Create Entity Handle Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Obtain a lightweight, managed Entity handle that bundles an Entifier with a World reference for convenient operations. ```cs Entity entity = world.GetEntity(entityId); Entity entity = world.CreateEntity(); Entity entity = new Entity(entifier, world); Entity entity = entifier.In(world); ``` -------------------------------- ### Manual Filtering with BitSets Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Manually create filters using BitSets for inclusion and exclusion of components. This provides fine-grained control over query definitions. ```cs // Super manual. var include = new BitSet[] { world.BitSet(), world.BitSet() }; var exclude = new BitSet[] { world.BitSet() }; var filter = new Filter(include, exclude); var query = new Query(world, filter); ``` -------------------------------- ### System Method Interfaces Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Defines interfaces for system methods that can be run on systems. Implement these interfaces to define custom update or cleanup logic for your systems. ```csharp public interface IUpdate : ISystemMethod { void Update(float deltaTime); void ISystemMethod.Run(float args) => Update(args); } public interface ICleanup : ISystemMethod { void Cleanup(); void ISystemMethod.Run() => Cleanup(); } ``` -------------------------------- ### ForEach with Extended Parameters for Filtering Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Utilize the ForEach method with extended parameters to directly access components of matching entities during iteration. This is efficient for accessing specific component data. ```cs // Iterate entities that have at least Position and Velocity and no Renderer. world.Exclude().ForEach((int entityId, ref Position position, ref Velocity velocity) => { }); ``` -------------------------------- ### Dynamic Filtering with Builder Pattern Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Use the DynamicFilter class to build queries in a more fluent, builder-like style. This simplifies the creation of complex include/exclude logic. ```cs // Builder-like. var filter = new DynamicFilter(world) .Include() .Include() .Exclude(); var query = new Query(world, filter); ``` -------------------------------- ### Include and Exclude Components for Queries Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Define queries that include specific components and exclude others. This is useful for selecting entities that meet precise criteria. ```cs var includeOnly = world.Include(); var excludeOnly = world.Exclude(); var combined = world.Include().Exclude(); ``` -------------------------------- ### Access Component Data via DataSet Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Use the DataSet to directly set component data for an entity ID. This bypasses the need for an Entity handle. ```cs var positions = world.DataSet(); positions.Set(entityId, new Position()); ``` -------------------------------- ### Count Matching Entities Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Use the Count() extension method to efficiently determine the number of entities that match a given query. ```cs world.Include().Count(); ``` -------------------------------- ### Iterate All Entity IDs Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Iterate over all entity IDs currently present in the world using a foreach loop or the ForEach method. ```cs // Using a foreach to iterate over all entity IDs. foreach (var entityId in world) { } // Using ForEach method to do the same. world.ForEach((int entityId) => { }); ``` -------------------------------- ### Iterating Entities with Filtered Queries Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Iterate over entities that match a defined query using various foreach loops and ForEach delegates. This allows processing of selected entities. ```cs foreach (var entityId in query) { } foreach (var entity in query.Entities) { } query.ForEach((int entityId) => { }); query.ForEach((Entity entity) => { }); ``` -------------------------------- ### Add Component to Entity Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Add a component to an entity. The 'Add' method adds the component without initializing data, while 'Set' adds and initializes. ```cs world.Add(entity); // Adds component without initializing data. world.Get(entity) = new Position(); world.Set(entity, new Position()); // Adds component and sets its data. ``` -------------------------------- ### Fetch First Matching Entity or ID Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Retrieve the first entity or entity ID that matches a query using FirstEntity() or First(). Returns a default value if no matching entities are found. ```cs // Returns first matching entity or Entity.Dead if none. var first = world.Include().FirstEntity(); // Returns first matching entity ID or Constants.InvalidId if none. var firstId = world.Include().First(); ``` -------------------------------- ### Check for Component Existence Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Determine if an entity possesses a specific component, either through the World class or directly via a DataSet. ```cs bool hasComponent = world.Has(entity); // Using data set: bool hasPosition = positions.Has(entityId); ``` -------------------------------- ### Disabling Tag Optimization Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Configures the World to treat tags (empty components) as regular DataSets instead of optimizing them as BitSets. Set StoreEmptyTypesAsDataSets to true in WorldConfig. ```csharp var world = new World(new WorldConfig() { StoreEmptyTypesAsDataSets = true; }) ``` -------------------------------- ### Destroy Entity using Handle Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Destroy an entity using its Entity handle. This is a convenient object-oriented way to perform the destroy operation. ```cs if (entity.IsAlive) { entity.Destroy(); } ``` -------------------------------- ### Check Entity Liveness Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Verify if an entity represented by an Entifier is currently alive in the world. ```cs bool isAlive = world.IsAlive(entifier); ``` -------------------------------- ### Iterate Entities with Specific Components Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Iterate over entities that possess at least the specified components, accessing their data directly via ref parameters. ```cs // Iterates only entities with at least Position and Velocity components. world.ForEach((int entityId, ref Position position, ref Velocity velocity) => { }); ``` -------------------------------- ### Destroy Matching Entities Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Use the Destroy() extension method to remove all entities that satisfy the specified query criteria. ```cs world.Exclude().Destroy(); ``` -------------------------------- ### Create Entifier Directly Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Create a new Entifier directly without first creating an entity. This Entifier can then be used to create an entity. ```cs Entifier entifier = world.CreateEntifier(); ``` -------------------------------- ### Clear Components or Entities Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Clear all instances of a specific component from entities, or clear all entities and their components from the world. ```cs world.Clear(); // Using data set: positions.Clear(); // Or destroy all entities in a world at once: world.Clear(); ``` -------------------------------- ### Check Available Rollback Frames Source: https://github.com/nilpunch/massive-ecs/wiki/Rollbacks Use CanRollbackFrames to determine how many frames can be rolled back. Rolling back with this value restores to the oldest saved frame. Using a value greater than this will throw an error. ```csharp var canRollbackFrames = set.CanRollbackFrames; // Calling Rollback() with this value restores to the oldest saved frame. // Using a value greater than this will throw an error. set.Rollback(canRollbackFrames); ``` -------------------------------- ### Ensure Initial Saved Frame for Rollbacks Source: https://github.com/nilpunch/massive-ecs/wiki/Rollbacks To prevent errors when calling Rollback() on a new Massive object, save an initial empty frame immediately after creation. ```csharp var set = new MassiveSparseSet(); set.SaveFrame(); // Save an initial empty frame to enable rollbacks. ``` -------------------------------- ### Iterate Rich Entities Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Iterate over entities using the rich Entity handle, which provides an object-oriented interface to world operations. ```cs foreach (var entity in world.Entities) { } world.ForEach((Entity entity) => { }); world.ForEach((Entity entity, ref Position position, ref Velocity velocity) => { }); ``` -------------------------------- ### Save Current State with MassiveSparseSet Source: https://github.com/nilpunch/massive-ecs/wiki/Rollbacks Use SaveFrame() to store the current state of a MassiveSparseSet before making changes. ```csharp var set = new MassiveSparseSet(); // Make changes. set.Assign(0); set.Assign(1); // Save the current state. set.SaveFrame(); ``` -------------------------------- ### Custom Default Component Value Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Defines a custom default value for a component using the [DefaultValue] attribute. This value is used when the component is added or removed. ```csharp public struct Orientation { public Quaternion Rotation; [DefaultValue] public static Orientation Default => new Orientation() { Rotation = Quaternion.Identity }; } ``` -------------------------------- ### Remove Component from Entity Source: https://github.com/nilpunch/massive-ecs/wiki/Entity-Component-System Remove a component from an entity. The 'Remove' method returns true if the component was present and removed. ```cs bool wasPresent = world.Remove(entity); // Using data set: positions.Remove(entityId); ``` -------------------------------- ### Rollback Beyond Last Saved Frame Source: https://github.com/nilpunch/massive-ecs/wiki/Rollbacks Pass a value greater than 0 to Rollback(frames) to revert to a state further back than the most recent SaveFrame(). This action erases the most recent saved frame. ```csharp // Make more changes again. set.Unassign(0); set.Assign(2); // Save the state. set.SaveFrame(); // Restore to the previous SaveFrame() call. // This erases the most recent saved frame. set.Rollback(1); ``` -------------------------------- ### Rollback to Most Recent Saved State Source: https://github.com/nilpunch/massive-ecs/wiki/Rollbacks Use Rollback(0) to revert to the state saved by the most recent SaveFrame() call. This can be called repeatedly without erasing the saved frame. ```csharp // Make more changes. set.Unassign(0); set.Assign(2); // Restore everything to the last SaveFrame() call. set.Rollback(0); // You can call this repeatedly, it does not erase the last saved frame. set.Rollback(0); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.