Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Is there some form of best practices for handling differing entity states/actions?

Discussion in 'DOTS Dev Blitz Day 2023 - Q&A' started by SolusFide, Aug 24, 2023.

  1. SolusFide

    SolusFide

    Joined:
    Oct 11, 2012
    Posts:
    3
    I am thinking about how to handle logic for managing various states of an entity. IE walking, attacking, resting, etc.

    I've seen people suggest using tag components for things like this. However from reading through how archetypes are handled, it seems like these types of structural changes could cause performance issues if the components were added and removed frequently for a large group of entities. Are there any best practices for this or general guidance for handling states?
     
  2. AlexAdach

    AlexAdach

    Joined:
    Jan 25, 2023
    Posts:
    24
    just use enableable components. It doesn't cause structural changes.

    Tho tbh my approach has been not to have components represent states, but rather the state is an enum on a single component, and then depending on the state, I enable/disable components that I want evaluated in the various states. Is the entity staggered? Dead? I disable the movement, navigation and combat components so that logic won't run etc.
     
  3. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    How do you handle the enter state and exit state logic with Enableable components ?
    Edit : Oh I guess you have some kind of system controll that can do that before disbling any components !

    You can find a good resume here

    I'm currently using this too : https://github.com/PhilSA/Trove/tree/main/com.trove.polymorphicstructs
    And there is also a sample available on the subject : https://github.com/Unity-Technologi...itiesSamples/Assets/Miscellaneous/StateChange
    Also the platformer character controller sample : https://github.com/Unity-Technologies/CharacterControllerSamples
     
    Last edited: Aug 24, 2023
  4. DaxodeUnity

    DaxodeUnity

    Unity Technologies

    Joined:
    Aug 5, 2021
    Posts:
    27
    This is an approach I've seen work quite well +1

    And one can mix match depending on the situation. E.g. walking vs jumping can be as simple as checking (with a switch) the enum of the component directly, without first enabling/disabling a `JumpData`/`WalkData`.
     
  5. AlexAdach

    AlexAdach

    Joined:
    Jan 25, 2023
    Posts:
    24
    the polymorphic structs are cool. And actually the approach I have to my state machine is similar to the AI example they have in that repository, but at the moment the state changes are are hardcoded in my systems. I do like the way they programmed the considerations though. Thanks for sharing.
     
    HBG-Mathieu and DaxodeUnity like this.