Accessing a base types functions

BlitzMax Forums/BlitzMax Beginners Area/Accessing a base types functions

QuickSilva(Posted 2009) [#1]
I have a TPlayer type that extends a TEntity type. Now, the TEntity type contains a function called Create() which create a new entity. This is fine if I do something like Entity:TEntity=TEntity.Create() but it doesn`t seem to work if I do Player:TPlayer=TPlayer.Make()

Why won`t this work? Shouldn`t the extended type contain all of the base types functions and fields too? I do not want to have a separate Create() function inside of the TPlayer type in this case so is there a way of getting this to work?

Am I doing something wrong?

Thanks for any help,
Jason.


Brucey(Posted 2009) [#2]
Change it to a Create method.

Then you can do something like player = TPlayer(New TPlayer.Create())

(you will of course need to change the internals of the method to work from inside the object)


QuickSilva(Posted 2009) [#3]
Thanks Brucey, thats exactly what I was looking for.

Jason.


Otus(Posted 2009) [#4]
BlitzMax supports covariance in method return values, so you can do:
Type TEntity
  Method Init:TEntity(...)
    ...
    Return Self
  End Method
End Type

Type TPlayer Extends TEntity
  Method Init:TPlayer(...)
    Super.Init(...)
    Return Self
  End Method
End Type

Local e:TEntity = New TEntity.Init(...)
Local p:TPlayer = New TPlayer.Init(...)



QuickSilva(Posted 2009) [#5]
I`m still a little puzzled. Why does the following fail to run?

SuperStrict

Type TEntity
	Global List:TList=CreateList()
	
	Field X:Int
	Field Y:Int

	Method New()
		ListAddLast(List,Self)
	End Method

	Method Make:TEntity()
		Local Entity:TEntity=New TEntity
		
		Return Entity
	End Method
End Type

Type TPlayer Extends TEntity
	Field Z:Int
End Type

Local Player:TPlayer=TPlayer.Make()


I`m obviously missing something? I see the post above works fine but do I have to add another Method to TPlayer to get this to work as it is already in the base type? I`m confused???

Jason.


Brucey(Posted 2009) [#6]
Make() is a Method, not a Function.

You either do :
	Method Make:TEntity()
		' do some initialisation stuff here
		Return Self
	End Method


Local Player:TPlayer = New TPlayer.Make()


or

	Function Make:TEntity()
		Local Entity:TEntity=New TEntity
		' do some initialisation stuff here
		Return Entity
	End Method


Local Player:TPlayer = TPlayer.Make()



Brucey(Posted 2009) [#7]
You can only call a Method on a Type, if you have an instance of an Object. (you need a TPlayer object, in your example)

A Function is Global (or static, in other languages), which means you can call it without a live object, simply using the Type identifier followed by the function name. MyType.MyFunction().


QuickSilva(Posted 2009) [#8]
OK got it. Thanks for the explanation.

Jason.