Turning an object into an extended type?

BlitzMax Forums/BlitzMax Programming/Turning an object into an extended type?

JoshK(Posted 2007) [#1]
Type TBody
EndType

Type TVehicle Extends TBody
EndType

I have a bunch of CreateBody() commands like CreateBoxBody() CreateConvexHullBody(), etc. It would be nice to be able to use these commands to create the body, and then create a vehicle from that body, but actually turn the body into a vehicle, instead of copying it:

body:TBody=CreateBoxBody()
CreateVehicle(body:TBody)
vehicle:TVehicle=TVehicle(body)


fredborg(Posted 2007) [#2]
You do need a convert function to upgrade from a base type to an extended type.

Alternatively, you might be better off storing the body inside the vehicle, like:

Type TBody
EndType

Type TVehicle
  Field body:TBody
EndType

body:TBody = CreateBoxBody()
vehicle:TVehicle = CreateVehicle(body)


Or you could even do an extended object, which actually uses another "TBody", like this:
Type TBody
  Method DoSomething()
EndType

Type TVehicle Extends TBody
  Field body:TBody
  Method DoSomething()
    body.DoSomething()
  EndMethod
EndType

body:TBody = CreateBoxBody()
vehicle:TVehicle = CreateVehicle(body)

But at the end of the day simply having a converter/copier function in your extended type is probably the best solution.


JoshK(Posted 2007) [#3]
Previously I was using your first method, but it sort of bugged me. I am writing docs, and it seems logical for the vehicle type to extend the TBody, like how my player controller type does.