Create object from name and change other properties afterwards

I’ve got a scene with hundreds of similar object instances. I’m using ‘Create an object from its name’ to create a new object based on the contents of a VariableString. However, I also need to change the z-order of that new object, set a couple of object variables on it and link it to another object. The trouble is, I can’t figure out how to sensibly get a handle on the object I’ve just created.

For example, if I’ve got:
Among objects Pipes, create object named VariableString(TakePipe)

How do I now change the z-order? This does not work if placed immediately afterwards:
Change the z-order of Pipes: set to 10000

Currently the only solution I’ve come up with, and it’s awful, is to create my new object in a special layer, then I can repeat for all instances of Pipes, check the layer, and then I can change the z-order and those other things if the object is in the correct layer. However, that performs very badly, because I have hundreds of these things in the scene.

There must be a better way?

Is there a way to do this in Javascript perhaps? Does anyone know?

When you create an object it is automatically selected. This means any action in the same event or subevents will automatically only apply to that object that just got created.

Thanks for replying Arthur, but that is only true for the normal create object action - it does not work with ‘Create an object from its name’. The object that is created in this case is variable. If anyone has working code that does this please would you share a screenshot?

In that case I recommend using an object variable.
If variable initialized = 0 do:
Set variable initialized to 1
Change Z order to 67

As variables are by default 0 this code will only be executed once after the object has been created. Make sure to put that after object creation to have it get initialized before rendering.

That’s no better than my current workaround, Arthur, as it requires me to loop through every object checking for the variable, and as previously stated I have hundreds of these objects.

There must be a way of getting a handle on an object created with ‘Create an object from its name’. I guess the only way to do this is to resort to Javascript. Checking the docs, I see that createObject() returns a gdjs.RuntimeObject so in theory it should be pretty straightforward.

Ok so after checking into the code it seems the create by name does exactly the same thing as not creating by name. As long as you don’t use a for each event or an action modifying the selected objects list the object you have created should be the one used in actions.

Else yes you can use JavaScript to get a reference to the object.

“… the object you have created should be the one used in actions”.

But Arthur, how can I possibly know which one was used? It’s a variable. The action might create one of seven different objects. What will the action look like immediately afterwards to change the z-order of the object it just created? Do I have to do seven different conditions to test the variable for each and have a tailored list of seven different actions? That would defeat the purpose of using “Create object by name” in the first place as I could just have used seven different conditions and seven different “Create object” actions.

Use an object group to let the action use any of the objects in the group. The create object by name will automatically make the selected object the one you just created.

No that doesn’t work Arthur, I tried that before I posted my original question. I use ‘Create an object from its name’ which takes an object group. Then I tried applying actions to the group immediately afterwards, expecting it to apply to my newly created object, but nothing happened. That’s how I started, and that’s then why I originally had to come up with my workaround involving layers. Did you try this yourself? Does it work for you?

I didn’t actually try as I am currently away from home and my computer, but I know from experience that it works with the classical create object and by looking at GDevelop’s code it should also be the case for create object by name.

Yes it works with ‘Create an object’, but I guess the reason it doesn’t work for ‘Create an object from its name’ is because you have to give that action an object group. This feels like a bug to me.

I suppose a more performant workaround in my case that doesn’t involve writing Javascript will be to create a function that uses ‘Create object’ and sets all the object parameters I need, then add seven conditions testing my variable for each name and sending the appropriate object to my function. That will be quicker to compute than doing a foreach over every object in the game. Not ideal, would be nice if this works properly in a future release.

I mean… couldn’t you just create an object group with the object you want in it, in this case?

I tested this event the other day and the created object was targeted.

I have an object group already and it doesn’t work, that’s why I asked the question originally. Interesting that it works for you. I’ll do some more testing, perhaps it’s broken for object groups inside functions.