Can I somehow print inherited field names?

BlitzMax Forums/BlitzMax Programming/Can I somehow print inherited field names?

SofaKng(Posted 2007) [#1]
I'm trying to make a base game object class that will be inherited by all other game objects. In this base class I'd like to have functions for getting the object's name, etc, etc.

Please take a look at the following short bit of code:

Type Test1
  Field Name : String = "Test1 type!"
	Method GetName : String()
		Return Name
	End Method
End Type

Type Test2 Extends Test1
	Field Name : String = "Test2 type!  Alright!"
End Type

Local MyObj : Test1 = New Test2

Print a.GetName()  ' displays "Test1 type!"
Print a.Name  '  displays "Test1 type!"


As you can see, the field called "Name" isn't inherited like methods are.

Is there some how I can inherit fields?

My point in doing this is so I can typecast any game object to a TGameObject (my base class) and get the objects REAL name.

I don't want to have GetName()/SetName() methods in every single object I create. (...because I want to also have a field called "ID" and other fields I'd like to "inherit")

Any help is appriciated!


Gabriel(Posted 2007) [#2]
Your code is kinda weird. You declare an object called MyObj and then refer to it with a. I'm going to assume you meant to be consistent and that a and myobj are the same object.

The problem is that you cast MyObj as a type1, and all type 1's have a field name which has the value "Test1 type" in it. So of course that's what it's going to print. The object is whatever you say it is. If you say it's a type1, you won't even be able to access a field or method unless type1 has one too.

I do what you're trying to do by having the method return the name as a string literal.

Eg:

Type1
   Method GetName:String() Abstract
End Type

Type2 Extends Type 1
   Method GetName:String()
      Return "Type2"
   End Method
End Type


There may be other ways. There may be better ways. But that works fine for my purposes.


Dreamora(Posted 2007) [#3]
The problem with above code btw is that you do redeclare the field name. This means that the base class and the extended have distinct name (which means that the extended actuall has name and a renamed unaccessable name on that class)


Perturbatio(Posted 2007) [#4]
Type Test1
  Field Name : String = "Test1 type!"
	Method GetName : String()
		Return Name
	End Method
End Type

Type Test2 Extends Test1
	Field Name : String = "Test2 type!  Alright!"
	Method GetParentName:String()
		Return Super.GetName()
	End Method
End Type

Local a : Test2 = New Test2

Print a.GetParentName()  ' displays "Test1 type!"
Print a.Name  '  displays "Test1 type!"




ziggy(Posted 2007) [#5]
@Perturbatio:
Print a.Name  '  displays "Test1 type!"
This sounds like a BlitzMax bug to me. Or the overried field is accesible by the extended object directly (wich I think is not a good idea), or the default value for the overriding field is never asigned.

I suggest:

Type Test1
  Field Name : String 
  Field FullName : String
	Method New()
		name = "Type 1"

		'This will never be executed in this 'base' type:
		if super<> null then	fullname = super.fullname + "\" + name  
	End Method
	Method GetName : String()
		Return Name
	End Method
End Type

Type Test2 Extends Test1
	Method New()
		name = "Type 2"
		if super<> null then	fullname = super.fullname + "\" + name
	End Method
End Type

Local a : Test2 = New Test2

Print a.Name  
Print a.FullName

Not sure if it works well, I'm not at home now and doesn't have blitzMax to test it, but this way we can have the current class name, and all its genealogic tree :D


SofaKng(Posted 2007) [#6]
ziggy: That's what I ended up doing.

Thanks for the replies everybody!


Perturbatio(Posted 2007) [#7]
This sounds like a BlitzMax bug to me. Or the overried field is accesible by the extended object directly (wich I think is not a good idea), or the default value for the overriding field is never asigned.


If you're saying what I think you're saying then, no, I just couldn't be bothered updating the comment.


rdodson41(Posted 2007) [#8]
In a Test1 there is one name field but in a Test2 there are two, the Test1 name field and the Test2 name field. So since the variable a is of type Test1, when you say a.name you get the Test1 name field. If you declare a as a Test2, then when you say a.name you'll get the Test2 name field. If you want you convert everything to a TGameObject and know what the "real" name is then you will have to use an abstract method as Gabriel said.


SculptureOfSoul(Posted 2007) [#9]
It's really a limitation of the message passing model of object orientation, not a problem with BMax itself.