Search Unity

Where to put gameSettings?

Discussion in 'Entity Component System' started by RogueStargun, Jun 1, 2020.

  1. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    In vanilla unity, I'd do something like this for prototyping

    public class GameSettings: Monobehavior{
    public static GameSettings instance;

    }

    to store GameSettings for prototyping, while making it possible to edit certain params in the editor (like mouseSensitivity). This probably isn't the best practice, but I was wondering what the proper ECS way to do something similar would be.
     
  2. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    Ok I figured this out
    Code (CSharp):
    1.  
    2. public class SomeSystem: JobComponentSystem{
    3. GameSettings gameSettings;
    4.  
    5. protected override void OnStartRunning() {
    6.  
    7.         // Get GameSettings Singleton struct
    8.         Entity gameSettingsEntity = GetSingletonEntity<GameSettings>();
    9.         if(!EntityManager.Exists(gameSettingsEntity)){
    10.             Debug.LogError("Error! Entity with gameSettings is missing!");
    11.         }
    12.         gameSettings = EntityManager.GetComponentData<GameSettings>(gameSettingsEntity);
    13.  
    14. }
     
  3. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    You can also just go
    Code (CSharp):
    1. gameSettings = GetSingleton<GameSettings>();
    2.  
    3. // and there's also option of
    4. RequireSingletonForUpdate<GameSettings>();
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    If GameSettings is class IComponentData it should be this.GetSingleton (extension method) as just GetSingleton has struct IComponentData restriction (will be fixed in future)
     
  5. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Thanks eizenhorn, I've been away from ecs for long time so lots has probably changed on me.

    If he's using EntityManager.GetComponentData<GameSettings>(), doesn't that mean it must be struct or can you get class objects the same way now?
    My memory is back from when we had GetComponentObject() for objects and GetComponentData for structs still. Has this changed now?
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Yes, that's a valid point, my answer was more to warn, that GetSingleton can be used for class ICD singletons as an extension method (and itellijidea wouldn't show you that until you will use this. for access base class). And same for EntityManager.GetComponentData as it also has the same extension method with class restriction instead of struct.
    upload_2020-6-1_18-53-21.png
     
  7. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Thanks again eizenhorn. Do you know why there are separate functions for IComponentData class objects vs generic objects?
    GetComponentData and GetComponentObject both return objects from ManagedComponentStore.
    Do IComponentData classes have some special purpose within the api?
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    API evolves :) And as @s_schoener told me it's just an oversight when class ICD was added and they added ticked for that task 2 months ago, and eventually, they will change that restriction. It's not a major issue as we have an extension method, and guys working hard on other much more important things, thus we just can use that extension until they'll fix that thing.
     
  9. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Ok thanks. I'll have to catch up on the current state of the api.