Search Unity

Unity Events + Scriptable Objects + Multiplayer - Game Architecture Question

Discussion in 'Game Design' started by skagla, Jan 19, 2021.

  1. skagla

    skagla

    Joined:
    Sep 18, 2015
    Posts:
    9
    Hi,

    I have a question about how to organice my game.

    I was studying the open project (chop chop) and really liked the way events are handled:
    There they are useing scriptable objects as connecting objects.
    Here is some simplyfied example code:
    The InventorySO and the Item have the same ItemEventChannelSO
    The Item OnTriggerEnter gets triggerd and invokes the RaiseEvent in the connecting ScriptableObject. The InventorySO subscriped to that "Event" and adds the Item to its Items-list.

    Code (CSharp):
    1. public class InventorySO : ScriptableObject
    2. {
    3.     [Tooltip("The collection of items and their quantities.")]
    4.     [SerializeField]
    5.     private List<Item> _items = new List<Item>();
    6.  
    7.     public List<Item> Items => _items;
    8.  
    9.     public ItemEventChannelSO pickUpItem;
    10.  
    11.     private void OnEnable()
    12.     {
    13.        if (pickUpItem!= null)
    14.         {
    15.             pickUpItem +=  Add;
    16.         }
    17.     }
    18.     private void OnDisable()
    19.     {
    20.        if (pickUpItem!= null)
    21.         {
    22.             pickUpItem -=  Add;
    23.         }
    24.     }
    25.     public void Add(ItemSO item)
    26.     {
    27.         _items.Add(item);
    28.     }
    29. }
    Code (CSharp):
    1. public class ItemEventChannelSO : ScriptableObject
    2. {
    3.     public UnityAction<Item> OnEventRaised;
    4.  
    5.     public void RaiseEvent(Item item)
    6.     {
    7.         if (OnEventRaised != null)
    8.             OnEventRaised.Invoke(item);
    9.     }
    10. }


    Code (CSharp):
    1. public class Item: Monobehaviour
    2. {
    3.  
    4.   public ItemEventChannelSO pickUpItem;
    5.  
    6.   private void OnTriggerEnter([URL='https://docs.unity3d.com/ScriptReference/Collider.html']Collider[/URL] other)
    7.    {
    8.    
    9.    pickUpItem?.RaiseEvent(this);
    10.    }
    11.  
    12. }
    I think it is a realy clever way to connect objects, but what I can't figure out is, how to use such a system in a multiplayer envirnoment.
    For example, if a game has 100 players does every player neet to have it's own ItemEventChannelSO ScriptableObject?

    I hope I formulated the question understandable, otherwise just let me know and I'll adjust it.
     
    Last edited: Jan 19, 2021
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Almost certainly yes. Those will be local, client-side objects.
     
  3. skagla

    skagla

    Joined:
    Sep 18, 2015
    Posts:
    9
    So, this "design pattern" is only usable for singleplayer? What would be a good alternative for multiplayer?
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    You can adapt them to multiplayer. But the design pattern will only be a small part compared to the part that makes it work with multiplayer. I recommend making a few very small multiplayer prototypes first before thinking about design patterns. Just get the multiplayer working however you can. This will give you a deeper insight into how to implement this design pattern if you choose to.
     
  5. skagla

    skagla

    Joined:
    Sep 18, 2015
    Posts:
    9
    thx for the tip, but it's more of a general question if it's possible to adapt the design pattern not about multiplayer. It would be the same problem if there are multiple characteres controlled by one player.
    Do you think it's possible to do with the scriptable object approach?
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Using ScriptableObjects, or more likely you're referring to ScriptableObject assets, is just one way of implementing the design pattern. It's just an observer. If you have 100 local player characters, you can either create 100 objects at runtime or include the the player ID with the event. In either case, there's probably no reason to use ScriptableObjects for them.