Links between objects in different tlists.

BlitzMax Forums/BlitzMax Programming/Links between objects in different tlists.

KronosUK(Posted 2006) [#1]
The situation is this I have (or will have) two tlists, each of which are constantly having objects added and removed.. The objects in tlistB are spawned by objects in tlistA who are otherwise oblivious of them(Fire and forget).

Every cycle objects in tlistB need to access certain fields on their parent objects in tlistA such as position and speed , which update every cycle, but are otherwise independent of tlistA.

The only way I can think to do this is using pointers. Object in TlistB has a pointer to its parent or the parent field in tlistA which it can use to access the relevant info.

Can anyone suggest a simple way to do this without using pointers?


Dreamora(Posted 2006) [#2]
There is no reason not to use references in this case.

If the reference is only from listB to listA and not vice versa, you won't risk any cleanup problems as well.

PS: there is no other way than actually using references in some way at some point. Question is only if you want to do it your way which is efficient or doing it in a really slow way by saving some kind of ID / Name and search for that in listA


SpaceAce(Posted 2006) [#3]
What is your aversion to the use of pointers? Type instances are pointers, anyway.

The way you're doing it is the way I do it in my own code. In my board game engine, every object has a Parent and a Child of the "Object" type. I simply set the parent or child of each object and then cast it back when I need it. For instance:

ObjectA:FirstType = New FirstType
ObjectB:SecondType = New SecondType

ObjectA.SetChild(ObjectB)
ObjectB.SetParent(ObjectA)

SecondType(ObjectA.GetChild()).SomeMethodFromSecondType()


Of course, if the Parent or Child will always be a specific Type, you can skip the casting and just have GetChild()/GetParent() return that type instead of an Object.

Exactly why is it you don't like using this method?

SpaceAce


Dreamora(Posted 2006) [#4]
reference != pointer

A reference is something that is handled by the GarbageCollector and that is typesafe, a pointer only points to some memory.


SpaceAce(Posted 2006) [#5]

reference != pointer

A reference is something that is handled by the GarbageCollector and that is typesafe, a pointer only points to some memory.


Was that for me? I'm not sure what part of what I posted you're correcting.

SpaceAce


Dreamora(Posted 2006) [#6]
No that was just a general posting to make sure that Kronos is not mixing that in case this would be his problem and because you used "pointer" in your explanation as well which is incorrect in this case (type references in BM)


KronosUK(Posted 2006) [#7]
Ok I think I understand.. to clarify

local Mob:object = new Object

local Mov:object

mov = mob

mov and mob both "reference" the same object data?

If this is so then I don't need to use pointers.

I knew there would be a simple solution.