Extends problem

BlitzMax Forums/BlitzMax Beginners Area/Extends problem

Grovesy(Posted 2005) [#1]
Hi all,

I am starting my first BMAX project and have already hit a problem i don't understand.

I have created a class called TObject which defines the prototype/interface of all other objects and looks like this.

TObject.bmx
Type TObject
	Field Name
	Field X#,Y#
	Field XSpeed#,YSpeed#
	Field Direction

	Function Create(name) EndFunction
	
	Function UpdateAll() EndFunction
	
	Method New() EndMethod
	
	Method Update() EndMethod

	Method Destroy() EndMethod

	Method Draw() EndMethod
End Type


I then start to create a new class that extends this object class. and looks like this.

TSwitch.bmx
Include "TObject.bmx"

Type TSwitch Extends TObject
	Global List:TList
	Global Image:Timage
		
	Function Create(name)
		name:TSwitch = New TSwitch	 
		If List = Null List = CreateList()
		List.AddLast name 'Add Ship Last in List
	EndFunction
	
	Function UpdateAll()
	
	EndFunction
	
	Method New()
		X = 300
		Y = 400
	EndMethod
	
	Method Update()
	
	EndMethod

	Method Destroy()
	
	EndMethod

	Method Draw()
	
	EndMethod
End Type


But when i compile the TSwitch.bmx i get the error saying "Identifier type does not match declared type"

Can someone please explain what i have done wrong.

Cheers


Stuart Morgan(Posted 2005) [#2]
Have a read up on the Abstract command in the docs. That should solve the problem.

Function Create() Abstract	
Method Update() Abstract
Method Draw() Abstract



Grovesy(Posted 2005) [#3]
Cheers Stuart.

Unfortunatly now when i compile the TObject.bmx i get the error "Duplicate indentifier 'New'" why is this?


Stuart Morgan(Posted 2005) [#4]
Unfortunatly now when i compile the TObject.bmx i get the error "Duplicate indentifier 'New'" why is this?

You have changed "Method New() End Method" to "Method New() Abstract" right?


Dreamora(Posted 2005) [#5]
Problem is different: field name is the problem.

In basetype you have declared it as int and in the extended type you do name:TSwitch = New TSwitch
which is incorrect in 2 different ways:

1) with the declaration of base type field as int
2) with the local declaration of input parameter name which is int as well.


Grovesy(Posted 2005) [#6]
OK i'm still stuck. Could someone please some an example of an interface/prototype object that is extended by another object in a different file.

Cheers


The r0nin(Posted 2005) [#7]
OK. Two problems. First, your TObject has a field "Name." Then, your create function has an input parameter called "name." Biltz is NOT case sensitive, which means that you are telling Blitz that the function Create will have a variable passed into it whenever it is called, and the data from that variable will be stored in a local variable called "name." (That's what "Create(name)" means...). Well, it can't make a local variable "name" within the type TObject because Tobject contains a Field called "Name", which is EXACTLY the same to the Blitz compiler.

Second problem. All variables are created as integers unless you explicitly declare them otherwise. So both your field "Name" and your variable "name" have been declared to be integer variables (they can only hold whole numbers). Then you try to put an object in one! (name:TSwitch = New TSwitch). If you want to be able to store an object reference in "name" you have to declare it as that type of variable, either in the field (Field name:TSwitch=Null) or in the call (Function Create(name:TSwitch). That's what the colon after the variable does, tell the compiler what kind of variable to make it. Now, let's say you wanted the field "Name" to hold the actual character name that you have given it; you need to define that Field as a string (of characters) like this-- "Field Name:String = Null", or in shorthand-- "Field Name$ = Null". Then you could pass the name of the new object in with the Create function to give it to your new object. You need to use other variables, though:

Hope that helps...


The r0nin(Posted 2005) [#8]
One last thing. When you see the line "Function Create:TSwitch(name_in$)" it means this to the compiler:

"Function" = I'm going to create a new function now, inside this object.

"Create:TSwitch" = This function will be named "Create" and will return an object of the type "TSwitch"

"(name_in$)" = This function will have to be passed a parameter with will be in the form of characters, and it will be stored in the local string variable called "name_in"

Then when I calle the Function in my program, I use "Global myobject:TSwitch = TSwitch.Create("First")". The compiler reads this to mean:

"Global myobject:TSwitch=" = I am creating a global variable (of type TSwitch) to store the returned object from the create function. This is optional, but useful if you want to do something else to that object, before you lose it and can only get to it through the List. It could be Local instead of Global, depending on what you are going to do to it.

"TSwitch.Create" = I'm telling the compiler to go into the Type TSwitch and perform the function called Create. If I want to use a method, I would call the method through the variable (i.e. myobject.update()), but Functions are called by Type.

"("First")"= I'm telling the compiler to send the characters F I R S T into the function, where we know they will be stored in a temporary string variable named "name_in". I could have also sent a string variable in, i.e.:

Local data_in$ = "First"
Global myobject:TSwitch = TSwitch.Create(data_in).

Hope this explains what is happening...