Game Development Community

How do I get a reference to the Force object I created in TXB?

by rwillis · in Torque X 2D · 10/23/2007 (10:43 pm) · 2 replies

I created a force in TXB in the force component, gave it a name, and it seems to work, but how do I reference that Force object in code? I tried using "TorqueObjectDatabase.Instance.Findobject" but it didn't work. Thanks.

#1
10/23/2007 (11:40 pm)
As far as I know, the individual forces can't be found by the TorqueObjectDatabase because they are not derrived from TorqueBase. Although the component is, you can't name this component and then search for it by name. You will need to find the scene object, then get the force component, then find the right force. For example, if you have a scene object in Torque X Builder, named MyObject, and it has a T2DForceComponent, and it has a force attached, named MyForce, then you could do the following:

T2DSceneObject objectWithForceComponent = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("MyObject");
T2DForceComponent forceComponent = objectWithForceComponent.Components.FindComponent<T2DForceComponent>();
 
foreach (T2DForceComponent.Force aForce in forceComponent.Forces)
{
    if (aForce.Name.CompareTo("MyForce") == 0)
    {
        float val = aForce.MaxStrength;
    }
}

This is about as direct as you can get to an individual force. Hope that helps...

John K.
#2
10/24/2007 (6:01 am)
Yes, that helped much. Thank you. So to change the strength in code of a Force object created in TXB I would have to use the ValueInterface that it creates? What about if I want to change its direction in code? Or should I just create the force object in code to begin with (so that I have a reference to it so I can change the properties)?