Controlling difference instance of a object

I am trying to create multiple instances of an object, but I can only control the newest instance of the object. Am I missing something?

I tried using for each instance of the object, but it only ever loops 1 time. I also tried repeat sceneInstanceCount which repeated the correct number of times, but the x / y coordinates are always the newest instance.

Trigger once under repeat for each works only for ONE instance

What you are trying to achieve cause i don’t get your logic?

Welcome. I went on a bit bc I don’t know what you know about GD.

When you create the new object, it’s becomes the only object in the pick list. So, using a for each instance would only use that object. You could use a pick all object instance of that object before the for each object. That would restore the pick list to all of the instances.

There are times when if you use a condition in an event before you add an object when those instances are already in the pick list so the new object would be added to those instances. So, the new object isn’t always the only object.

If you wanted to use a repeat, you would need a condition to pick just 1 object at a time. You could use a loop index counter and object IDs.

LoopIndex=0
Repeat number of object in scene times
… Object variable ID = LoopIndex
… … Do something
… LoopIndex + 1

If you add the new LoopIndex to the repeat event then you wouldn’t need to reset or increase the LoopIndex variable.

The for each instance would be the better approach. IDK your goal.

Conditions work like filters. Without a condition all of the instances are used for conditions,expressions and actions. Once you use a condition it picks the matching objects and that new pick list is used for any other conditions, expressions and actions or subevents. This continues until either a new event or certain actions or conditions. If you return to a previous event level (indentation) , that pick list gets reused. Once the events are no longer indented than all of the instances are used again.

There are exceptions like pick all. The compare number and compare string conditions don’t change the pick list.

If you used a collision condition then it would modify multiple pick lists. Picking only the objects that are in collision. You could start with check for collisions between 10 tiles and 10 enemies and end up with pick lists that have 3 tiles and 4 enemies. Other conditions could further reduce the lists.

Yeah, I am brand new to GD. Thank you for showing me how to do this. I think I get it now.

1 Like

I am able to access the instance with pick all. But can I use pick all / for every instance within another pick all / for every of the same object?

I am trying to find the distance between 2 objects in the same group. And if the distance is < 120, then do something. Numbers and Numbers2 are group of same objects. I just use 2 different group so I know which one I am calling.

Object groups create references to the objects in them. They don’t maintain their own pick lists. I wish they would. They create picklist for the objects that are referenced just as if there wasn’t a group. So, if you have 2 groups with the same objects, the pick lists would be identical because it’s looking at the reference objects not the list. In the end, it’s comparing the same object. It can be difficult to compare instances of the same object.

You can get around this in multiple ways. You could store the position of one object in variables and then compare the objects to the position. Instead of using IDs, I made it visual and drew a line between the objects.

I just noticed in your screenshot, the 2nd for each instance isn’t a subevent of the 2nd pick all object. It wouldn’t have made the project work bc of the same issue but the 2nd for each object wouldn’t be using all of the objects, it would’ve only used the object from the 1st for each instance. Each level or indentation maintains its own pick list. It can be confusing. There’s a lot of hierarchy.

Imagine each indentation as a room. In the first room are all of the objects. When you add conditions then it creates another room with only those objects. Another subevent and more conditions then you get another room. When you leave a room and return to an event on an indentation that’s more to the left than it’s like returning to that room. If you go back further than that room is used.

My result.

When you use for each instance it’s best to reduce the list first. That’s why I compared the objects distance twice. Before you check the distance the 2nd time, you would probably want to make sure they’re not the same object. In my example I could make sure that X ≠ Obj1.X() and Y ≠ Obj1.Y(). In your project, you could also save the ID and make sure the ID in the variable doesn’t equal the ID of the object in the 2nd loop.

There’s no condition to check for the distance between an object and a position. So, you need to use the expression inside a compare 2 numbers condition.

You could also use multiple objects as long as you were only checking for something like the distance between players or players and enemies. You would still have a problem if you also want to check the distance between players or between enemies.

You could use the same object and stick a 2nd object to the first object. Each object would be made of 2 objects. The 2nd could be hidden. Then you could check for the distance between obj1 and obj2.

You could the locations in an array ahead of time and then compare the objects to the array. You could also use the array by itself and compare element to element. This is getting more advanced and depending on the needs, a bit inefficient. The array would need to be continuously updated.

I would recommend either storing 1 position in an array or using multiple objects.

There are infinite ways to do things. We all have our preferences and knowledge.

Thank you. I ended up using the hidden object as you suggested. I was going to check the values of each of the object and destroy them if they meet a certain condition.

I was thinking this kind of things should be common and there would be an easy way to do it. Guess not.

The way GD does instances can be infuriating. There’s a simplicity with instances but there’s difficulties when trying to compare instances of the same object to each other. Player to enemy is simple. Enemy to enemy is more difficult.

You can use Javascript with GD, I rarely do, but if you use Javascript then you can select objects like an array by putting the index in brackets like tile[0] or tile[7] and then add the method or property that you want. That part is nice because you can easily compare instances without using another variable or object. You can just use tile[0].GetX() = tile[7].GetX(). It would be nice if there was a similar approach available without Javascript.

The Javascript part is well documented.

https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/modules/gdjs.html

I just wish there was a way to pick the objects like in Javascript.

1 Like

thank you. I will look into that.