SendMessage Vs Caching

I’m sure they both have their pros and cons…but in the resource-limited scope of iphone programming, which one of them is generally the best choice? Which of them is it best I design my games around?

By sendmessage i mean SendMessage, BroadcastMessage and SendMessageUpwards.

The manual states that sendmessage is inherently slow.

But does caching require significantly more memory than sendmessage (especially when caching a big object)? or does caching just hold a pointer to the object that isn’t “exactly as big” as the big object that needs to be accessed?

Sorry, I guess I should have mentioned my priority. I’m more interested in a low memory footprint than fps right now (though fps is always nice).

By caching, you mean assigning a GameObject to a var? That’s just a reference (or pointer, like you say), not a copy of the entire object. The size is inconsequential, unless you have a gazillion (well, thousands) of them.

Awesome. thanks for the info.

May it hereby be established throughout the four corners of this forum…a decree that all Unity iPhone games must be built around caching and not message sending.

Amen

Well…you should use whatever’s more appropriate. There are things that SendMessage et al can do that are not easily duplicated by calling functions directly, and if you’re not calling it that often, saving a few milliseconds is not worth the increased code complexity. You can’t really say that one is inherently better than the other in all situations.

–Eric

I just tried a massive SendMessage() for a generic purpose, on just 4 scripts x 2 gameObjects, and even in the editor it was outstandingly slow to load :sweat_smile:
(checked if it came from a bad code, but didn’t)

SendMessage (et al) are probably best used in random situations which may be hard / impossible to predict the call receiver and are not absolutely required to have a immediate effect (like applying damage to some random object struck by bullets where the bullet sends a message to apply damage to anything it hits).

For performance reasons, the performance documents go a step further and say you should cache (when possible/practical) local references to often used getComponent calls (including GameObject.[some Component] like gameObject.collider). I haven’t tried to measure the performance differences, but I did take the recommendation to heart.

Hope this helps,

Galen