I have 4 instances of the same button sprite in my scene, so 4 buttons all with different instance variables, (name, cost, gain, ID)
So say
Button 1
Button 2
Button 3
Button 4 (all the same object)
I used ‘pick all instances’ in my events so when i click these buttons they all work fine and have their correct instance variable values.
I now want to create a text object to display these different values on top of the buttons, but no matter what i try it only shows the first instance of the button values 4 times.
So the text shows -
Button 1
Button 1
Button 1
Button 1
Ive tried using the ‘pick all instances’ event but its not working. i also tried creating a ID variable for the button and the text object and when they match to then set the text but i still got the same result.
Does any one know how to fix this issue or the correct way to achieve this? I’m new to Gdevelop and i love learning this program and this is the first time I’ve been completely stuck.
Basically i want 4 button sprites with all their different instance values and then i want 4 text objects on top to display those values.
You probably won’t need to use “pick all instances”, rather use “for each” instead.
Like, for each text object,
if text object id variable is equal to button id variable,
change text of text object to “button number”
Something like that.
Then again, maybe for each isn’t even needed since they shouldn’t have the same ids anyway. But I’m not sure. You can try it yourself though.
Yeah, you don’t really need IDs, as long as you have some other way to pick one from the other:
Repeat For each Button
ButtonText is in collision with Button | change text to Button.var
or
Repeat For each Button
ButtonText.Y() = Button.Y() | change text to Button.var
( ^ works if you have them arranged vertically and you know Y position will be the same)
Or if you are going to have ID anyway, you can certainly use that:
Repeat For each Button
ButtonText.ID = Button.ID | change text to Button.var
Object picking can be confusing especially if you already have programming experience, as its kinda “inverse” to the way you would think about it in other environments. Think of it as a filter. Everything starts out “picked” - conditions filter them out. An action that operates on Button, with no condition, will operate on all Buttons at the same time.
For Each goes through the designated objects one at a time instead of in bulk. But, since you’re also trying to work on a second object here, you need to filter the second object so that the action isn’t operating on all of them at once. (by the way, For Each isn’t a regular condition, it’s a special event in the right click → Add menu)
Pick All is only useful in certain logic pathways where you want to do some filtering, but then go back and pick something else that was previously filtered out. Imagine you want to change all of the buttons to match a specific button… you would first need a condition to find the specific one, read the information into some variables, then Pick All buttons to write the saved information to all of them.