Group variables (Share one variable with entire group)

Sometimes, you need to have several different objects own the same variables. For example, giving all enemies an HP variable.
Imagine that you have hundreds of objects in your game, but now you realize that you need a bunch of them to have the same variable. Now you have to spend a good while adding that variable to every individual object, one at a time. This is quite time consuming, and possibly risky, since you can accidentally have a typo when copying the variable, which can easily break the code and take a while to find and fix.
Wouldn’t it be super convenient and speedy to be able to create a variable that is shared with an entire group? It would greatly speed up development time, and it sure would beat having to go over each object in a group to add the same variable over and over again.
How it would work: If you right-click over a group, it should have an “Edit variables” option. When you add a variable to the group, then this variable is copied over to EVERY object that exists inside the group. If any new object is added to the group, the variable will automatically be copied over to the object. If an object is removed from the group, then the variable is removed from the object. If the group variable is deleted, then the variable is deleted from EVERY object in the group.
Out of many things I’d love to see in GD5, this would by far be the most convenient addition.

So what do you all think? Yay or nay? Feedback is appreciated ^^

1 Like

Why not use the events sheet?

For each Enemies, set variable HP of Enemies to 100.

I mean, sure. There are events to set the values of the variables. But I’m saying there should be something in GDevelop to ADD new variables to ALL objects in a group.
Once the variable is added to every object in the group, then use this event:
Set variable HP of (GroupName) to 100
Although, you probably don’t want EVERY enemy in one group to have the same health…

Of course! When i programmed some gestion applications in the past, i used to initialize some data with a routine dedicated: i didn’t enter these data directly modifying the files on the disk with a texte editor.

But i well understand it will be very pleasant to have the functionnality you said in GDevelop.

You don’t need to declare the variables in advance, you can simply use the event I wrote above to declare+set the variables. The only downside is that you can’t access them through the variable list:image

I’m not saying it’s a bad idea, just saying that you are asking the devs to implement a feature that can be done on your side in a single event… 30 secs on your side, 30 hours on the dev side. :grin:

I am not sure what this would bring to the table. The only thing I can come up with, where it would be good to have is when objects are in multibe groups and you just want so single out some groups from groupevents

So you’re saying that I can create new variables for objects using events, rather than just creating them manually?
Sounds quite handy, but for the game I’m making, it requires me to manually set certain variables for each instance of the same object to make it more interesting (E.G. I have a chest in my game that gives loot. If loot variable is 0, give health. If loot variable is 1, give gold. If loot variable is 2, give weapon, etc, etc.)

Well, I mean you know when you’re making some enemies in a group or something of the such, and for example, you want them each to have their own health values? To do that, you have to add a new HP variable to each enemy object one at a time.
When you declare the variable via the group, then the (empty) variable is copied over to every object in a group instantly. After that, you can use manual typing or events to declare the values of each enemy’s HP (E.G. Enemy 1’s HP = 10, Enemy 2’s HP = 50, etc.)
However, Gruk did tell me that there is a way to accomplish this with events. I’ll have to look more into this.

this is exactly what i mean, they all should have INDIVIDUAL HP, unless u want a group for each HP stat shared, which would not make much sense.
if you have losts of overlapping variables, copy an enemy that has similar variables and then tweak. if you want to add lots of shared variables later on, use events at the beginning of the scene to generate those variables.

I suppose. I was just trying to say, in case you use an event like this:
If variable HP of ENEMYGROUP = 0, then kill the corresponding enemy
…it would be less code then writing this:
If variable HP of ENEMY1 = 0, then kill enemy1. If variable HP of ENEMY2 = 0, then kill enemy2, (Repeat for every enemy)
I guess I should look more into this and experiment to see

by this approach, you dont need groups in the first place.

What should I do then?

you just wrote it yourself.
just add the variables manually, cant take that long. you have to do the typing anyway, you would just save to type “HP”

Okay, I see what you mean now. I was basically trying to make my code as flexible, clean, and reusable as possible, but I guess I was overthinking my suggestion ^^;

are the enemies all instances of the same object or different objects? If you can make them instances, then the issue is easily handled. One way to do this (if possible) is to have one object Enemy with different animation “skins”. I did something like this in my game, where I have 4 borders around a tray and I wanted to identify when any object is overlapping the tray border. The For each instance of … is your friend here.