Jump to content


Photo

instanceof


  • Please log in to reply
2 replies to this topic

#1 Karahs

Karahs

    GMC Member

  • GMC Member
  • 11 posts

Posted 31 January 2012 - 06:14 PM

Hi everybody, I made this little script which emulates the well known operator "instanceof" used in java.
It checks whether the supplied instance id is an instance of the specified object or is a child of it.
It should allow easier type-checking... I couldn't find something like this in forum, so I just wrote it.

Feel free to use it, no credit needed :happy:

GMVersion: any(?)
Pro-only? No

/*
    boolean instanceof(instance_id inst, object_index ind)
    
    returns whether inst is an instance of ind
    
    ######################################################
    # PLEASE NOTE: inst must be the *id of an instance,* #
    # ind must be the *index of an object*               #
    ######################################################
    
    by Karahs
*/

var inst, ind, indx;
inst = argument0;
ind = argument1;

indx = inst.object_index;

if(ind == indx) return true;

while(indx > 0){
    indx = object_get_parent(indx);
    if(ind == indx) return true;
}

return false;

  • 0

#2 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • GMC Elder
  • 9331 posts
  • Version:Unknown

Posted 31 January 2012 - 06:57 PM

Um...GM has object_is_ancestor(), which does the same thing as yours, only you'd supply inst.object_index directly to the argument...

-IMP
  • 0

#3 Karahs

Karahs

    GMC Member

  • GMC Member
  • 11 posts

Posted 01 February 2012 - 01:46 PM

Damn, you are right :mellow:
I knew about that function, I just thought I needed to know if an object is the child of another, not if it is the ancestor... :skull:

Moderators, please remove this horror of mine, and sorry for posting unnecessary script...
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users