Need Help with GDevelop: Creating Buttons to Display Text

Hey everyone,

I’m currently working on a project in GDevelop and I’m struggling with creating buttons that display different text when clicked. For example, when you click on “Nudle,” a text corresponding to that button appears. Similarly, when you click on “CoreTech,” a text corresponding to that button appears, replacing different text. I’ve tried setting up variables and events, but I’m not quite getting it right. Can someone provide a step-by-step guide or example code on how to achieve this? Any help would be greatly appreciated! I have attached a screenshot for reference.

1st of all you should learn to use instance variables and so you would have only ONE text object in your object list
Put it multiple times on your scene
And each button would act as independent button

How it works is same as with animation
You can have ONE object in object list named zombie
Yet two zombies in game made from same object can have different HP (since one got hit already) and different animation

Basically you put that object on your scene click on it and then on the left in object properties window you add instance variable to it and repeat it for each object

By default variable name will be Variable (i did not change mine yeah i am that lazy)

And now in events i can do this

As you see all of them are EXACT same object
Yet they have different instance variable so i can reference them independently
And result is

While you see 4 different texts
They are all ONE single text object
They are just duplicates of themselves (click and hold your object in scene and hold left ctrl then move mouse to make duplicate)

And now if i would want to do what you are doing
I could do 2 things
1 - I could simply put in value of that instance variable of each text object Name i want to be displayed
2 - add two instance variables to each of them
1st would be called FOR EXAMPLE ID or maybe Number or maybe Tag
But lets stick with ID
and go from 0 to 5 or more depending how many text objects i have
And 2nd variable would be name of order and this variable i would name Order and for example in first text object instance variable i would set it to Nudle in 2nd i would set it to CoreTech and so go on (btw isnt it noodle?)
(important here pay attention what type of variable you give
For example if you go with text you set it to string but if number it may be wiser to set it as number variable)

Reason why i would wish to add 2 instance variables to each text is
I can do something to each text just by checking its ID variable
BUT i could then manipulate/change/edit Order variable as i want
So for example i could change text with ID 1 its order variable to NudleExtra or anything i want
While i would be able to still reference this text only by checking if its variable ID is 1

BUT if you do not intend to change this variables and they need/should/will stay the same forever then going with just one variable is perfectly fine

Now let’s say i click on my text object and i want its variable to be displayed as text on the right under price

And what i would do is (this is for 1st method assuming you left your variable name as Variable)

Condition
Cursor or touch is on text
Trigger once

Action
Change text of PriceText set to Text.Variable(Variable)

To explain what is what
PriceText is text that will show up on the right under price
Text is name of object in your case it would be name of your button
(Variable) is name of that instance variable i did not change when adding instance variable so if you would change it to Order it would be Text.Variable(Order)

And now just by hovering your mouse cursor over that text it would change text on the right If you also want player to actually click on that text/button
You simply add in condition mouse button or touch pressed

This way you do not need to declare what text to display
Since all your objects have exact same variable with just different value

And if you would go for method 2 then it would be exactly the same

WHILE you are not required to go with instance variables
You still can have different button objects where each is another object on your list
BUT then to simplify the process you do 2 things
To each button you add variable with same name
For example OrderName where in value of it you type the name of that order
And then add all buttons to some group let’s say Buttons (to object group not object folder)

And now in events you go with
Condition
Cursor or touch is on Buttons
Left mouse button pressed or touch held
Trigger once

Action
Change text of PriceText set to Buttons.Variable(OrderName)

And it would also work

So how you wanna approach it is up to you

You’re awesome, thank you!

1 Like