• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [Solved] How to find multiple object instances?

M

M3gal0maniac

Guest
Hi all, I'm fairly new to this.

I am wondering if there is a way or a function to search for multiple object's instances and check if they exist.

For example (wrong code but shown as an example of what I want) :
Instance_exists(obj_1,obj_2)

I understand I can use multiple if statements to check each instance_exist but is there a more elegant way in doing this? Thanks!!
 

NightFrost

Member
Not on command level, no. But you could throw the object references into an array or ds list, and iterate through it in a loop. If the checklist of objects does not change you need to set the array up only once.
 

Yal

🐧 *penguin noises*
GMC Elder
You could use parenting if all the objects you want to search for have something in common, e.g. all of them are terrain, collectible items, or enemies, which also has other benefits like being able to not repeat code in multiple places. Or you could use an array and a loop like this:

Code:
lookfor[0] = obj_1
lookfor[1] = obj_2
lookfor_num = 2//The array has two things in it right now, #0 and #1
found = false
for(c = 0;c < lookfor_num;c++){
  if(instance_exists(lookfor[c])){
    found = true
    break//leave the loop early to speed up, since we just want to find either object and we just did
  }
}
if(found){
  //Do something here
}
 

jo-thijs

Member
Hi ad welcome to the GMC!

So, you want to check if any instance of type obj_1 or type obj_2 exist?
Then just use:
Code:
(instance_exists(obj_1) || instance_exists(obj_2))
And if you want to know if instances exist for both obj_1 and obj_2, you just replace || by &&.
 
M

M3gal0maniac

Guest
Wow. First time using the forum and am impressed by such quick responds! Thanks so much guys I'm going to try it out. :)
 
M

M3gal0maniac

Guest
You could use parenting if all the objects you want to search for have something in common, e.g. all of them are terrain, collectible items, or enemies, which also has other benefits like being able to not repeat code in multiple places. Or you could use an array and a loop like this:

Code:
lookfor[0] = obj_1
lookfor[1] = obj_2
lookfor_num = 2//The array has two things in it right now, #0 and #1
found = false
for(c = 0;c < lookfor_num;c++){
  if(instance_exists(lookfor[c])){
    found = true
    break//leave the loop early to speed up, since we just want to find either object and we just did
  }
}
if(found){
  //Do something here
}
Actually, they are all under a category of enemy. If I have an enemy parent, how will I go about looking for it? Just instance_exists(par_enemy)?
 
M

M3gal0maniac

Guest
sorry for my inexperience, but I'm abit confused.

As my 'parent object' has some codes in it, for a parent object to have its events run I will have to place it in the room as invisible? Hence, doesn't that mean that the parent instance exists in the room?

I did try 'if !instance_exists(par_enemy)' but it does not trigger the event of the if statement.
 

NightFrost

Member
Note that you cannot give an object multiple parents, which would be a nice convenience to categorize objects into type groups but I guess GM doesn't want to deal with potential of infinite loops (though those could be discovered with tree traverals). You can however grandparent, giving parents to parents and create a tree, which will allow you to catch large groups of objects.
 

Yal

🐧 *penguin noises*
GMC Elder
As a rule of thumb, any code that refers to an object also includes all its children as a "special case". It also works recursively with "grandchildren" and so on, so you can use as many layers of parenting as you wish. (For instance, in one of my platform games, I have a hierarchy of "terrain in general" having an "auto-tiling terrain" child and a "uses separate sprite" terrain child, where the latter also has "moving platforms" as a child; each of them except the 'terrain in general' parent has children, and in my collision checking scripts I only need to check for the 'terrain in general' object)
 

NightFrost

Member
As my 'parent object' has some codes in it, for a parent object to have its events run I will have to place it in the room as invisible? Hence, doesn't that mean that the parent instance exists in the room?.
It is usually best to create these parent objects as placeholder objects that have no sprite and no data. For example, I tend to have an "obj_enemy" object that simply acts as parent to all hostiles, and does nothing by itself, and may be a child to some larger grouping. Also, the room doesn't need to have instances of that object.
 
M

M3gal0maniac

Guest
ahh, thank you so much! I did not realize that the parent_object does not need to be put in the room. Learnt something valuable today! First post, great community. glad to be part of it. :)
 
Top