What am I doing wrong with using Sets?

Monkey Forums/Monkey Programming/What am I doing wrong with using Sets?

AaronK(Posted 2011) [#1]
Hi guys, new to Monkey and trying to find out why I'm getting an assert when running some code.

Here's an example program that causes the exact problem I get in my actual problem

Import monkey.set

Class Blob
End

Global a:Set<Blob> = New Set<Blob>

Function Main()
	a.Insert(New Blob)
End



Running that gets me an 'TypeError: Cannot call method 'bbm_Insert2' of null' error.


muddy_shoes(Posted 2011) [#2]
I haven't used the monkey sets myself, but looking at the code it looks like the base Set class is intended to be Abstract and you're supposed to extend it to make a Set that can store your custom classes. It also appears to require a specialised Map class to use, which means you'll have to decide how your "Blob" class can be uniquely ordered as the Map relies on that ability.


muddy_shoes(Posted 2011) [#3]
What follows is an example that I'd be very glad to be told is completely wrong, because it's not pretty.




AaronK(Posted 2011) [#4]
Thanks muddy, although if true that's very peculiar because sets in any other language I've used tend not to work that way.

When I look at the set class it all looks OK on the surface and ready to go. It just uses a map internally keyed in my type with null as it's dummy value in the actual map. And I can't see in the map code any obvious places where null would cause a problem.

Very strange.


muddy_shoes(Posted 2011) [#5]
Set does use a map internally, but it never initialises that map. It expects the map to be passed in the constructor. Your code was failing because you were calling the default constructor (arguably this shouldn't be allowed if you've created another constructor) so the internal map was null.

Set uses Map to ensure that entries are unique by adding inserted items as keys. As Map is implemented with a tree that expects a unique ordering value and Monkey doesn't have a base Object definition that ensures such a value exists, if you want a map that is keyed with a custom class then you have to extend the base Map to provide the ordered comparison.

My opinion is that this is all pretty broken on a structural level and will have to be changed at some point.


AaronK(Posted 2011) [#6]
Yeah sounds like it's quite broken in that regard. I guess I expected it to use essentially an "operator=" to handle the key comparison.

Cheers


GfK(Posted 2011) [#7]
O/T: Aaron Koolen??? Where do I know that name from???


Roger Lockerbie(Posted 2011) [#8]
@Gfk Aaron wrote some games in the vision software days e.g the PC port and enhancement of Seek and Destroy sold by Epic

He also wrote the apath example in blitz max and for bonus trivia he was the shirtless zombie in the digitization of the acid and vision staff for the Amibkitz zombie apocalypse examples.

BTW Hey Aaron!


AaronK(Posted 2011) [#9]
Hey Roger!

As Roger said, all those things, and to learn Monkey I'm writing another AStar algorithm, although this time it's for an actual game.

More recently I released my first iOS game, TrapIt - an old-school, score-attack casual-arcade game. <pimp, pimp> :) http://littlegeek.co.nz/trapit.html

My aim was to retire in my dream mansion from my earnings, but so far I can only afford the letterbox. Hence, I'm starting on my next game.


GfK(Posted 2011) [#10]
Ah a minor celebrity then! Seek & Destroy was cool.