[Solved] Spawning objects with instance variables

Hello, I’m trying to create a system where objects can spawn in at different locations around the scene, and giving each of them a randomized amount of health.

Obviously, spawning them in is easy enough with the create object action, and giving objects individual randomized health is easily done with an instance variable… However, I’ve noticed that you can’t assign an instance variable to created objects.

I found another topic with a similar problem, and it seems to not have any solutions… is this just something Gdevelop can’t do, or do you need to write a behavior or something?

Just do it like you would intuitively do: create the object, and use the action to set an instance variable on the object. GDevelop object selection will automatically select the created object :wink:

However, I’ve noticed that you can’t assign an instance variable to created objects.

Assign your health value to an object variable.

How do I assign an instance variable to the object so that I can set it, though? Do they automatically assign variables to things when you set it up? You can’t modify an instance variable through actions, unless they’re counted as object variables, and if they’re counted as object variables, then how would I define them as an instance variable.

Also, I thought it would only know what object I was trying to select if the object was also on the conditions side.

Assigning health to an object variable means all of the instances of the objects will have the same amount of health throughout gameplay, which obviously, wouldn’t make sense.

Like I said, no. GDevelop has object selection, which means your actions don’t apply to each object by default, but only to the ones meeting every condition. That means that for example if you have a “condition object touching cursor” and a “apply force to left on object” action, the object you hover on only will be affected and start to move. This is the same with variables, it just applies the new value to the selected objects. Creating a new instance automatically overrides the selected object with the created instance (like I said in my first post).

Sorry for my misunderstanding. But I need each one to have separate health values though, and object variables apply to all instances in the scene, do they not? I thought that the only way to have separate values for each instance, is to have an instance variable, and I do not know how to assign one outside of the editor itself. This is the question that I’m trying to ask.

Like I said two times now, the action “chang object variable” only affects the selected objects instances, meaning it only changes the variable for the selected object, and when you create an object instance it is automatically selected.
If you don’t believe me or my logic, just try it, I can assure you that it is how it works, instead of telling me I am wrong without trying.

That’s the thing, I have tried it. Object variables apply to all instances in the scene, instance variables are the variables that only apply to that specific instance of those objects. If object variables did what instance variables do, instance variables wouldn’t even be necessary. Except instance variables are necessary because object variables apply to all instances.

Your original proposal was to set an instance variable with an action, except I don’t even know how you go about setting an instance variable in an action, and you have since changed your mind that I should actually be using an object variable when an object variable does not do the things I need it to.

Can you show us the code that you tried it with? Maybe the way you’re doing it is not what Arthuro555 means.

Anyway what I’m trying to say is that reading object variables isn’t tied to instances. I would have thought this is the purpose of instance variables, but you can’t assign instance variables to objects in code, only object variables.

Here’s some example code I spat out:


As you can see, you can’t differentiate between object variables of two different instances.

Your display text is the problem. It’s not linked to an object, so your event is just checking the first object variable it can find that matches.

You should do a for each on the text, as well as link the objects.

This works for me.

2 Likes

First thing you need to realise is you’re arguing something doesn’t work. However, it’s worked for the GDevelop community over and over again. This points to the possibility that you’re doing something incorrectly.

So, about your events. Here’s my take on it (@arthuro555 and others, please correct me if I’ve got this wrong):

In the third event you are setting the text of textObject ‘display’. The question is, which one? The answer is, all of them when the events are processed and neither events 1 or 2 are triggered. This will occur during the frame following the one where they are triggered. And as this happens so fast, it appears to be instantaneous.

You have not specified or narrowed down which one is to be modified. So GDevelop does them all. I believe that the value of objectVariable for each testObject is not what is displayed above it. You can confirm this by checking in the debugger.

One way to only update the text of the ‘display’ object above the ‘testObject’ is to link them when you create them in event 2.

Then change event 3 to a “Repeat for all testObjects”, with a subquery that has the condition “Take into account all display linked to testObject”. Only then will the correct display above testObject be selected.

[edit]
Beaten to the explanation by @Silver-Streak :smiley:

And for the record, I didn’t see what @Silver-Streak had replied before I posted mine.But they’re identical answers.

1 Like

Continuing the discussion from Spawning objects with instance variables:

Apologize for the late reply… Wow, okay. I had no idea that I could actually separate objects that way. Really sorry for my extreme ignorance; I’ve somehow missed the existence of linking in the documentation. That function alone helps me accomplish a lot of things I wasn’t able to achieve before. Thank you so much for enlightening me here. I guess this issue can now be considered [Solved].

2 Likes

Hello all, this thread is solved but I just wanted to add my thoughts. I myself happened to come across this “error” when using Gdevelop. In my game there are more than one instances of cloud object and I wanted to make them move at different speeds. So I decided to use instance variable but in action tab I couldn’t call them. I mean there wasn’t a code for it in Gdevelop. Later I thankfully noticed there was no need for it as other people mentioned up there :smiley: Because you create object variable and call it in action tab with “object.variable()” command. (In my case, I was using Cloud.variable(wndspd)). In scene tab, you can already easily change instance variable (when you click a instance) because object variable turns into instance variable when you change them individually. That’s because object and instance variable are the same thing for gdevelop, and thanks a lot for gdevelop making this much easier. :slight_smile:
An example video you can find here to understand the concept easier :