Thursday, May 29, 2008

Subclassing or parenting? (Shake)

When you reuse some class and make a new class in c++, you have two ways: making a subclass or having an instance of the class in your new class. The latter is considered to be the better one in general, unless there's conceptual "is-a" relationship.

I realized I also have the same two options when I am making a new Shake node from an existing one. Subclassing, or having an instance of the existing node as a child in my node. And this time I need to choose one of them in more practical way.

When I subclass a node, everything, UI, serialization, ... is o.k. but you need to make a creator function from the scratch, (see my previous post). I need to be careful not too miss anything, e.g. notify(), it's really time consuming because I need to observe the existing node closely.

When I use parenting, making a creator function will be probably easier if the number of arguments are fixed, but I need to connect plugs, need to implement some to have on screen control work as well with the new node, etc. And I see no way of calling a creator function inside my creator function if it accepts variable number of arguments. Maybe C language limitation? Now I'm stuck here...

I wish I had Shake source code, then subclassing would be really easy.

Jun 11,
After all I concluded I could do neither subclassing nor parenting. For a simple node like Move2D, I can guess exactly what the creator function does, and I can recreate it. But what I tried to use was much more complicated node (namely MultiPlane). I can observe its behavior closely and imitate the standard behavior, but how can I be sure that my creator function does exactly the same as what the standard one does? So I cannot make subclassing. Then my option left is parenting, but I cannot use it as well because parenting means I create MultiPlane object somewhere inside the creator function for my custom node. MultiPlane creator function has variable number of arguments so my creator function also needs variable number of arguments to pass them to the internal MultiPlane's creator function. How can I do that? 's printf() has a sibling vfprintf() that takes va_list but Multiplane creator function doesn't. It's not a good idea to use assembler here. Actually I came up with another idea. First I can make a standard multiplane and customize inside node structure after. But MultiPlane is MultiPlane and when it's saved and loaded, MultiPlane's creator function gets called. There's no place to hold additional parameters, sigh.

No comments: