OOP dilemna
BlitzMax Forums/BlitzMax Programming/OOP dilemna
| ||
| I have these classes: Entity Light Extends Entity PointLight Extends Light SpotLight Extends Light Then I have a list of Light classes that affect a certain entity. I want to get some information about the light that is specific to one of the light subclasses, like the cone angle of the spotlight. I can't get at it easily, because the cone angle parameter is part of the "SpotLight" type, and I have the handle to the Super "Light" type. I can easily solve this by moving the coneangle field from the "SpotLight" type to the Super "Light" type, but it's not very nice. Any suggestions? |
| ||
| Check if it casts to SpotLight? |
| ||
What Azathoth means:Local spot:SpotLight = SpotLight(YourLight)
If spot Then
Print "Is a spotlight"
EndIfI'd prefer to place a class ID in the light entity to check before casting just because it would save time. However, I'm more in favor the component-object entity system than the current means of just extending things each time I need some new functionality. Instead of doing that, I just add a new component to its list of components and the functionality is added, so it's like I can have multiple inheritance rather than extending a single existing type. There's a crude example of this in my code archive entries and it's covered in Game Programming Gems 5. |
| ||
| That's an interesting way to test a type. In Delphi you can say "If Yourlist is Spotlight" which is pretty cool. Another way is to have a tag in the Light field which defines the type of type it is, then you can just query that later on. |
| ||
| Would suggest the second way as well. Create a field _type:String in your entity class and any extended class assings usefull data to that in its default constructor (method new() ) |
| ||
I'm not sure if this solves your problem but it might. You can loop any object type in your Tlist.
'Loop all SpotLights in your LightList
For local Light:SpotLight = Eachin LightList
If Light.Radius > Light.Cone
'do what you want
Endif
Next
'Loop all PointLights in your LightList
For local Light:PointLight = Eachin LightList
If Light.PointCenter < Z
'Do something :)
Endif
'In this case Light is always of the Pointlight type.
Next
|