Extended Types and Create()

BlitzMax Forums/BlitzMax Programming/Extended Types and Create()

Macguffin(Posted 2008) [#1]
Hey all,

Using the Grey Alien Games Framework, I've got a type for my playing card (TCard) extended from TSprite (has lots of good stuff for manipulating images).

TSprite has its own Create() function. When I try to redefine that in TCard, I get a compile error, "Overriding method differs by type." I tried resolving this by overloading TCard.Create()'s parameters, but it doesn't look like is supported in BlitzMax.

So... I know I could just run TSprite's Create() and then do an Init method for other things, but that causes problems for the way I currently have things set up. It'd be much easier to make this work if there was some way I can pass in extra info when calling TCard.Create().

I'm guessing one way around this is to, within TCard.Create(), call Super.Create() with some dummy parameters, cast that to create the TCard, and set all the fields for real. But that seems really kludgey. Anyone have any ideas?

Thanks. I can post code if needed, I just figured it wasn't worthwhile.


Macguffin(Posted 2008) [#2]
Think I've gotten around the problem by just renaming the Create() function to something else - but if anyone can shed light on how to do it with Create(), I'm very curious.


SebHoll(Posted 2008) [#3]
Unfortunately, BlitzMax doesn't support overloading so unless your extended type's methods have exactly the same parameters as your base type, you will get compiler errors...


Arowx(Posted 2008) [#4]
One option for a framework would be to use a lighweight Object as the creation parameter this would then allow extensions and get around this limitation.

Or you could use the Builder or Factory patterns, where you assign another object to create/setup the desired object. Sounds complex but can be very good for complex objects that need to be constructed and would otherwise require a very complex setup process. In effect you simplify the created object by letting another object create it.

E.g. Wall object made from lots of bricks in a pattern.
Bricklayer object creates bricks and places them in the pattern within the wall in the correct order.
Now the Wall object can concentrate on being a wall not a bricklayer as well.

But as Macguffin says just renaming the Create() method/function Construct() and then call Create() with the base parameters.