Search Unity

Question Class Dependency Refactoring

Discussion in 'Scripting' started by brian1gramm1, Sep 28, 2022.

  1. brian1gramm1

    brian1gramm1

    Joined:
    Sep 15, 2017
    Posts:
    56
    TO THE POINT:
    I have 3 classes, EnemyManager(EM), PlayerStatsManager(PSM), and InventoryManager(IM). I do not want any dependencies between these classes, but, EM needs info from PSM and IM to determine Player damage on enemy health. I already have a static variable and function system in PSM so the EM can give the player experience for each enemy killed. I do not like using static Vars and Funcs, but I felt it was the best solution for avoiding dependencies. Now I'm not so sure.

    What other ways are their to keep classes independent, while sharing info between them?

    MORE DETAILS:
    I've created a turn based 2d game. The main system is my TIleManager that uses IEnumerators to give control to different classes, aka EnemyManager, ShopManager. The other classes are PlayerStatsManager, and InventoryManager. The TM waits for the player to select a tile to move to than checks to see if any TileEvent classes are controlling that tile and gives that class control, aka a random enemy attack or a city shop.

    It wasn't till I started adding experience from the EM to the PSM that I started having dependency issues. I would rather my EM class did not have references to the PSM or IM, otherwise it can not function without those classes. Adding an event system (OnEnemyKilled) and having other classes be notified when such event occurred is still a type of dependency.

    I researched Dependency Injection and googled class Dependencies but could not find a solution. I suppose in a situation where having one class to handle all three functions (EM, PSM, and IM) would be the only real solution, but I wanted to ask anyway and see what programming options are available in this situation.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Shouldn't that be a TurnManager?? It seems like TileManager shouldn't know anything about time and turns... I would assume a TileManager would know about ... Tiles.

    While everybody loves to write clean code, at the end of the day if you have more than one subsystem mutating the same piece of data, there has to be some awareness between them.
     
  3. brian1gramm1

    brian1gramm1

    Joined:
    Sep 15, 2017
    Posts:
    56
    Then do you think using static vars and funcs is the best way, or should each manager have a reference to the other managers, or should I just have a new manager manage the lower level managers....that is my question???
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I know this isn't going to be what you want to hear but...

    I have used and continue to use 100% of all the ways listed above.

    In fact I frequently mix and match methods within a given project.

    And when it causes me issues or growing pains, I stop and refactor.

    Refactoring helps keep my code minty fresh. Or well, at least not decomposing.