This might belong to programming 101, but i have been using GDevelop in its no-code modus operandi. However now i need the flexibility of arrays and their indexes to associate them with sprites.How do i do that?
What exactly do you want to do or know?
Have you read the docs?
https://wiki.gdevelop.io/gdevelop5/all-features/variables/
https://wiki.gdevelop.io/gdevelop5/all-features/variables/reference/
There’s also an Array Tools extension.
You need to pass the variable name as text inside quotes or using another variable to hold the name. It has nice functions.
can you give me a visual event sheet where i can use array indexes to dynamically control sprites. Like when i mouse click over a player name button the player avatar and the player tower and the tower name becomes visible automatically, I know i can do this through hide layers/groups/sprites, but i want to learn a sophisticated way to do it, and yes player name and player sprites along with the associated array indexes need to be global
I continued to play with the character select project. I added events to create the buttons based on an array of the structure. It’s getting bigger.
Try me:
Source (click the green [code] button and select download)
This is the part that reads the array and adds buttons. The buttons are assigned an ID variable which is used to select the picture of the castle. In this case just a quick doodle with the first letter of the country. The nice thing about the setup is that it’s dynamic. So, the characters don’t need to have the same number of children and you can change the array without having to change the events.
Then when a country button is clicked it uses the selected player and the clicked on country
The current version of the global structure
I have “locked” in there because I wanted to experiment with locking locations. It would easy to add an event to check the value of the Boolean and decide whether to make a country appear and/or be selectable.
Can anyone provide me with a customized event sheet where clicking on a button shows the player name, his avatar and his tower sprite highlighted with glow.
Heres my global variable setup:
Heres my main editor screen:
Seeing the things in an event sheet coming to life like i want to will help me learn how the whole concept of variables work in gdevelop. It might take a bit of effort, but my heartfelt thanks in advance!
This goes on for a while. It could probably have been shorter and more precise. I wrote it on my phone and I tried to add as many details that I could think of.
The most basic array is VariableName[number]
Tower[0]. (the first element is zero)
If you need to add a structure after it
Tower[0].Ammo
Tower[Variable(i)].Ammo
Tower.[Button.Variable(ID).Ammo
Any place with a number can be replaced with a variable (scene, global or object) or a formula or any other expression that returns a number.
To make the variable easy to save or convert its best to keep everything under one name.
PlayerCharacters.tower[0].Ammo
The shorter you keep the names the easier it will be to write and debug. While keeping it descriptive.
Structures use strings for the children names they can be written out or put inside braces and put into quotes or string variables of any type or any expression that return a string
Game.Towers.Excalibur
Game.Tower.Excalibur.Ammo
Game.Tower[object.VariableString(id)]
If you use quotes, it’s usually to build a name like by adding a number to a string to form something like “level1”
Game[“Level” + VariableString (Level)]
If you add braces or brackets then you don’t need a period but if you add childName then you do.
Game.Level
Game.Level[1] for array
Game.Level1 for structures
Game[“level1”] for structures
Anything past the variable name can be replaced with a variable either a number for an array or a string for a structure.
You can use for each child to go through the children like the for each instance It works the same for arrays or structures.
The for each child has a spot for 2 variables. By default the first is child the 2nd can be anything. I usually use childName
This will return the children values and their names based on the supplied variable name. This can be any level that has children not just the top level.
Let’s use fruits as an example.
Fruits.Apple
Fruits. Oranges
Fruits.Grapes
For each child of Fruits would return
Apple, Oranges and Grapes
If you added further details like the size or color then you could list the color of grapes. With Fruits.Grapes
Fruits.Grapes.Red
Fruits.Grapes.Green
Fruits.Grapes.Purple
Would return Red, Green and Purple.
Again, you can replace any child of a structure with a string in brackets.
You can also use repeat to go through an array using another variable. There’s an expression that tells you the number of elements. You can use that for the number of times to repeat.
Set variable i to 0
Repeat number of children in Game
Do something that references Game[Variable(i)]
Set variable i +1
Ok. That’s all. I probably went on way too long. I apologize for any typos.
Sorry, its dumbo me again, i was trying to customize your structure with my game, but I am enscountering a problem, the label text displays behind the button sprite, i think its a layer issue, if it is how do i change the layer of text generated on the basis of variables. Here’s a screenshot:
heres how the game screen looks now(I havent implemented the click system yet!
And a whole, whole lot of thanks to @Keith_1357 for helping me so much
On a closer inspection, it is just showing the index values as text
Should i change the root variable to a structure?
I’m having some issues. My Windows profile got corrupted or something I’m in the middle of trying to fix it.
The object variable doesn’t need to be a structure. Just remember, arrays use numbers and structures use strings. It sounds like either a layer or z-order issue.
no, issues, thanks for there for me
I cant figure out this part, how do tell the game engine to go through all my array indexes and print their string values to serve as label text.
The previous code example returns this:
Good news. My PC seems fine. I did some backing up just in case but when I rebooted my PC, everything seems fine. I ran 4 separate tests. They did not find a single error.
If you’re creating the buttons, then you can use a for each child like in my example. If the buttons already exist then IDK. You could give each button an ID variable or use a for each object
Something like this as long as there are 4 children and 4 buttons.
Or if the buttons have a sequentially numbered variable named ID.
well, somehow, i have managed to dynamically set the player avatars and their towers. You can find the scene here:
But now, i need to dynamically add text when the button of a particular player is chosen! Here is my eventsheet
My question is that how do i properly configure the last line(both sides markeed with red)
worked on the issue for the whole day, here’s where i am stuck, can i make the index of an array a variable, it may sound, but these programming info seems hard to grasp at times. the intent is to create an array that will dynamically display a text based on a variable array index. is it even possible?
Variables are very flexible. You can have any combination you want or need.
Instead of an array of players or characters, I think what would work is an array of structures with children of name and tower. You could then set the button text to the name and a button string variable to the tower. You could then retrieve the tower name from the button.
Variables
Events
When you use a for each child it will return either the value or a structure.
If the children are an array then child would be the value and childName the arrary index or element number. meaning 0,1,2
If the children are a structure then the child is the structure and the childName is the name of the structure. You can then use the variable child like the structure as in child.name and child.tower