A problem related to custom types
BlitzMax Forums/BlitzMax Programming/A problem related to custom types
| ||
Okay, the title is a bit fuzzy, but I couldn't think of anything else. Here's the scenario: Say I have a base type: Type Base Field Stuff Method Load() Abstract EndType And a default type, let's say: Type Default1 Extends Base Method Load() 'Load stuff EndMethod EndType Now I have a bit of code that checks a list and decides what type to assign, for example: Function DoStuff() Local t:Base For t = EachIn MyList t.Load() Next EndFunction This whole list is created automatically and types are assigned by the file extension. My problem now: How can I allow a user to easily insert his own type derived from the base one as to allow the initialization process to correctly assign his type to his preferred extension? I suppose some pseudo code would look like: Function GetCustomTypes() Local t:LoadingType For t = EachIn TypesList If (file.extension = t.Extension) Local x:[Custom type] = new [Custom type] <--- I have now idea how or if I can do this 'Fill fields MyList.AddLast(x) Next EndFunction If this doesn't make sense I'm sorry, but this is as readable as I can make it :) |
| ||
I think the user would have to create a custom type extending your base type, and they must add their own `create` method which returns a new type, which is cast back to a Base type, and then you fill in the fields. Or you might look into reflection. |
| ||
Well, as usual, I come up with a solution soon after I posted the question. Thanks for the answer. That crossed my mind, but I want to make it as easy as possible for the "user" and having to create an new type, with a new method was more than I wanted :) I made a predefined "custom" type that holds a Data field of the type "Object" and another field that's a callback to a loading function defined externally, which should return an "Object" and have a "String" (the path) as a parameter. Well, expect to see this code in use soon. I have to clean it up, put comments and maybe make it bmax docs compatible. :) |
| ||
For that you need the reflection module. But that can be a bit of a hassle. Instead, a factory pattern might be a better choice. Check out http://www.gamedev.net/reference/programming/features/objectfactory/ to learn about it. I took a similar approach and it's working nicely. Especially when you decide to export the object factory to your scripting language of choice. Also, your For EachIn can be written easier. For Local t:LoadingType = EachIn TypesList Next |