Creating an object within an object

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

Leon Brown(Posted 2007) [#1]
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.


SebHoll(Posted 2007) [#2]
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.


Leon Brown(Posted 2007) [#3]
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.


Brucey(Posted 2007) [#4]
This seems to work..



Leon Brown(Posted 2007) [#5]
Thanks for that Brucey :-) Works a treat! Took me a whole day to figure that out.


Brucey(Posted 2007) [#6]
I see you changed the original post. So it wasn't too far off then ;-)


Leon Brown(Posted 2007) [#7]
Not too far off, but it's amazing how such small things can make a big difference :-D


H&K(Posted 2007) [#8]
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)


Blueapples(Posted 2007) [#9]
Wouldn't go amiss?


Leon Brown(Posted 2007) [#10]
It was only a slight change and I did say in the following post.