How to structure my Types and Tlists

BlitzMax Forums/BlitzMax Beginners Area/How to structure my Types and Tlists

Robb(Posted 2009) [#1]
I am trying to implement a sort of tag-search system to my game's entities however I am totally confused as to how to best set up my types and data structures.

1) At the moment I have a main 'world' object that contains a list of 'entity' objects together with a list of 'bundle' objects.

2) Each bundle will contain a list of search tags that can be assigned to the entity objects.

3) An entity can have multiple tags, however it can only have one tag from each bundle (hence the bundle object). The structure (and pseudocode of the objects) I am thinking of is something like this:



Is this a sensible way of doing this? there a better way of structuring this?

I want to be able to return a list of all entities or entities by tag.
I also might need to return a list of all tags – could this be built on request by looping each of the world's bundles and adding the tags to a temp list (rather than a global tag list)?

Basically I'm a bit confused as to how to put this all together, and whether I should be using global lists or per-object lists or even if I should be using Tlists!

Any help would be greatly appreciated!


Scienthsine(Posted 2009) [#2]
That seems like a well laid plan. There is nothing wrong with having a global list, a list of lists containing things that are in other lists, etc... Just make sure you handle removal/addition, etc all correctly. Keep track of everything, and your good to go.

It depends on your speed requirements if you need to build the list of tags on request or not. If you are needing everything to be lightning fast, keep track of another global tag list, else do it on request.

To get entities by tag, ofcourse, you'll need to loop trough the bundles of tag lists, match, then add the entity to your temp list, then return the list.

In a physics engine, and a gui engine I built I used global lists so I could easily do batch processing on all of a type. I also had other lists though, for example I had groups (which I also had a global list of) which contained a list of certain objects (also in their respective global lists).

Just structure it how you have it in the diagram, and you'll do fine. :)


Czar Flavius(Posted 2009) [#3]
I'm not too sure what you are asking for, but I feel that TMaps may be of use to you here. A TMap is kind of a cross between an Array and a TList. They increase in size as needed like a TList, but you can jump straight to the item you need right away, like an Array.

You basically associate each object in the TMap with a String, which is known as the key (for your purposes, read: tag!) Then to get at the object, you just give the map that tag, and out it pops! Like so,

Type TMushroom
	Method speak()
		Print "I am a mushroom."
	End Method
End Type

Local map:TMap = New TMap
Local m:TMushroom = New TMushroom
map.Insert("Tag_Mushroom", m)
Local temp:TMushroom = TMushroom(map.ValueForKey("Tag_Mushroom"))
temp.speak()


I notice in your description you have need for multiple objects to share the same tag. Hm, you can't add multiple objects to a map with the same key (it will overwrite). However what you CAN do is add TLists to the map! :D So to give an object a tag, add it to the end of the list that exists in the map. To get all objects with that tag, get the list from the map and go through each item! I'll leave the specifics of how to code that to you :P


Oh yeah, I'm in the helping zone this week!


Robb(Posted 2009) [#4]
Thanks for the replies! They both clarify a lot of things for me.

What is the best practice regarding Tlinks and objects being in multiple lists?

Would it be wise to have more than one tlink field in my types, e.g. the Tag object has a link to the bundle object's list and another link field for it's position in the global list?

I think this would fall down with an entity object though as exists in both the World's entity list, and also in each Tag's list. Basically, an entity could feasibly be in 5/6 lists and I'm a little concerned as to how best to break all of those links when the entity is removed.


Czar Flavius(Posted 2009) [#5]
Why not give each object a TList of TLinks to all the TLists it's in? Say that three times fast!

it's position in the global list
Tut tut, no ' in possesive its.


Robb(Posted 2009) [#6]
Many thanks for your response, that seems like a sensible option!

Tut tut, no ' in possesive its.

No offense but I'd appreciate not being called out on a grammatical mistake. I understand the use of an apostrophe, it was merely a typing error.
While you probably did not intended it as such, your correction comes across as rather patronising and within the context of the thread, is entirely unnecessary.


Scienthsine(Posted 2009) [#7]
A TList of TLinks would allow for easy removal from all lists, and once again is completely ok aslong as you keep track of what your doing.

Since your obviously capable of making little diagrams, I would suggest that you make one detailing your final structure when finished. That looks like great documentation, even if just a reminder for yourself. :)


_Skully(Posted 2009) [#8]

Tut tut, no ' in possesive its.



Nothing like breaching the rules while correcting someone else... eh?


I'm a little concerned as to how best to break all of those links when the entity is removed.



Of course, the best practice is to remove them from each of the lists that its been added to prior to removal... don't want null object references!


Czar Flavius(Posted 2009) [#9]
I jested :'(