Reference to "Parent"
BlitzMax Forums/BlitzMax Beginners Area/Reference to "Parent"
| ||
Hi I'm new to BlitzMax and have a quick question for someone who can help! I want to have an object (let's say objectA) create/manage a list of other objects (let's call this List of objectB's). I want objectB's to be able to reference it's "Parent" ObjectA when they need to. I tried passing SELF but ObjectB's dont know what type objectA is unless I Include a referenc to ObjectA's file ala: Include "objectA.bmx" The problem I'm having is that objectA.bmx is already included in another file thus locked. A simple problem I'm having is having ObjectA hold a list of ObjectB's and when ObjectB's die off I need them to remove themselves from ObjectA's list of them. Does this make sense? Ha lots of A's n B's. Thanks for the help!!! |
| ||
Neither of them should include each other, instead your main program file should include both of them. You can then use Lock Build file on the main file that includes everything else in order to avoid compiling the other files by themselves which of course they can't due to being inter-dependent on other files in the include tree. They can however import each other but I won't go there as you should have a solid grip on the include command first. |
| ||
OK so that's cool. That's much cleaner. Thanks. Now how do I pass a reference of ObjA to ObjB? |
| ||
Maybe something like...Type ObjectA Field ListOfBs:TList Method release:Int(toRelease:ObjectA) ' receives a reference to the object it should let go ... Searches for and releases the object ... Note you might want to use a HashMap (or even Array) ... container if this happens a lot as searching through long lists will be slow! End Method Method add:Int(toAdd:ObjectB) ListOfBs.AddLast(toAdd) toAdd.setObjectA(Self) ' Self is the key for passing this ObjectA back to B End Method End TypeHope this helps the key to passing back a reference of the current object is Self. |
| ||
Why not extend the TList Object?Type ObjList Extends TList '.... End Type It already has an add and remove method that you can overload and make whatever changes you like. Type ObjA Field MyParent:ObjB 'yada yada yada End Type Type ObjB Extends TList 'Overloading the original AddLast method Method AddLast:TLink(obj:Object) Local oa:ObjA = ObjA(obj) if oa <> Null then oa.MyParent = Self Super.AddLast(oa) 'This calls the original AddLast Method end if End Method End Type |
| ||
Thanks for help! Gotta say I'm really diggin BlitzMax. |