A way to identify sub types ...

BlitzMax Forums/BlitzMax Programming/A way to identify sub types ...

Jeremy Alessi(Posted 2005) [#1]
Without adding an extra identifier field to my super type ... is there a way for me to iterate through all of my lowest level type and determine what more specific type each of them are?

Exampe:

I have

Type entity2D

Type player Extends entity2D

Type bullet Extends entity2D

Is there a built in way to iterate through all entity2D type objects and find out which ones are of type player or bullet without adding an identifier field in the entity2D super type?


Mark Tiffany(Posted 2005) [#2]
Assuming you are maintaining a list of all entity2d objects, I believe that the following will work:

for tmp_b:bullet = EachIn entity2dlist
  'iterates just the bullets in the list
next

for tmp_p:player = EachIn entity2dlist
  'iterates just the players in the same list
next



FlameDuck(Posted 2005) [#3]
AFAIK you can explicit downcast them. If they return a null pointer, they're not of that type.

eg if player(myEntity2D) 'myEntity2D is a player.

However this is dangerous as any objects that extend player will work, even though they strictly speaking aren't players.

It would be better to solve this problem using polymorphism.


Jeremy Alessi(Posted 2005) [#4]
Is there a way to insert into the entity2D (in my case) list? I don't see an insert command. I want to control the draw order of my entities depending on what sub type they are ... but I'd rather do this in the New function. So when I call New() I'd like to be able to ListAddFirst or ListAddLast depending one what I'm adding to the list and when if you know what I mean.


Charles(Posted 2005) [#5]
forgive my impudence, but I would suggest that you create a Entity2DManager class that contains your list(s) (specific or generalized) and a static factory method for creating said objects. When you ask the Entity2DManager for a new instance(Type) it should handle creating the object type and upkeeping the list(s) for you.

IMHO objects shouldn't have the burden of keeping track of that much information about itself.. my 2 cents.


Jay Kyburz(Posted 2005) [#6]
I've been going the other way, I'm moving toward giving my objects as much power and autonomy as I can. I have manager classes, but i am simply using them to tick each object when appropriate. (i have separate timer clocks for my render, ui and game entities)


Charles(Posted 2005) [#7]
@Hakea : Forgive my entry into this forum (long time lurker, first time poster [*waves*]). I'm more familiar with OOP from a Java perspective, so please pardon some of my assumptions if they don't apply to BlitzMax.


Jeremy Alessi(Posted 2005) [#8]
Yeah, I might have to do that ... but it's kind of a shame ... it'd be so easy to just be able to identify what sub type the incoming object is supposed to be and place it in the list accordingly. Also, without an insert command how are you supposed to custom sort a list? It's got sort commands but they don't seem to give you any control over how they are sorted. I guess I'd have to convert to an array and sort that custom and then write it all back to the list.


Jay Kyburz(Posted 2005) [#9]
I didn't mean to suggest what you were suggesting was wrong, I'm very new to this myself. I posed because I had been thinking about this very subject over the weekend. I opted for powerful objects and weak managers because.. well I'm not sure why.. to embrace the object ornateness I guess.

In b3d all you have is manager functions and types that don't know how to do anything.

Current my objects add remove them self from a global list on .New and .Close

I'm not sure it I would g as far as allowing individual objects sort the list if they needed to reposition them self.


FlameDuck(Posted 2005) [#10]
it'd be so easy to just be able to identify what sub type the incoming object is supposed to be and place it in the list accordingly.
I recommend using the Factory like Charles suggested. Let the element that has intricate knowledge of the instance in question, decide where they go in the list, and add them at creation time.

Also, without an insert command how are you supposed to custom sort a list?
By implementing a compare() method.

I'm not sure it I would g as far as allowing individual objects sort the list if they needed to reposition them self.
No, I wouldn't recommend that either.


Charles(Posted 2005) [#11]
@Hakea : I'm a newbie to BlitzMax as well; I'm being over-cautious with my comments - don't mind me.


For my own data management I created a Node class :

.. partial code ..

Type Node

	Field nParent:Node
	Field nChild:Node
	Field nPrev:Node
	Field nNext:Node
	
	Field sName:String
	
	Global numNodes = 0

	'Constructor
	Method New()
		
		nParent = Null
		nChild = Null
		nPrev = Self
		nNext = Self
		
		numNodes:+1
		
	End Method

... code continues



I was going to use this as the basis for a primitive scene graph and use the position in the tree to determine sort order / render. I can add / detach / delete Nodes from each other...


Who was John Galt?(Posted 2005) [#12]
@Charles/Hakea-
If you think you've got something useful to say, just post it. It generates conversation and that's how we all learnt something. Worst that can happen is that flameduck rips u to shreds ;)