Search Unity

Code design suggestions

Discussion in 'Scripting' started by PizzaPie, Jul 11, 2019.

  1. PizzaPie

    PizzaPie

    Joined:
    Oct 11, 2015
    Posts:
    106
    The set up: I ve got a Pickable asbstact class and three pickable children (Coin, SpeedBoost, FinishLine*). The base class defines whats happens on pick up on itself (changes the state of the pickable object) and takes care of some global events. (pickables are picked with a trigger event)

    The dilemma: each one of the pickables should have a different effect on the Player (thus change its state). So my question is How should I change the state of the player without breaking encapsulation (not sure if the correct term)?

    Solutions (I see):
    -Direct, check against the type of the pickable inside the Player object and change its state accordingly (makes inheritance of pickables useless).
    -Define appropriate methods for each one inside the Player class and call said methods inside the pickables (most probable).
    -Define the effect method inside each pickable and call it internally while passing the player object as parameter.
    -Invoke an event on pickup, but there will be partial coupling.

    It doesn't matter much which one I choose, the game is fairly small and is not going to get expanded, but I'd love to hear other ways this can be accomplished. Overengineering suggestions are most welcome.
    Cheers
     
  2. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Have you considered using interfaces in conjunction with enumerators? You could create an IPickable interface with a PickableType enum. Whenever an object implementing the interface is picked up, you'd switch to the appropriate logic based on the value of the PickableType enum.

    Determine on how much logic is actually happening in the child classes, you could very well discover that it might be possible to remove child classes altogether and just handle everything with a single PickableObject.cs class, while using the PickableType enum to indicate which effects should get triggered.
     
  3. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    I would also consider using an interface :)
    Also having the pickable object modify the player's state is fine. Moving what the pickable object does from the player code to the pickable code is a good idea, since it allows easy addition of features by just adding a new pickable class, without having to mess with the player code at all. OOP is beautiful.

    Code (CSharp):
    1. interface IPickable {
    2.    void OnPick(Player player);
    3. }
    4.  
    5. //Pickable that kills player:
    6. class KillerPickable : IPickable {
    7.    void OnPick(Player player) {
    8.       player.Kill();
    9.    }
    10. }
    11.  
    12. //Pickable that gives gold:
    13. class KillerPickable : IPickable {
    14.    void OnPick(Player player) {
    15.       player.GiveGold(12);
    16.    }
    17. }
     
    Last edited: Jul 11, 2019