Efficiency of SendMessage

Hey all,

I’m working on a space shooter type application, and have read some things on Answers about SendMessage being relatively slow.

I’m at a stage where I’m working on optimizing possible bottlenecks and awkward areas in my code, and having read this, I’m looking into my (possible) overuse of SendMessage in some cases.

In one situation I have my simulated projectiles colliding with some game object. At this point, I use something like this in the OnCollisionEnter event.

whatIHit.SendMessage("ApplyDamage",damage)

This transmits the damage information to the various controllers on the object that was hit (generally one of three health controllers, a shield FX controller and a sound controller).

Would it be faster to instead use a GetComponent call to get the health controller and call an ApplyDamage method directly on it? This controller would have cached references to all the other controllers so would just call ApplyDamage on all of them.

whatIhit.GetComponent(GenericHealthController).ApplyDamage(damage)

and on the controller:

function ApplyDamage(dmg:float)  {
SubtractHealth(dmg);
shieldFXController.ApplyDamage(dmg);
soundFXController.ApplyDamage(dmg);
}

The issue I can see there is that I have different types of health controllers so would need to check for each of them - is checking for them and them getting the appropriate one still faster than SendMessage?

The second situation, I’m using SendMessage every frame to transmit player input to the player ship. I highly suspect that this is a no-no if SendMessage is slow, but how slow exactly is it? This is what essentially goes on.

if (accelerate key is pressed) 
SendMessage("Addthrust")

In general I’m a pretty newby programmer (artist background), so I’d appreciate the help, or suggestions on how to do any of this better

My advice, run a benchmark with both so that you can really see the differences.
Here is an extract from the forum :

“So, on my (rather old) machine, a loop of 5000000 SendMessage calls, using an empty function, takes 9.872 seconds. A loop of 5000000 direct calls (using a cached GetComponent) takes .024 seconds. Therefore, direct calling seems to be about 400 times faster, although considering that the loop itself takes up some time, it’s probably more than that.”

This was run by Eric5h5 (hopefully courtesy of him)

Here is the original post http://forum.unity3d.com/threads/38094-Is-SendMessage-really-that-bad

Both have to go through many components in the game object to find the right one.

It is recommended to cache GetComponent when you need to perform the action many times.

    function Start () {
        var foo : MyScript = GetComponent(MyScript) as MyScript;
        foo.DoSomething();
    }

So my point of view, SendMessage may look easier to implement and is fine once in a while, but GetComponent is more appropriate for repeated actions and is fast enough if cached.

The cache is a small memory physically closer to the CPU that allows faster access to the data.