First hello and problem

BlitzMax Forums/BlitzMax Beginners Area/First hello and problem

AJirenius(Posted 2007) [#1]
Hi, I'm now slowly trying out BlitzMAX as I believe my next game will be created in this language.
Already (of course) I'm stumbling into problems and am thinking if you could just point out what am i doing wrong here cause I just don't get the thing with dynamic arrays.
I have 3 different types here and in the 3rd (TPond) I'm trying to write a normal Create function but it just won't let me create an array of objects on the fly. What am i doing wrong? The errormessage I receive is: "Unable to convert from 'TSquare Array' to 'TSquare Array'"

Type TObject Abstract 
	Const CTILE = 1
	Field Mesh
	Field Typ
	Field X
	Field Y
End type

Type TSquare Extends TObject
	Field Obj:Object
End Type

Type TPond
	Const MaxSquareWidth = 80
	Const MaxSquareHeight = 20
	Field Squares:TSquare[]
	
	Function Create:TPond(SqWidth,SqHeight)
		Local Temp:TPond=New TPond
		Temp.Squares=New TSquare[SqWidth,SqHeight]
	End function
End Type



AJirenius(Posted 2007) [#2]
God, I'm sorry for starting up that low and bad. I found the solution myself just seconds after so it's fixed. The syntax should be :

Temp.Squares = New TSquare[SqWidth,SqHeight]

It didn't like to have thise brackets afterwards.


Who was John Galt?(Posted 2007) [#3]
<beaten to it>


AJirenius(Posted 2007) [#4]
Ah.. thanks I changed my question slightly but I did miss out the 2D 'comma'.
Thanks a lot Nomen.
I think I will enjoy BlitzMAX even more than B3D

*edit*
Now this was a confusing first post, I agree. Nomen, you helped me out before you erased it though. I did 2 mistakes in the code and you showed me the second problem. Now it's all fixed and I can go on making more mistakes. Thanks


Who was John Galt?(Posted 2007) [#5]
No problem at all. Not even sure if the comma in the brackets is the right way to denote a 2D array.


AJirenius(Posted 2007) [#6]
It gives that errormessage in first post if it isn't there so at least it works properly.


H&K(Posted 2007) [#7]
Does that work?

I would have thought that as Temp isnt returned or stored anywhere, that it would be GC'ed.

If you are moveing from B3D, types arent automaticly put onto a list, so the New TPond stops existing when control leaves Create


AJirenius(Posted 2007) [#8]
Ah, well I hadn't come to that part yet as i was trying out the initiating of the array. Yes, of course the Temp-object will be returned. Still it's really tricky this type of handling objects.
Now I'm stuck on another thing (as predicted). Hope I'm not a pain in the ass but I guess you can just stop answering me if I am. Here it goes, this is what it looks like now:
Type TObject Abstract 
	Const CTILE = 1
	Field Mesh
	Field Typ
	Field X
	Field Y
End type


Type TTile Extends TObject
	
	Field Letter
	
	
	Function Create:TTile(letter=0)
		Local Temp:TTile
		If Letter=0 Then Letter = Rand(65,80)
		Temp = New TTile
		Temp.Letter = Letter
		Temp.Typ = CTILE
		Return Temp
	End Function

End Type
Type TSquare
	Const CTILE = 1,CWATER = 0

	Field Obj:Object
	Field SqTyp%
End Type



Type TPond
	Const MaxSquareWidth% = 80
	Const MaxSquareHeight% = 20
	Field Squares:TSquare[,]
	Field SqWidth%
	Field SqHeight%
	
	Function Create:TPond(SqW,SqH)
		'Creates an empty pond. Returns a Pondobject.
		Local Temp:TPond=New TPond,n,m
		If SqW > MaxSquareWidth Then SqW = MaxSquareWidth
		If SqH > MaxSquareHeight Then SqH = MaxSquareHeight
		Temp.SqWidth = SqW
		Temp.SqHeight = SqH
		Temp.Squares=New TSquare[SqWidth,SqHeight]
		
		'Sets water in all Squares
		For n = 0 To SqWidth-1
			For m = 0 To SqHeigth-1
				Temp.Squares[n,m].SqTyp = 0
			Next
		Next
		
		Return Temp 
	End function
	
	Method AddObject(Obj:Object,x,y)
		Self.Squares[x,y].SqTyp = Obj.Typ
		Self.Squares[x,y].Obj = Obj
	End Method
	
End Type


I'm trying to make this as easy as possible. I have this basic foundational Type called TObject that has basic fields telling what kind of object it is and a handleholder for a mesh (don't ask).
The TPond-type is basically an array containing Squares. Each Square can contain any extended object from the basic TObject Type. Now in the last Method in TPond I'm trying to use fields from the basic TObject as I know only Objects extended from the TObject will wind up there.
This doesn't seem to work
Must I write an Add-function for every extended Type from TObject even if I'm just using fields from the basic TObject??

Edit: To clarify my question. I don't know WHAT Type of object will be passed into the AddObject Method, but I do know that all those objects will be extended from the Basic TObject and therefor I know all of them will have a Field called Typ.
Still trying to reach the field with Obj.typ will render an error. What do I do? Write Add for each Type that will be passed as a parameter?


Paposo(Posted 2007) [#9]
Hello.

The method AddObject get parameter Obj:Object
The type Object not contain field Typ
The correct declaration is Obj:TObject

Note: You write code plus debugable if you use the directive:
SuperStrict. Is unvaluable this directive!!!!

Bye,
Paposo


AJirenius(Posted 2007) [#10]
I understand, but will that actually work? I mean I will pass objects that are not of the type TObject but several different extended types from the TObject.


AJirenius(Posted 2007) [#11]
God this really is hard to get your head arund, not even the simpliest code seem to work. Can you please guys take a look at this and don't mind the above:
Type TSquare
	Const CTILE = 1,CWATER = 0
	
	Field Obj:TObject
	Field SqTyp%
End Type



Type TPond
	Const MaxSquareWidth% = 80
	Const MaxSquareHeight% = 20
	Field Squares:TSquare[,]
	Field SqWidth%
	Field SqHeight%
	
	Function Create:TPond(SqW,SqH)
		'Creates an empty pond. Returns a Pondobject.
		Local Temp:TPond=New TPond,n,m
		If SqW > MaxSquareWidth Then SqW = MaxSquareWidth
		If SqH > MaxSquareHeight Then SqH = MaxSquareHeight
		Temp.SqWidth = SqW
		Temp.SqHeight = SqH
		Temp.Squares= New TSquare[SqW,SqH]
		
		' Sets SqTyp to 0 in all Squares
		For n = 0 To SqW-1
			For m = 0 To SqH-1
				Temp.Squares[n,m].SqTyp = 0
			Next
		Next
		
		Return Temp 
	End Function
	
End Type


The last rows in the Create Function that says
Temp.Squares[n,m].SqTyp = 0
just don't seem to work and complains about trying to access a field of a NULL-object, but I just created those objects with Temp.Squares= New TSquare[SqW,SqH], didn't I????
Help please!


AJirenius(Posted 2007) [#12]
Making it clearer: what is WRONG with the code below?
Type SmallFighter
	Field Name$
End Type


Type Cruiser
	' This Type got an array holding SmallFighter in it.
	Field Hangar:SmallFighter[]
	
	
	' This Function below takes one argument (number of Smallfighters in the array) and then returns a cruiser with that array.
	
	Function CreateCruiser:Cruiser(NrShip)
		Local Temp:Cruiser = New Cruiser
		Temp.Hangar = New SmallFighter[NrShip]
		Return Temp
	End Function
	
		
End Type

' Program starts here where I create a Cruiserobject with name Mothership (holds 4 SmallFighters)
Local Mothership:Cruiser = Cruiser.CreateCruiser(4)

' Now I'm trying to set a name on the first fighter in the hangar. Errormessage!
' Seems like it's just a null-object
Mothership.Hangar[0].Name="Black Hawk"



H&K(Posted 2007) [#13]
Type SmallFighter
	Field Name:String
End Type


Type Cruiser
	' This Type got an array holding SmallFighter in it.
	Field Hangar:SmallFighter[]
	
	
	' This Function below takes one argument (number of Smallfighters in the array) and then returns a cruiser with that array.
	
	Function CreateCruiser:Cruiser(NrShip)
		Local Temp:Cruiser = New Cruiser
		Temp.Hangar = New SmallFighter[NrShip] 
		For f = 0 To NrShip-1
		   Temp.Hangar[f] = New SmallFighter
		Next
		Return Temp
	End Function
	
		
End Type

' Program starts here where I create a Cruiserobject with name Mothership (holds 4 SmallFighters)
Local Mothership:Cruiser = Cruiser.CreateCruiser(4)

' Now I'm trying to set a name on the first fighter in the hangar. Errormessage!
' Seems like it's just a null-object
Mothership.Hangar[0].Name="Black Hawk"



Brucey(Posted 2007) [#14]
but I just created those objects with Temp.Squares= New TSquare[SqW,SqH], didn't I

Just so you know, using New MyArray[x] or New MyArray[x, y] etc, only allocates memory for the array itself, but it does not create any of its content. You have to do that yourself.

As H&K's example demonstrates, you first initialise the size of the array, and *then* populate that array with new objects.

The only kind of arrays that don't require you to do this are with "primitive" types, like Int, Float, Double, etc. Once you create an array of those, you can start using it immediately.

HTH

:o)