2D Array of a class
Monkey Forums/Monkey Programming/2D Array of a class
| ||
I think I have this sorted out correctly:Strict Import mojo Class myClass Field value1:Int Field value2:Int End Class Class myGame Extends App Field board:myClass[10][] Method OnCreate:Int() SetUpdateRate(60) For Local i:Int = 1 Until board.Length board[i] = New myClass[8] For Local j:Int = 1 To 7 board[i][j] = New myClass Next Next board[1][1].value1 = 20 board[1][1].value2 = 20 Print board[1][1].value1 + " " + board[1][1].value2 Return 0 End Method Method OnUpdate:Int() Return 0 End Method Method OnRender:Int() Return 0 End Method End Class Function Main:Int() Local game:myGame = New myGame Return 0 End Function Is this the best way to do this? I'm going to have a board of probably 80-100 locations, and I want to store various attributes for each location on the board. This seems to work, just wondering if this is the best way to do it before I get too much further down the track. I've read a bit of stuff on 2D, Mutli-D arrays, but I don't recall coming across anything mentioning using anything other than the standard data types, int, float, string etc. Cheers. |
| ||
Yes, that's a solid approach. You might deviate from it if you needed super calculation efficiency, e.g. if you were trying to make a world-class AI for a game like chess. Otherwise, what you're doing is likely the simplest, and reasonably efficient. |
| ||
Thanks very much. Just wanted to make sure I was using the right approach. If it was something like tracking aliens that have a variable number (of total aliens) to them then I'm guessing I'd do the same thing with lists instead of arrays? |
| ||
In general, yes. Lists are the easiest way to handle... well, lists of stuff. You can easily add or remove them, or sort them according to move order or drawing order etc. The only issue here is that using lists freely often incurs a garbage collection overhead, which is more likely to bite you than any issues related to optimising access to elements in arrays. But all in all, I'd say go for the simple approach, and worry about optimising only if you have problems. |
| ||
Thanks very much Gerry. I haven't really done too much lists, I know how they work but I wasn't aware of the garbage collection issue, probably because I haven't read about them too much. One of my favorite apps tends to slow down after a couple of hours of game play, but it speeds up after quitting & reloading. I'd always assumed that this was in some way related to garbage collection not being handled correctly. I'm not going to use lists in this instance, so I haven't looked into it - does Monkey have functions that address garbage collection in case I use this in the future? |
| ||
It's more a case of learning what generates garbage and what doesn't. You certainly CAN use lists while generating a minimum of garbage. Lists often tend to encourage comfortable coding methods that can result in generating more. Conversely, the techniques you tend to use with arrays tend to involve accessing and changing stuff in place without allocating a whole lot of new things. But it's all down to what you are doing. Everytime you 'forget' an object, it becomes garbage. That won't happen too much with aliens, or with your board cells. But imagine you kept a list of bullets. Everytime a bullet is fired, a New Bullet() goes on the list. Every time a bullet's time is expired, it gets removed from the list and forgotten about. Each of those forgotten bullets is a piece of garbage that has to be collected, and they could be building up at a rate of dozens every second, if your game is exciting enough!. You might find that a game with such a list suffered GC problems (mostly they occur on Android, I think). If it ever becomes a problem, though, Monkey has all the tools you need to minimise it. With the bullets, you would want to re-use expired bullets instead of forgetting about them. You could roll your own re-usable bullets, or use the Monkey Pool class. I guess the issue boils down to this: if you were using arrays to store your bullets, you would probably automatically start re-using the things, because using arrays you would have to manage them anyway in such a way that re-using them would be just as easy as discarding them! |
| ||
@Arabia - Your code is addressing arrays starting from index 1. Arrays indices start at 0. |
| ||
@Arabia - Your code is addressing arrays starting from index 1. Arrays indices start at 0. Yeah I realize this - I just picked [1][1] as a place to put some garbage values, nothing to do with not knowing [0][0] is actually the first location in the array :) @Gerry - Cheers, that make sense. |
| ||
You're also only initialising the array references from 1. Is that just because you prefer to leave nulls at the zero position? |
| ||
You're also only initialising the array references from 1. Is that just because you prefer to leave nulls at the zero position? Oh I see what you mean - that was a typo in my example, yes it should have been For Local i:Int = 0 Until board.Length Thanks for picking that up, I would have transferred this to what I'm about to write and of course it would have created a Null object run time error :) |
| ||
One final question. Where I'm initializing the array here:Field board:myClass[10][] For Local i:Int = 0 Until board.Length board[i] = New myClass[8] For Local j:Int = 0 To 7 board[i][j] = New myClass Next Next I can use board.Length to find the size of the array's first dimension, so I can initialize it by using a 0 Until board.Length loop. How can I find the size/length of the 2nd dimension of the array (8) without hard coding it as For Local j:Int = 0 To 7 |
| ||
You can use board[i].Length. |
| ||
I thought that was the case, but when I typed in board[i]. - Length didn't come up in the intellitext in Jungle IDE - so I thought wasn't an addressable property. Thanks again for the help mate. |