- Home /
Too subjective and argumentative
Delegate or Event: Reacting to another object's collision functions
Hey, folks!
I'm new to the fancier aspects of C#. I'm trying to figure out when to use Events instead of delegates.
I have a character who swings around a sword with a hitbox attached to it. When the hitbox collides with something, I want the character class to do the damage computations.
I currently have this working by firing off a delegate from the weapon-hitbox's OnTriggerEnter function. When the character class wakes up, they just add a function to the hitbox's trigger-enter delegate.
I haven't worked much with events. Would I have been better off with events?
The hitbox currently has the following code:
public delegate void OnTriggerEnterDelegate(Collider Other);
public OnTriggerEnterDelegate OnTriggerEnterSteps;
void OnTriggerEnter(Collider Other){
if(OnTriggerEnterSteps!=null){
OnTriggerEnterSteps(Other);
}
}
The character code then assigns the damage function like so:
WeaponHitBox.OnTriggerEnterSteps+=MeleeStrikeTarget;
How would you even add it as an event? A delegate is just a fancy way for you to call a function to be named later. But I$$anonymous$$HO events need to be created and dispatched by the engine. Since Unity doesn't have an OnOtherColliderEnter(Collider watchThis, Collision col)
, adding a new event seems impossible.
The way you have it seems to fake watching someone else's collision in the only way possible.
"I want the character class to do the damage computations." doesn't sound like a good idea to me. Send whatever data you need, via an event, to whatever needs to react.
Answer by Jessy · Jun 07, 2013 at 04:09 PM
You don't need to define your own delegate for this. C#'s System namespace already has an appropriate one created for you: Action. You're probably never going to have to define your own delegates.
Do not use PascalCase for fields. PascalCase would be used for properties, or events, which have similarities to properties behind the scenes. Do not use public fields. There is generally no reason to have public delegates; definitely use events for better encapsulation.
Your system doesn't sound great, to me, but given what you have, here's what you'd want to change to:
public event Action<Collider> OnTriggerEnterSteps;
The rest of the code could stay the same.
Thanks again, @Jessy. I'll keep those tips in $$anonymous$$d.
This doesn't seem like the appropriate site for this; I don't see a clear question that can be answered. You should delete this and take it to the forum after reading Stack Overflow for a few hours. Posts on that site from years ago generally get me up to speed on whatever program$$anonymous$$g topics I'm learning.
The answerable question was "Would I have been better off with events?", which at the time did not seem like a particularly helpful summary.
The expected result was that someone would say something along the lines of "no, events should only be used for UI buttons or [situation x] in unity" or perhaps "Oh, you can already just use 'OnTriggerEnter' as [Unity Concept Y]".
Your suggestion to send an event was actually even better, because it pointed out a better way to implement the problem.
Would a question of "How do I implement a sword impact?" be more appropriate? I thought that putting some effort into it first might make it easier on folks answering the questions.
I'll go check out the forums.
Also, the delete button is not working because there are some answers that cannot be deleted. What is customary in this situation?
Can you close it? I can, if that's what you think we should do. I feel like this is something that will lead to an open-ended journey.
Answer by sportsfan78 · Jun 08, 2013 at 07:30 AM
If I understand this page correctly, I don't have enough rep to close my own question. http://answers.unity3d.com/page/faq.html
Please close it if you get the chance.
Follow this Question
Related Questions
Method doesn't get implemented after subscribing to Func 1 Answer
Is there any event or delegate that is called with OnValidate() ? 0 Answers
Unity Events - Subscribe and Unsubscribe Odd Behavor 0 Answers
Help to understand events and delegates (Game Manager) 2 Answers
Using Delegates for separating UI and Logic, are these in the right place? 1 Answer