Hi, I was wondering if anyone has any experience with trying to substitute a SendMessage or BroadcastMessage call with a GetComponent(Script Name). I currently have explosions that use the SendMessage call to apply damage to game objects caught in it’s explosion radius. I’m wondering if it’s worth changing my collision check with something like:
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for(i=0; i<colliders.Length; i++)
{
var closestPoint = colliders[i].ClosestPointOnBounds(explosionPosition);
var distance = Vector3.Distance(closestPoint, explosionPosition);
var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
hitPoints *= explosionDamage;
var aiDmg : AI = colliders[i].collider.gameObject.GetComponent(AI);
if (aiDmg)
{
aiDmg.ApplyDamage(hitPoints);
}
}
Now, the issue with doing it this way is that I have to do a GetComponent with every possible Game Object that should take damage from the explosion (within range, of course.)
Seems like it could be alot of computing overhead to do a check for all possible Game Object types. But it still may be faster than a single SendMessage.
And if it is ‘slower’ does this mean it will actually slow down the performance of the entire game? or just take longer for the damage message to be recieved?
I just want to optimize as much as possible and if others have already been through this maybe they’d be kind enough to offer their wisdom?
Any insight appreciated. thanks.
You can’t do a single SendMessage anyway; you still have to go through them all and do a SendMessage to every object. Even if you use BroadcastMessage on an object hierarchy, Unity has to go through them all itself, so it’s never a case of just doing something once.
It’s all done in a single frame regardless; SendMessage functionality would be seriously broken if you didn’t know when it would be received. GetComponent is somewhat faster than SendMessage, but while there is some overlap in functionality, they are two different things, especially when using DontRequireReceiver. So you should use whichever one is more appropriate, regardless of performance. In those cases where the goal can be equally achieved by either, then GetComponent would be more optimal as far as speed goes.
Speaking of optimization, in that function, you should do the closestPoint, distance, and hitPoints calculations in “if (aiDmg)”…no point calculating all that stuff if aiDmg is null.
Hey thanks for all the great info. I’ll try the GetComponent thing and see if it makes a difference.
Regarding your final suggestion, if I wanted to move the closestPoint, distance and hitPoints calculations inside the if(aiDmg) conditional, I would have to duplicate those calculations for every other possible game object that would take damage as well. So if I had 3 types of enemies, plus the player involved in a grenade explosion for instance, those calculations would be repeated 4 times. (Unless there’s something I’m overlooking which is quite possible)
but wouldn’t it be better to have a boolean that enables explosion damage? Then you wouldn’t have to update the “if” check everytime you added a new enemy