Creating an object within an object

BlitzMax Forums/BlitzMax Programming/Creating an object within an object

I was wondering if anyone knew how to instantiate an object from a method within an object. The following is an example of what I'm trying to do:

Type map
  Field width:Int=50;
  Field height:Int=50;
  Field layers:TList=CreateList();
  ' *** CONSTRUCTOR
  Function Create:map()
    *** Works fine here
  End Function
	
	
  Method addLayer(id:Int,layerName:String)
    **** Following line tries to create new layer object
    Local layer:mapLayer = New mapLayer.Create(1,layerName,Self.width,Self.height);
    For Local rep = 1 To 20
      layer.map[Rnd(Self.width),Rnd(Self.height)] = 1;
    Next
    
    ListAddLast Self.layers,layer;

  End Method
	
  Method drawMap()
    **** draw map code
  End Method

End Type


Type mapLayer
  Field id:Int;
  Field name:String;
  Field map:Int[50,50];
	
  ' *** CONSTRUCTOR
  Function Create:mapLayer(layerID:Int,layerName:String,width:Int,height:Int)
    Local newLayer:mapLayer = New mapLayer
    'newLayer.map = New Int[width,height];
    newLayer.name = layerName
  End Function
End Type






As you can see from the code, the new object is added to the layers list, but when I try to access the created object, none of the properties such as the map array can be access.

I'm not entirely sure about what you are trying to do but one thing that strikes me odd is that you have a Field map:int[50,50] inside a method - as far as I'm aware, if you want to declare a field in a type, it has to be done outside any methods or functions where all the other ones (e.g. Field width:Int) are declared.

I've got a feeling I'm missing the point entirely, but thought I'd mention it.

I wrote an shortened version of the full code and didn't notice that it wasn't complete before I posted. Just edited the code so that it reflects the actual code.

What I want to do is create a map object that has a list of tile layers. However, because I've not been able to put an array into the layers list, I've had create an object for each layer. If anyone knows how a similar way to put arrays into lists, this would be good also.

This seems to work..
SuperStrict

Type mapLayer

	Field width:Int
	Field height:Int
	Field id:Int
	Field name:String
	Field map:Int[,]
	
	Function Create: mapLayer(id:Int, name:String, width:Int, height:Int)
		Local this: mapLayer = New mapLayer
	
		this.id = id
		this.name = name
		this.width = width
		this.height = height
	
		this.map = New Int[width, height]
	
		Return this
	End Function
	
	
End Type

Type map
	Field width:Int=50;
	Field height:Int=50;
	Field layers:TList=CreateList();

	' *** CONSTRUCTOR
	Function Create:map()
	'*** Works fine here
	End Function


	Method addLayer(id:Int,layerName:String)
	'**** Following line tries To create New layer Object
		Local layer:mapLayer = New mapLayer.Create(id,layerName,Self.width,Self.height);
		For Local rep:Int = 1 To 20
			layer.map[Rnd(Self.width),Rnd(Self.height)] = 1;
		Next
		
		ListAddLast Self.layers,layer;
	
	End Method

	Method drawMap()
		'Field map:Int[50,50];
		'**** draw map code
		For Local layer:mapLayer = EachIn layers
			Print "Layer : " + layer.name
			Local s:String
			For Local y:Int = 0 Until layer.height
				For Local x:Int = 0 Until layer.width
					s:+ layer.map[x, y]
				Next
				Print s
				s = ""
			Next
		Next
	End Method

End Type


Local myMap:map = New Map

myMap.addLayer(1, "Bottom")
myMap.addLayer(2, "Middle")
myMap.addLayer(3, "Top")

myMap.drawMap()


Thanks for that Brucey :-) Works a treat! Took me a whole day to figure that out.

I see you changed the original post. So it wasn't too far off then ;-)

Not too far off, but it's amazing how such small things can make a big difference :-D

I see you changed the original post
Lol, a little note to that effect, on the first post would go amiss. (Cos Sebs anwser (for example), just looks silly now)

Wouldn't go amiss?

It was only a slight change and I did say in the following post.