Persistent Entity System - Maxodie/BattriKeepel2 GitHub Wiki
You can create persistent entities and store either their ID or their class
- every TEntity is a template class using the IGameEntity interface; the template format can be deducted by default using any TEntity parameter, so you can either ignore it or use it for an ambiguous class.
- EntityID is equivalent to an int, it corresponds to the ID of the persistent entity
EntityManager.Get()
public EntityID CreatePersistentEntity<TEntity>(out TEntity outEntity, params object[] variadicArgs) where TEntity : IGameEntity
- out TEntity : the entity result parameter
- params object[] variadicArgs : the optional parameters of the class constructor
public bool Exists(EntityID id)
or
public bool Exists<TEntity>(TEntity entityToCheck) where TEntity : IGameEntity
public void DestroyPersistentEntity(EntityID id)
or
public void DestroyPersistentEntity<TEntity>(TEntity entityToCheck) where TEntity : IGameEntity
public TEntity GetEntity<TEntity>(EntityID id) where TEntity : IGameEntity
or
public EntityID GetEntity<TEntity>(TEntity entityToGet) where TEntity : IGameEntity
//exemple of a persistent entity
class BigManager : IGameEntity
{
public int health = 100;
public void TakeDamage(int amount)
{
health -= amount;
}
}
class SecondPersistentEntity : IGameEntity
{
SecondPersistentEntity(int number, string _name) {}
}
public class GameManager : GameEntityMonoBehaviour
{
BigManager bigManager;
int id;
SecondPersistentEntity secondEntity;
void Start()
{
// exemple with one entity
// creation :
id = EntityManager.Get().CreatePersistentEntity(out bigManager);
// basic use
bigManager.TakeDamage(25);
// get it from the storage with its id
BigManager get = EntityManager.Get().GetEntity<BigManager>(id);
Log.Info("entity 0 hp : " + get.health);
// exemple with a second persistent entity
EntityManager.Get().CreatePersistentEntity(out secondEntity, 10, "kendrick");
// get the id from the storage with its instance
int id2 = EntityManager.Get().GetEntity(secondEntity);
Log.Info("entity 1 id : " + id2);
// destroy entities
EntityManager.Get().DestroyEntity(id); // with id
EntityManager.Get().DestroyEntity(secondEntity); // with instance
}
}