How to compare indefinite amount of parents

U

Uulamock

Guest
So here is the setting: Let's say I have a giant object parent tree, to keep it simple I'll have it be linear. So for example:

obj_parent1
obj_parent2 //child of obj_parent1
obj_parent3 //child of obj_parent2
/* skip a few */
obj_parent50 //child of obj_parent49
I have these 50 objects, and they are all part of an inheritance tree (a line in this case). Now, what I want to do is check if an object is a child of any previous parent. So ex:

var child = obj_parent50;
var parent = obj_parent1;

if (child == parent) { /* do stuff */ }

In this example, child does not equal parent, because child is obj_parent50 while parent is obj_parent1. How can I always get a positive comparison if an object is a great, great, great...great grandchild?
 
U

Uulamock

Guest
Okay, I haven't gotten any feedback for a while, so I did some hard work and forced my own search through parents.

Code:
   ///scr_compare_parents(child_instance, parent_object)
   
   var child = argument0;
   var parent = argument1;
   
   var test = noone;
   
   var searching = true;
   var found = false;
   
   if(child.object_index == parent) {
        found = true;
        searching = false;
   } else if (object_get_parent(child.object_index) >= 0) {
        test = object_get_parent(child.object_index);
   } else {
        searching = false;
   }
   
   while (searching) {
        if (test == parent) {
            found = true;
            searching = false;
        }
        if(object_get_parent(test) >= 0) {
            test = object_get_parent(test);
        } else {
            searching = false;
        }
   }
   
   return found;
This script will search through every possible parent and tell you if a match is found, tested and functioning.

HOWEVER, I would truly like to think GameMaker has an easier method already installed to do a task such as this, if there is anything I can use build into GameMaker that does the same thing as the script above (basically) then that would be much appreciated.
 
D

dj_midknight

Guest
If this were Java Id have an answer for you, but in GML this may be more complex than GML is designed to handle. Its really more of a 1 off situation than a smaller development company would be setup to accommodate. Id say if your script is working go with it, or find a simpler structure. Others may disagree with my assessment as I am fairly new to GML.
 

sp202

Member
I believe object_is_ancestor is simply the reverse of object_get_parent. I don't think there'd be a built-in function that could check through a whole line of ancestry. I'd be interested to know why you'd need such a system.
 

Perseus

Not Medusa
Forum Staff
Moderator
That is certainly possible with object_is_ancestror(), for it is more than just the reverse of the object_get_parent() function. The latter provides the object index of the object that another specified object inherits from, but it provides no information about what its parent inherits from. On the other hand, object_is_ancestor() takes into account all its ancestors. For example, if object0 inherits from object1 and object1 inherits from object2, then object_is_ancestor(object0, object2) would have returned true, because object2 is one of object0's ancestors. And that's basically what the OP wants. :)
 
Top