Game Development Community

When to use cached interfaces?

by AIDan · in Torque X 3D · 05/23/2009 (12:48 pm) · 1 replies

Something that still eludes me is when to use cached interfaces. Isn't it always better to use FindComponent instead and retrieve the value that way?

I saw that this must be done for mount points on meshes.

What I find really strange is this:
TorqueInterfaceWrap<HealthComponent> healthComponent = _collisionInfo.Target.Components.GetInterface<TorqueInterfaceWrap<HealthComponent>>("healthComponent", "");

Why is that better than the following?
HealthComponent healthComponent = _collisionInfo.Target.Components.FindComponent<HealthComponent>();

#1
05/28/2009 (2:40 pm)
I think it has something to do with using TorqueSafePtr's. Maybe you should only do it if you intend to keep the reference longer than the method you're in?

Just a guess.

Edit:

Another look suggests this is the case. TorqueInterfaceWrap wraps TorqueInterface, which has a reference to the component and a TorqueSafePtr to the component's owner, suggesting that you should check the IsValid property before accessing Wrap. This is preferable so that if a TorqueObject is removed from the graph it can be disposed without you needing to release all your references to the component, thus creating potential memory leaks.

Of course, I could be completely wrong :)