Is it possible to store objects reference as a variable ? And for example to make a list of objects.
For example, I am making a game with about 1000 people. They have familly ties. I’d like them to remember who are their siblings, parents, cousin, friends… My first idea in other programming language would have been to create different lists of people called “Siblings”, “Parents”… These list storing pointers toward “people” objects. Also for some reason, it would be efficient if these lists were ordered (I can directly pickup the sibling n°2 in the list)
I don’t know how to do this efficiently in GDevelop. Is there a way ? (appart going into JS…)
I may have read your question wrong, but it seems to me what your are asking would fit in the Variable array. I am attaching a pic to help explain what I was thinking would handle that.
Please ignore if I misunderstood the question
You can use arrays and structures in Gdevelop. There’s an extension called “array tools” which adds a lot of functions. I’ve never used it.
Any variable you set for an object can be used to reselect or “pick it” later. Whether it’s an ID, a name or anything else.
In GDevelop, there are no object IDs. You got to think the other way around: instead of having a list where you put objects in, you put attributes on objects. You can then operate on the objects of your liking using object conditions to tell GDevelop what conditions you would like an object to meet in order to have your actions applied to them.
A simple example: If you have a team red and a team blu, and want to execute an action on all members of one specific team, instead of having an array of blu player objects and an array of red player objects, you would have a “team” variable on your object that is set to either “red” or “blu”. Then, to execute an action on all members of team blu for example, instead of iterating on an array of blu objects, simply add a condition
object variable "team" = "blu".
For a more complex case, like the one you are describing, you will need to model the variables you attach to your object in a way that lets you filter objects by all of your criteria. For example, let’s say you want to be able to get the children of a given parent, and to get the second sibling of a character. With those example requirements, it might make sense to model the data like this: an attribute for the generation, an attribute for the household and an attribute for the birth order. To pick the parents, you would use the household name to filter out characters of other households, and add another condition to pick among those the people of one generation prior, and bam you got the parent objects selected for your next actions. For the second sibling, use the household name to filter other households, then the generation to pick only the ones of the same generation, then pick the one with the birth order + 2, and there you go, the second sibling object is picked.
Hmm. Thank you. That’s what I thought.
But in terms of performance is it as efficient as storing objects reference in an array ?
Does it means that when I want to get the 3 siblings, GDevelop will iterate through the 1000 people to find those with the matching attribute ?
If so, it is definitelly not something that I will want to do, for each people, every frame…
You probably use some kind of ID or UUID to all your objects so is going to be easy and fast to identify their sibiling by ID like in any database.
You can do that manually or using an extension UUID.
The logic will be when an object is created assign the variable like:
MyParent.UID = x so when a child or brother or sister or wherever is related is going to be
ImChild.UID = j
ImChild.ParentUID = x
If you do something like this you later to get all the sibilings of an object you only loop in the parent only without the need of looping through all the objects. and 1000 records is nothing, if we’re talking for a database like mysql look in the world database just the cities are 4079 took 0.02 secs to get that info. I mean you don’t need to worry if the records are not millions in that case you maybe start to think in a better game engine.
mysql> SELECT COUNT() FROM city;
| COUNT() |
| 4079 |
1 row in set (0.02 sec)
Thank you. I had indeed that possibility to create ID (or UUID) in mind.
My issue is really about performance : my filtering loop would be more like testing each 1000 people against 1000 people. So, naively, I’d say 1 000 000 operations.
If I could store the objects references in an array, it would only be around 1000 operations (one for each people).
Anyway, I’ll try your solution and see how it goes. If the performance is still not good enough, there are some tricks I will try (using “Linked Objecst”), or even dive directly into JS and work with objects reference.