[SOLVED] - How do I create an object based on its name and then use the instance in another event

How do I edit an instance of an object created by its name in a previous event

Basically, I have a scene variable of type “structure”:

something = {
    "a": {
      "name": "Object1",
      // ...
    },
  "b": {
    "name": "Object2",
    // ...
  }
}

I want to loop over the properties of this scene variable and create instances of the object whose name is the value of the “name” property. I’m aware I should group them and then use the action “Create an object from its name”. This is not the issue.

The issue is that I want to edit this particular instance in events after its creation, for example link the instance with another object.

The only solution I found so far is to manually create each event and select the object manually etc. The problem is that I would have to duplicate a lot of events and edit just a few parameters for each actions, etc. It’s quite annoying and I’d like to have a better solution.

I’m kind of new to GDevelop and I’m used to Godot where I can just instantiate the object and manipulate the instance using a temporary variable, something that doesn’t seem allowed in GDevelop.

An example below: (don’t pay attention to the underlined error of the first action, the problem is at the second action)

IDK if what you EXACTLY expect to do is possible
But i can explain my 1st idea

Just to be clear it would work if copies of your object could have same exact name
If not then what is written below will not help

IF you need just a copy of same object and if you want to edit it AFTER it’s creation
Then i think my idea would be to go with instance IDs

Instead of having Object1 and Object2 or even Object 3 Object4 Object5 and so go on

You should just have Object which have copies of itself and each copy holds different instance ID

Look here


In red area at beginning of the scene i create 7 text objects from itself
Each time object is created i give it instance ID +1 (GiveID scene variable)
I mean like object is created to scene variable GiveID 1 is added and so we are going with 1 2 3 4 5 6 7… number of that variable
And with each created object this number it is given to him

Next in green area depending on ID variable of that object with exact same name
I do different crap to it depending on that ID variable

In this case instead of creating 7 text objects for my setting window
I have just 1 text object from which i make copies of itself
And place them on the scene give them instance ID and then move them to proper spots via events and depending on that ID change their text to whatever i want

But again
For you that is only the solution if you do not need to care about name of object and you only need copy of it

So idk if that gonna help you

And as you see i have only 1 InstanceText object on screenshot below
Just to be fair here i was creating too many text objects and i decided to explore how instance ID system works and it was last text object i added to object list
And since this is just my test project to test stuff i see no reason why to delete previous text objects and use only instance text

I just created a couple of examples in this thread. In your example, I’m assuming it’s bc the link action isn’t in the same event or a subevent. If you fix your issue in red and move the link up or make the event line a subevent then it should work. :crossed_fingers:

Hi, I tried those solutions but I don’t think I explained the issue correctly. Let me be more precise. Basically, I want to re-create the solar system in 2D, and each planet would be in an elliptical orbit around a sprite called “Sun”.

To do this, there is an extension called “OrbitingObjects”.

However, I’d have to copy-paste 18 actions (two for each planet + pluto) + a lot for some moons and tweak a few parameters for each action, which is very annoying I must admit. To fix this, I created a structure called “PLANETS” and the keys are the name of the object they are meant to create:

PLANETS = {
  "Mercury": {
    "orbitSpeed": 3,
    // ... etc.
  },
  // then "Venus", "Earth", etc.
}

The problem is that the action to create this orbit requires an Object. I can’t give a variable to this parameter. I decided to open the extension in the editor and try and do it myself. I created a copy of the action but I changed the type of the parameter of the orbiting object to a Text.

As a consequence, I had to use “Create an object from its name” action instead of “Create object”. Each planet is in a group called “Planets”. I also added this parameter.

Unfortunately, the instance of orbiting object is used a lot in the program, and I don’t know how to manipulate this instance. I was hoping that maybe I could store an instance of this object, created dynamically, in a temporary variable, but this doesn’t seem to be possible in GDevelop.

Using an ID variable is confusing, because how do I access it? In addition, the extension also sets its own variables, from what I understood from reading its event sheet.

Here what I modified in the extension:

wait one sec apparently I’m not allowed to post multiple screenshots

What it was initially:

The “OrbitingObject” parameter is used everywhere, in events I don’t even understand.

So, here is a better way to formulate my question: How do I make it so that the orbiting object is created from its name?

Using ID variable is like

You use condition if OBJECT with said ID exist
Imagine you have 20 apples and all are named Apple
But to each apple you assigned a number 1-20
Now you do condition
If ID of Apple is equal = to 4

Action
Give apple to zero

Condition
If ID of Apple is equal = to 17

Action
Give Apple to to Keith

This way even so all 20 objects are exactly same apples
I and Keith won’t be able to be given same Apple

So basically thx to condition check of ID you can do whatever in action with it

In condition you only check what ID object have

While in all actions you just pretend you referring to on and the same object
You could even change string variable of object to something
So if you would have object named Planet
You could depending on its Instance ID go with

Condition
Planet ID is equal = to 5

Action
Change Planet NAME variable to Mars

And now after you set your planets you could instead of going with ID 1 ID 2 ID 3 and so go on

Go with
Condition
Planet Variable string = Venus

Action
Tint Planet to blue

So, if I understand this correctly, I could create only one object named “Planet” which would have an object variable named “NAME”. Mercury would be an instance of the object “Planet” with variable “NAME” set to “Mercury”, and so on…

When looping over my “PLANETS” structure, I would create only one Planet instance, and then customise it depending on the value of its “NAME” variable.

Condition: if Planet.Variable(NAME) = “Mercury”
action: tint planet to grey.
action: set size to Earth_SIZE * ratio_earth_mercury

action (with no condition): orbit Planet around Sun

All right, mayyybe it would work, however there is one slight issue. Each planet has a unique sprite animation. How could I set the animations dynamically on an instance of Planet (which is a Sprite) using this technique?

“So, if I understand this correctly, I could create only one object named “Planet” which would have an object variable named “NAME”. Mercury would be an instance of the object “Planet” with variable “NAME” set to “Mercury”, and so on…”

NO

Looking at screenshots i provided above
When you create copy of object you use scene variable to add 1 to itself (GiveID)
So each time object is created this variable goes +1
And so each time object is created it is given that NUMBER variable
Which is set to that object NUMBER ID variable

Imagine having a counter

You create OBJECT it goes up 1 digit
And number that is displayed at that exact moment on well display (current number) is given to that OBJECT as it’s NUMBER ID variable

And now based on that ID number variable of object you can do whatever you want with it

You could also prepare same event i have with scene variable GiveID
To be like
If GiveID = 1
Change Planet Name Variable to Mars

IF GiveID = 2
Change Planet Name Variable to Venus

And instead of giving ID variable to Planet you would give Planet Name Variable to Planet at the moment of it’s creation

But that is only the matter WHEN you gonna give your planet name variable
In above example you give it at creation

In my previous post you do it in separate event after your planets are created

But effect will be the same just matter of how you gonna prepare your events

Most simple way to explain it to you so you can picture it very easily is

Imagine you are baking breads
Each bread is the same
How you distinguish one from another?
Best solution is to give it name tags

But we want to automate this process

So instead of you manually slapping name tag with
Bread for John
Bread for Trevor
Bread for Lisa
Bread for Anna
You slap current time stamp on each bread automatically
And now you can tell that bread created at 12:15 will go to Trevor
While bread created at 13:20 will go to Anna
So they do not fight over which bread is theirs

I hope you get it now
If not i have more stupid examples

ok, I think I understand now. I could be wrong but to create an object from a group with an extension, I think you need to use the extension’s object group not the scene group. The downside is you would have to use a parameter for each object.

An alternative might be to create the same object but change the animation. You would only have to supply 1 object and 1 string

To create an object from a group, the object name would be the name of the parameter not the name in the scene.
With these parameters, objectName, obj1, obj2, x, y

You can use this event in the extension

With this extension group

and this scene event using a button for testing

If you used the animation method, it would be 2 parameters an object and a string (or number if you prefer) and then change the animation as normal.

Hi, I’m trying to apply your solution using the animations method.

I have an object called “Planet” which has a few animations: “Mercury”, “Venus”, etc.

I modified the extension and I think it should work.

However, I’m struggling with another matter, the loop:

Here the scene variable named “PLANETS”:

Here the result I get on the screen for the 3 texts I created:

I don’t understand the "0"s. The initial text was different so it’s the value GDevelop gives me. The weirdest thing in this is that if I use “childValue.ratio” (for example) for the 3rd text, it works (it gives the right value).

I started GDevelop 3 days ago, I hope my question isn’t too ridiculous

If a variable is text you need to use VariableString() As is I believe it’s trying to retrieve the value as a number but since it’s text/string it returns the default value of zero. Ratio works because it’s a number,

1 Like

Sorry i did not read this message correctly

To answer your questions directly

So, if I understand this correctly, I could create only one object named “Planet” which would have an object variable named “NAME”. Mercury would be an instance of the object “Planet” with variable “NAME” set to “Mercury”, and so on…

No
You have one object named whatever you want and you can create as many copies of it as you want
Idea is to distinguish one from another you give them ID variable

Not name but ID variable a NUMBER variable
Like i mention in previous post to automate creation process
You add to that variable 1 on each created object
You would either need to prepare another event in creation process or after it for giving object STRING variable (where you can put in its name)

If you go by NUMBER ID
Condition
ID variable of object PLANET = 3

Action
Change animation of object to (and here you either go by animation number or name)

If you add STRING variable to objects (a NAME variable)
Condition
NAME variable of object PLANET = Saturn

Action
Change animation of object to (and here you either go by animation number or name)

Same goes to doing ANYTHING to specific object/planet

In same fashion you could have 1 object and 20 animation for it
Duplicate it on scene and change each copy animation
OR do it via events sheet on creation
Where you still would need to have some variable that would +1 on each object created

2023-08-19_16_04_I0ZSn5mYrn_ShareX

Now in events sheet i can do this

And even so as you see i have only 1 OBJECT but 3 copies of it
I can do different stuff to only one of them not affecting others

If you would use instance ID it would work exactly the same
Difference is
Animation is something you have and you change it
Variable is something you can GIVE and with whatever value or string you want

And to explain confusion between giving number and giving string (name/letters/word)

When you create that GiveID NUMBER variable you can make action to add 1 to it on each object created
Which automates the process

IF you want to give STRING variable then how you would change from
Saturn to Earth to Mars to Venus?

You can’t simply make something like add 1 to string variable
You would need to prepare/premade that strings manually
That’s why i am trying to explain to you it is better idea to just give planets ID NUMBER variables
And then depending on their ID give them PlanetName STRING variable
And that is only so you do not reference to planet as planet 1 planet 2 planet 3
So you do not need to remember which is which
Like you do not to remember 3 = Mars and 5 = Jupiter
But so you can reference them by their variable names which would simplify stuff for you

We all come here to ask questions (well some of us to answer them)
I see no shame in asking for help
I only see shame in believing it is not worth asking for help because of being afraid of something
So do not be afraid to ask
That is the purpose of this forum

Hi, I’m stuck again :frowning:

The method given by Keith seems to be working, and it looks like the easiest to implement. Basically, I have one single object named “Planet”, which is a sprite with a lot of animations: “Mercury”, “Venus”, etc.

I added a parameter called “OrbitingObjectAnimation” to the extension “OrbitingObjects”. When the instance of the planet is created, I just change the animation by name (I also added parameters for the width and height of the planet, and set it to 4000 pixels by default just to be able to see it properly during the countless tests).

So far, so good.

However… only the first planet of the PLANETS structure is being created:

(which is the Earth).

I identified the problem, it comes from the fact that the extension creates a specific number of copies of the orbiting objects. This “LinkedObjectQuantity” variable of “CenterObject” (the Sun) limits the number of objects the extension creates. Because I want only one planet of each type (only one Venus, etc.), this parameter will always be 1. For some reason, on the next iteration of my loop, this variable stays the same, so the extension thinks it created the maximum amount and just ignores the creation of a new object. I don’t know if my explanations are clear, because I’m not even sure I understand the issue.

Here the code of the extension:

When I log “This planet was created” in the console, it appears only once, for the Earth (which is the first child of the PLANETS structure).

The message “animation is going to be set to” works, but they never achieve the part where the object is created:

I have no idea how to make it so that the extension just creates the right objects, and then, once they’re all created, stops creating them again.

Ok, I figured it out. I’ll explain the solution I found in case it’s useful to someone in the future. Basically, the issue was that a copy of an object was created, and the rotation around the center object was applied on this copy. However, I needed to build the rotating objects dynamically, as the name of the orbiting objects were the properties of a scene variable of type “structure”:

For the orbits, I used the extension called “OrbitingObjects”, but because it doesn’t allow the creation of an object based on its name, I had to edit the extension so that it creates a copy of an object called “Planet”, and that this planet is transformed to fit the properties of each planet I wanted when the instance was created:

For the sprites, the Planet object has animations:

In the extension, the creation of an instance is done like this:

First, I needed to make sure, for some reason, that the planets don’t get created indefinitely. Originally, the extension controls the number of orbiting objects, using a counter, incremented at each object creation, and makes sure that it doesn’t create more objects than needed. For unknown reasons, my version of the extension was creating only the first planet of the “PLANETS” scene variable. As a consequence, because I’m used to develop in regular programming languages, I used a loop to iterate over each instance and using a boolean object variable on the unique CenterObject I can make sure that the same planet doesn’t get created multiple times.

Finally, for unknown reasons once again, I couldn’t use GetArgumentAsNumber() in the rest of the sheet because it was always the values of the first child of the “PLANETS” scene variable. By assigning them to the instance of Planet at creation time, I knew it was the right values, so I copied them onto this instance and I access the variables using OrbitingObject.Variable() instead of GetArgumentAsNumber().

It was quite difficult for an easy task, but I guess it’s because I’m not used to no-code.

1 Like