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.
public class InventorySO : ScriptableObject
{
[Tooltip("The collection of items and their quantities.")]
[SerializeField]
private List<Item> _items = new List<Item>();
public List<Item> Items => _items;
public ItemEventChannelSO pickUpItem;
private void OnEnable()
{
if (pickUpItem!= null)
{
pickUpItem += Add;
}
}
private void OnDisable()
{
if (pickUpItem!= null)
{
pickUpItem -= Add;
}
}
public void Add(ItemSO item)
{
_items.Add(item);
}
}
public class ItemEventChannelSO : ScriptableObject
{
public UnityAction<Item> OnEventRaised;
public void RaiseEvent(Item item)
{
if (OnEventRaised != null)
OnEventRaised.Invoke(item);
}
}
public class Item: Monobehaviour
{
public ItemEventChannelSO pickUpItem;
private void OnTriggerEnter([URL='https://docs.unity3d.com/ScriptReference/Collider.html']Collider[/URL] other)
{
pickUpItem?.RaiseEvent(this);
}
}
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.