Game Development Community

Components for only certain object types?

by James Ford · in Torque X 2D · 06/27/2007 (4:23 pm) · 5 replies

Is there a way to make sure a particular component only gets added to objects of a particular type? Eg. You don't want a component that specifies animations for left,right,up,down to get added to a static sprite. Will or is this functionality be buildt into TGBX? My idea is to put an assertion in the component's onRegister to check the owner's type, but it would be nicer to be able to specify a valid owner type for a component then that component only appears in the drop down component menu for objects of that type.

#1
06/27/2007 (5:08 pm)
You can specify dependencies for other components (for example, you can specify that a component requires a physics or collision component) and the editor will post a message and automatically add them, but not for types. That seems like it would be a useful feature to add, though.

You could test the object type in OnRegister and not allow objects to register if they don't have that object type:

protected override bool _OnRegister(TorqueObject owner)
{
    if (!base._OnRegister(owner))
        return false;

    if (!(Owner is T2DAnimatedSprite))
        return false;

    return true;
}
#2
06/28/2007 (9:08 am)
Very cool... How can I setup a dependency between components?
#3
06/28/2007 (9:19 am)
Only if you add the other components when adding the first one (ie on the OnRegister or similar)

Otherwise you will ask for problems as the loading order is not predetermined, so a "needed component" might be loaded after the requesting component.
#4
06/28/2007 (9:53 am)
I don't remember the syntax exactly. The movement component in StarterGame is set up to be dependent on a physics component, so you could look there. It's an attribute on the class, something like [TorqueXmlSchemaDependency].

@Marc - Components are all added to their owner before OnRegister is called on anything. So, Owner.Components.FindComponent will find a component even if it isn't registered yet.
#5
06/28/2007 (11:47 am)
That indeed is good to know
Thank you.