I have a group, called ObjectGroup. In that group are a number of sprite objects, including one named “SilverCoin”.
In my scene I have a number of the ObjectGroup objects scattered around, including a SilverCoin.
If I try to delete the SilverCoin object with this :
It does not work. The object remains on screen.
But if I use a Repeat for each ObjectGroup, it does delete the SilverCoin object :
Is this a bug, or am I expecting the wrong behaviour from the expression only condition?
With the first part of your condition,
you are calling an instance of ObjectGroup without any prior conditions to target a specific object/instance, so GDevelop picks any instance of that group existing on the scene.
I would guess that it will be the instance most top-left of the scene, or closest to X/Y 0/0, but I might be wrong.
I would have thought it would find all objects from the group that have that name, rather than just pick the first one and see if the name matches. I expect it to work similarly to other conditions that filter a group of objects.
If I’m incorret in my expectations, how can we select the group objects that match a specific name?
For me, this condition removes everything that is in that group. It doesn’t select a specific instance because of the expression.
A workaround would be to give each object in that group a variable with the object name.
But this is probably not what you are looking for.
So I believe this issue is due to how object selection works for comparison conditions (or how it doesn’t work), although I could be incorrect and I’d want others to chime in more to confirm such as @arthuro555 .
The “Compare” expressions (string or number) doesn’t do a lot in the ways of object selection. Just “Is this true or false”. My understanding is that they can select objects but not to the same extent as other object-specific conditions unless.
Repeat for each starts by selecting an object, then only proceeds if the sub conditions are true. Since you’re giving it a “True/false” equation with your compare condition, it’s able to continue on, and since it already selects the object, it can delete it accordingly.
Gotcha. And so if there’s a number of eligible objects eligible, it’s pot-luck whether the right one is selected for comparison. Which would explain the non-deletion of the object.
Yep, only object conditions do object selection. The compare two number actions is a free action, meaning it is not using an object as a parameter, and therefore the engine doesn’t do object picking. That’s an optimization and a way to make the engine more logical, since object picking works by executing the same condition multiple times, once for each instance of the object passed into it. Conditions and actions that do not have an object as a parameter, for example,
Compare if Random(10) = 1 is not executed once for each object.
If an object expression is used like this, by default, it just takes the first object of the objects list and executes the expression on it, unless you are in an object action or condition. The for each object event will run free actions and condition once for every instance of the object, hence why it works.