#Hi guys let s see if you can help me out

1 messages · Page 1 of 1 (latest)

slate yacht
#

This is a code design problem more than a "what do I type" one. What is an entity in your project?

frozen lodge
#

It's simple to solve, use an interface instead of your actual Entity. I believe you can also add a Generic Argument for Entity. Alternatively, you can use object as the state entity parameter than parse to the appropriate type when using the object.

Note that I would advice against placing the StateMachine directly inside the Entity as some Entity, such as Projectile, Collectable, Switch, etc., might not require StateMachine. Even some type of Character might be simple enough to not use a StateMachine.

Additionally, you will probably want to have State as Serializable to be able to directly modify their value which could be hard to pull off if you place your StateMachine inside your Entity (Usually, you need to remove the argument by defining directly the type; public class StateMachineCharacter : StateMachine<Character> {}).

Finally, your StateMachine should hold all of your State and you should reuse them to have the best performance. Otherwise you are going to create a significant amount of Garbage that would slow down considerably your application given short live State. It will also give you the ability to use [SerializeField] value inside your State keeping your overall Entity/Character/Projectile clean.

public abstract class Entity<E> where E : Entity<E>
{
    public StateMachine<E> stateMachine = new StateMachine<E>();
}

public class Character : Entity<Character>
{

}

public class CharacterState : State<Character>
{
    public CharacterState(Character entity, StateMachine<Character> stateMachine) 
    : base(entity, stateMachine)
    {

    }
}
elder gulch
#

That might be a good solution. Although I think I solved it a different way. I made an Entity class that only inherits MonoBehaviour and another one that inherits from Entity and has the generic parameter for the gameobject.

#

But what do you mean by not having the state machine directly inside Entity. Where else would I put it?

#

@frozen lodge

frozen lodge
#

Not all Entity will need a StateMachine, only a subset of them.

elder gulch
#

That makes sense

frozen lodge
elder gulch
#

And regarding the states. Each state has a data scriptable object related to it. How could I make it so in the inspector I can set the states and its data? From what I know you can't serialize dicts

frozen lodge
elder gulch
#

Well if it's that easy then it might actually work