Search Unity

Reducing Dependency, ScriptableObjects, get return function value

Discussion in 'Scripting' started by Gordon_G, Jun 7, 2020.

  1. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    372
    Hey All, lately I've been learning about ScriptableObjects and UnityEvents to cut the dependencies on the various game managers in my code. I've got about everything severed, but I've come across this situation I'm not sure how to handle:

    The simple view is I have a Player class that lets players use items in the game and an Items class that keeps track of the various items in the game (including how much they manna cost to use, ...), and a PlayerStats class the keeps, among other things, the player's current manna.

    Now the Player needs to know if it has enough manna to use a particular item. So in the PlayerStats class I have a function "CanUseItem( current_item_reference )" that returns true or false when I pass it a reference to the item the player is trying to use - it checks to see if the player has enough manna to use the given item. In the PlayerClass I have a reference to PlayerStates and use that to call CanUseItem.

    I realize there ware a lot of ways to jigger this, but they all involve some dependency between at least two of the classes.

    Is there some way of doing this with ScriptableObjects that remove the dependencies - or some other way?

    I thought of doing a ScriptableObject UnityEvent, with a listener in the PlayerStats class, and passing it a writable parameter that I could then check is true or false, but I'm not sure if there is a better/more sane way to approach the problem.

    Thanks for your ideas in advance!
     
  2. MatrixQ

    MatrixQ

    Joined:
    May 16, 2020
    Posts:
    87
    Hmm. I'm not sure why you want to cut all the ties. Aren't player and playerstats dependent on each other at the least? Is PlayerStat its own gameobject? Seems like it could just be a class to hold and handle all the player's stats, wouldn't even have to be a Monobehavior.

    For interacting with the items, you could look at interfaces, they are pretty good at establishing something like that.

    Otherwise, I don't think I fully get your problem.
     
    Gordon_G likes this.
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    What I like to do is use a scriptable object as a middleman between classes. I create a static instance of a scriptable object on startup and have a class that stores it. Then either have classes, such as a ui manager subscribe to events in the SO or pass their reference. Then the events or methods can be used on the SO call methods in other classes.

    For example, when my player's health changes, it reports that to a player manager scriptable object which will then fire an on health change event. The ui can listen to that and then adjust its display. That way, I can put a player in the scene and it doesn't care if there is no ui. I can also put a ui in a scene and it doesn't matter if there is no player there.
     
    Gordon_G likes this.
  4. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    372
    That's a strategy I haven't thought of. So, you're actually having the ScriptableObject initiate the health change event or the Player initiates the event after it changes health? If it is the former, how are you doing that ?- I mean, right now I have ScriptableObject events and I'm not sure how I would get another ScriptableObject to initiate one of those if I couild.... call a function in the ScritableObject to change the health value and Raise the event there? hmmm... that would require the ScriptableObject would have a reference to the ScriptableObject event...
     
    Last edited: Jun 8, 2020
  5. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    372
    Yes, I'm not sure I would want to either, I guess that's part of why I'm asking.

    Perhaps I am mixing together values in the Player Stats class, such as the score that can be completely separated from the Player, with abilities, such as manna, that cannot be. So maybe I need to look at that class and think about moving all the score related stuff into a separate class and not worry about separating the ability related values that affect what the player can do... Your reply has helped me to think about that!
     
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    I would recommend watching this unite talk on scriptable objects -
     
  7. Gordon_G

    Gordon_G

    Joined:
    Jun 4, 2013
    Posts:
    372
    Thanks - that's a good video and I've already watched it multiple times in the past. Doesn't address my question though.