The title summarizes my question as quickly as possible. I have a pastebin post with a much more detailed version of the question if it helps to explain what I want.
Essentially, if the class Foo subscribes to a delegate event, and the class Bar has a Foo variable, I want a method on Bar to run when Bar’s Foo instance’s “event handler” method fires. This is made confusing to me because:
Foo can’t need to know anything about Bar
Foo must hand the delegate event’s arguments to the method on Bar.
Does any of this make sense? Does needing this functionality mean I’m doing something silly?
Thanks for your consideration,
Your code example makes no sense at all You can’t override a function on a foreign instance of a totally different class. You can only override functions when you derive your class from the other. Your case has nothing to do with inheritance.
If your “SomeBehavior” script wants to get an event from the “MonoEntity” instance, the MonoEntity class should offer a delegate itself to which your SomeBehaviour can subscribe to.
public class MonoEntity : MonoBehaviour
{
public System.Action<PhysEventData> OnPhysicsReaction;
void SubscribeToReactions()
{
ReactionManager.PhysicsEvent += ReactionManagerReceiver;
}
public void ReactionManagerReceiver(PhysEventData PED)
{
if (OnPhysicsReaction != null)
OnPhysicsReaction(PED);
}
}
public class SomeBehavior : MonoBehavior
{
public MonoEntity myMonoEntity;
void Start()
{
myMonoEntity.OnPhysicsReaction += PhysicsHandler;
}
private void PhysicsHandler(PhysEventData PED)
{
// ...
}
}