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

Question How to Implement such a requirement using dots

Discussion in 'Entity Component System' started by Bagazi, Sep 6, 2023.

  1. Bagazi

    Bagazi

    Joined:
    Apr 18, 2018
    Posts:
    609
    Assuming in an RPG game, players can play as a warrior character with a sword and can perform slashing actions (animations, e.g., considered effective at specific frames), and when hitting enemies, it causes damage to them. What would be a simplified or most reasonable approach to implementing such a requirement using ECS?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,976
    How I would probably do it using my own tech:
    1) At specific point while animation is playing, instantiate a hitbox entity. Entity contains position, collider, damage, lifetime, and a buffer of hit entities. It might also be parented to the sword.
    2) Have system that destroys hitboxes when their lifetime expires.
    3) Have system which builds hitbox and hurtbox collision layers.
    4) Have system which tests the hitbox layer against the hurtbox layer. For each hit, check if the hurtbox entity is already in the list of hit entities on the hitbox. If so, return. Otherwise, add it and apply damage to the hurtbox entity.
     
  3. Bagazi

    Bagazi

    Joined:
    Apr 18, 2018
    Posts:
    609
    Is it valid to depend on the main character's animation for sword swinging, for example, if the main character's sword swing animation has animation events like SlashStart and SlashEnd? How can you bind or associate animation events with a system?

    I can mostly understand the approach you mentioned earlier, which is quite impressive. However, considering another scenario where the game's precision requirements are lower (e.g., a third-person game), and collision detection is not used, but instead, active collision queries are performed (e.g., checking if there are target objects within a certain sector), how should this be done? Assuming in the traditional GameObject mode, maybe I can get the execution time of this animation and calculate the targets and damage simultaneously, all of which can be encapsulated within the logic of a game object, such as PlayCharacter. However, in ECS, I feel that these logics need to be considered in a more holistic way (this is just my feeling and may not be reliable). If so, how should the query conditions be set?

    Because I haven't had previous experience with ECS, I find it a bit challenging to think about slightly more complex gameplay scenarios, like how to design different systems and combine them organically. Let's take the previous scenario as an example: suppose the player's sword has a buff feature, and enemies hit by it will lose ten health points per second (for 5 seconds). Should I add a BuffSystem for this? If so, how should the query be done in the BuffSystem? Is it based on specific components or states (iterating through all entities), or does it depend on the results of other systems, such as calculating the list of damaged entities in other systems? If so, how can data be shared between different systems, using a global singleton? Is this reasonable? Does this approach align with the principles of Entity Component Systems (ECS), or does it break the DoD (Data-Oriented Design) principles?
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,976
    One system drops the active events into a DynamicBuffer, and the other systems poll on that buffer.
    For me, this would depend on whether or not I need write access to the target objects immediately after finding them. If not, my physics engine has a method that handles this use case. But if I do need write access, then what I would do depends on a lot of unspecified factors.
    I think in steps. Then I consider which data is the input and output of each step, and then design those into components and queries. But everyone goes about this slightly differently.
    This takes time and practice. You aren't going to get it right away. Start with small projects with low complexity first, and then work your way up to more and more complex things.
     
    Bagazi likes this.
  5. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    288
    I will firstly think about data and components rather than systems. I find it hard to plan the systems if I don't have a clear vision of what data I would need and the relationship between them. I speculate that's the reason it spelled "ECS" rather than "SCE".

    With the requirement in place, I would ask a lot of questions to clarify every data pieces both necessary and redundant. For example, I would ask "what are the data of this attack animation? (or all animations?) and where to get them?", "what data would be similar, can be reused for every animations?", "what data should be used to initiate the damaging logic?", etc.

    For my game, after the data analysis process, it turns out that my heroes, enemies, even skills will produce the same damaging entities, so the game doesn't care whoever initiates the attack or whoever receives that attack as long as the damaging entities are tagged correctly. The systems become greatly simplified compared to what I initially thought about (some months ago, the time I was less experienced in ECS).
     
    Last edited: Sep 7, 2023
    Bagazi and DreamingImLatios like this.