Is it counting as Circular dependency when 2 files includes each others “usings” to subscribe to an event?
Any time you find yourself with
using NameSpaceB;
public namespace NameSpaceA
{
//
}
using NameSpaceA;
public namespace NameSpaceB
{
//
}
you have set up a circular dependency regardless of the activity.
Thanks.
If an have an AnimatorController that subscribes to other class events, I should put any animaton events to class they more related to excluding AnimatorController?
If by Animation events, you mean the ones set up in the Animation, you can put them in whichever class will be responding to the event.
For example: In the RPG course, we have an animation event in all of our attack animations “Hit”. In the Fighter class, we have a method Hit() that will respond to the animation event. Since animation events are fired using Unity and not our own code, there really isn’t any cross dependency happening.
If, however, you are using a pass through class to pass the events on to other classes (an example might be where the Animator is on a child object but the class that needs to respond to the event is on the parent object), then the pass through class should set up a regular event, which the class in the parent object will subscribe to. In this way the pass through class is the publisher and knows nothing about the class that subscribes, keeping it’s proper separation.
Example:
using UnityEngine;
namespace AnimationEventHandling
{
public class PassThroughHandler : MonoBehaviour
{
public event System.Action OnHit;
void Hit() //called by animation event
{
OnHit?.Invoke();
}
}
}
using UnityEngine;
using AnimationEventHandling;
namespace Combat
{
public class Fighter : MonoBehaviour
{
void Awake()
{
var passthrough = GetComponentInChildren<PassThroughHandler>();
passthrough.OnHit()+=OnHit;
}
void OnHit()
{
//damage the target
}
}
}
thanks for responding
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.