Objects instance question

Monkey Forums/Monkey Programming/Objects instance question

Rushino(Posted 2012) [#1]
Hello,

I had a question regarding objects instancewith monkey, this might be stupid somehow but i just want to be sure about something.

Considering thoses points..

- If i have a tileset which contains tiles.
- I also have a grid which contains tiles but it is empty at the start. - So each tile in the tileset have an image loaded.


My question is, if i set a tile from the tileset on the x,y position on the grid. Does it contains the actual reference of the tileset tile ?

Thanks!


Tibit(Posted 2012) [#2]
Not entirely sure what you are after.

Yes.

Objects are handled by reference, so if you have one instance of an object (like a tile object) and set it to 100 tiles you still only have one object, and changing any of these 100 references changes the same object data.

Did that answer it?


Rushino(Posted 2012) [#3]
Yes that answer my question. I just didnt want the grid to load several time the same image.

Thanks Tibit!


Gerry Quinn(Posted 2012) [#4]
You need to distinguish between objects and primitives (primitives are things like ints and floats, and strings work in a way that makes them seem the same).

So an array of ints acts just like an array of ints in C. Each array element is simply a number.

An array of objects is really an array of pointers to objects. The objects could be the same, or they could be all different. If they represent a map of a dungeon, say, they should all be independent objects.

	' Field map:Tile[][]
	map = New Tile[ 60 ][]
	For Local x:Int = 0 Until 60	
		map[ x ] = New Tile[ 40 ]
		For Local y:Int = 0 Until 40
			map[ x ][ y ] = New Tile()
		Next
	Next


Each of these tiles is now an independent object.

But if you said:
	map[ 14 ][ 5 ] = map[ 13 ][ 5 ]


Then these two would thereafter refer to the same object. For a dungeon map you would never want to do that.

If the map contained ints instead of Tile objects, it would be okay to write the above, because the two cells would still only contain the same int value.


Rushino(Posted 2012) [#5]
Alright. :)

Thanks!