Search Unity

Passing reference from GameObject to GameMaster okay?

Discussion in 'Scripting' started by padomu, May 9, 2014.

  1. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    Hi,

    Sry for the bad title, but just coudln't think of anything.

    My Game has 4x4 Fields. Each field represents on of 10 possible Objects. (Think of it as coins, cards whatever)

    This Object has a Number and some Bonusstats like: Multiplay by 2, add extra Time etc.

    The fields are prefabs, so each one uses the same script. The Objects are assign randomly.

    I also have a GameMaster script which should check if anyone has won etc. it also add the bonuses from the Objects like "extra time".

    But how do I connect it?

    Each of these 10 Objects are prefabs.

    I though of doing something like this:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class mainCoinLogic : MonoBehaviour {
    5.     [SerializeField] private TextMesh coin;
    6.     [SerializeField] private GameMaster gm;
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         coin = //get prefab and assign it
    12.     }
    13.  
    14.     void OnMouseDown() {
    15.         gm.AddChoosenCoin(coin);
    16.     }
    17. }
    Is this ok? In the GameMaster I could now calculate all new bonuses and whatever on the addchoosencoin and then throw it again or save it in an list/array for later things.
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    If it works, then it's okay!

    I would suggest a change though. You should have the GameMaster hold references to each mainCoinLogic. Each mainCoinLogic should have an event that announces when it was clicked. The GameMaster knows about all the mainCoinLogics, but the mainCoinLogics doesn't even know that a GameMaster exists.

    You're way can work too, but if you build an entire game like that you end up with the small little parts of your game relying on it's parent. You end up with every little class relying on all it's parents working, all the way up it's chain. One part breaking somewhere can break the chain and cause bugs all over the place.

    In object oriented programming, this is called Encapsulation or black boxing.
     
  3. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    I don't see the point of making the fields/mainCoinLogic members of the GameMaster. Whats the point of it?

    Do you mean, that I connect GameMaster and mainCoin Lgoci only via events? Anyway, the first question still remains.

    The way I did it, it is very interchangeable. You can call the AddChoosenCoin(prefab) from anywhere, you can add a new type of coin and call it from there and pass the prefab to it. The gameMaster doesn't care about anything but receiving the correct data.

    How I understand what you said:

    -make 16 members, each one holds one of the coins. (or a list, whatever) -- I think this is bad because it isn't generic but fixed
    -make an event which gets fired when someone clicks a field/coin, receive data and make calculations.

    o_O i think, I don't get your idea, it really makse no sense to me at all.

    I don't see why you think that the why I did it is a chain. I know what you mean which chain but here it isn't the case.
     
    Last edited: May 9, 2014
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Not sure what "fiels" is, coins?

    But yeah, the idea is to have a coin work by itself. Then you can reuse it easier and it doesn't rely on other classes to function properly.

    I would normally do something like this:
    Code (csharp):
    1.  
    2. public class mainCoinLogic : MonoBehaviour {
    3.   public event System.Action Selected;
    4.  
    5.   void OnMouseDown() {
    6.     if (Selected != null) { Selected(); }
    7.   }
    8. }        
    9.  
    Alternatively, I might have mainCoinLogic take a delegate, and have GameMaster provide a delegate to mainCoinLogic when it initializes.

    But I would really stress, if your code works... then it works!
     
  5. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    delete
     
    Last edited: May 13, 2014
  6. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    Needed to work myself a bit into events since I'm not used to use events. I still don't see how it is more independent.

    What I do is basically using a setter for setting data. By using events, I'm not more independent.

    It even feels wrong. I'd use events for generic things. stuff like OnClikc(); things which can be called from anywhere, but this is a strictly defined relationship, it never gonna change. It's just for passing the data around.
     
    Last edited: May 13, 2014