In the observer pattern, specifically with Unity game dev, is it not possible to subscribe to events from specific instances of the same class? A simple example I came up with to explain: I have a class Dog which has a method "Bark" which does some stuff and also raises the "OnBark" event action. Dog has been instantiated twice, one golden retriever and one husky. The golden retriever is the leader of the pack and wants to subscribe specifically to the husky's bark event. How can I make this happen? Do they need to be different classes altogether?
Specifically, I am working on a sports video game (Lacrosse) that involves two teams of opposing players, and so of course I will need to implement a lot of "AI" CPU logic to play along with / against the person playing the game. The teams of players are instantiated from the same class and observe some of each other's actions depending on the context of the game, their position, etc. I can't figure out how to make an individual player instance subscribe to another's events.
Code:
First, the SimpleController class of which each player on the field is an instance. It has two arrays referencing the player's teammates and the players on the other team.
public class SimpleController : MonoBehaviour
{
public SimpleController[] teammates;
public SimpleController[] otherTeam;
// the brain of the player, which gets necessary info from this parent controller
public Brain PlayerBrain;
}
Next, the Brain class, within which I was hoping to include some simple event actions. This class is for handling logic of the players being controlled by CPU. I imagined that they can be raised by a player based on his choices and subscribed to by certain other player "brains" to react if they need to (i.e. the goalie won't subscribe to a "pass event" from his own teammate John, but an opposing player defending John certainly would want to react to a pass made by/to John).
public class Brain : IBrain
{
// Reference to parent SimpleController for teammates and opposing team members etc.
private SimpleController _player;
// event raised when player's brain decides to pass the ball
public static event Action OnPassEvent;
// method to call when a different SimpleController's brain passes the ball
private void reactToPass() { //.......// }
public Brain(SimpleController controller) {
_player = controller;
// Pretend I'm goalie -> subscribe to pass events for each of the other team members
foreach (var adversary in _player.otherTeam) {
adversary.Brain.OnPassEvent += reactToPass;
}
}
}
I know this is wrong. Is the logic incorrect? Is there a better way to do what I'm trying to do?