Game Development Community

How to add or remove components at runtime

by Diego Santos Leao - GameBlox Studio · in Torque X 2D · 11/20/2009 (6:17 pm) · 4 replies

I have come to the conclusion that I can't add or remove components at runtime in Torque X 2D/3D. Is this really the case?

Because this would defeat part of the purpose of components, and it is very upsetting. If true, I would like to request this feature right now :)!

#1
11/20/2009 (7:43 pm)
Pretty much... It is true that components are not allowed to be added or removed once an object has been registered (you can still add stuff at runtime up until the object is registered). If you genuninely need to add/remove components after an object is registered, rather than simply enabling/disabling them, then consider whether your design needs changing, or perhaps writing a component that uses a component container to have a list of components that you can modify at runtime.
#2
11/20/2009 (7:47 pm)
I add components all the time:

MyComp comp = new MyComp();

comp.thing = something;
comp.foo = somethingelse;

MyT2DObject.Components.AddComponent(comp);

I have never tried to toss one.

As Duncan has posted while I was writing, this is before registering the object.


#3
11/20/2009 (8:17 pm)
Yeah, I want to change it after it is already on screen (that is, registered). What we do today is to enable/disable them.

Possible usage for adding or removing components at runtime:
* using a scripting language to change the behavior of your scene object
* Maybe you want to try a component on an object that you didn't though when you ran the game.
* Maybe you are trying out new enemy behaviors as part of your prototyping process, and you want to be able to add components instead of adding all of them with enable=false at the beggining of the level - which will cause your scene to run slowly if you have many instances of that object.

Instead of asking "why" adding/removing components, we should ask "why not?".

I do believe that we can live without this functionality by putting an "Enable" property on every component and use it to mix components at runtime, but the usability of this workaround sucks anyway for prototyping.
#4
11/20/2009 (10:54 pm)
Without knowing the details of what you are trying to do all I can suggest is that you mod the engine, if enabling/disabling of components would suck too much. I doubt that it would be a feature that gets added as it's not typical to need to swap components in and out at runtime.