Help how to add a variable in a "GUI"?

Started by BowsetteGamer, Thu 27/07/2023 11:38:19

Previous topic - Next topic

BowsetteGamer

hello guys how are you, I would like to know how to add variables (text/numbers) that are present in the "gui" of the game at all times, as well as the @SCORE@ function that has the "gui", that it is also possible to add a variable, for example coins. but I don't know how to do it can you help me? ???

Crimson Wizard

#1
This is covered in Frequently Asked Questions in the manual by the way:
https://adventuregamestudio.github.io/ags-manual/FAQ.html

Look for "How do I put a variable on a GUI label?".

In general:
Code: ags
mylabel.Text = String.Format("Coins: %d / %d", current_coins, total_coins);
to keep this updated, either run this in function repeatedly_execute, or when the variable changes. Usually it may be convenient to make a custom function for changing a variable, which would also update the label at the same time.

BowsetteGamer

Quote from: Crimson Wizard on Thu 27/07/2023 12:01:19This is covered in Frequently Asked Questions in the manual by the way:
https://adventuregamestudio.github.io/ags-manual/FAQ.html

Look for "How do I put a variable on a GUI label?".

In general:
Code: ags
mylabel.Text = String.Format("Coins: %d / %d", current_coins, total_coins);
to keep this updated, either run this in function repeatedly_execute, or when the variable changes. Usually it may be convenient to make a custom function for changing a variable, which would also update the label at the same time.

thanks for answering me. I would have to put "mylabel" in the "gui" right?
I mean, you know the tag says @score@, @overhotspot@. I wonder if you put "mylabel" or @mylabel@ in the GUI? Or how is that specified in the gui

Crimson Wizard

Each GUI and each gui control (label, button, and so on) has a Name property. This is how this gui or control may be addressed in script. Same as with Characters, and almost everything else.
"mylabel" in the above example is a label's Name.


BowsetteGamer

Quote from: Crimson Wizard on Thu 27/07/2023 20:02:51Each GUI and each gui control (label, button, and so on) has a Name property. This is how this gui or control may be addressed in script. Same as with Characters, and almost everything else.
"mylabel" in the above example is a label's Name.



Exactly, but since I tell the GUI to show me the variable instead of the text, I am aware that "mylabel" is the example. but what do i have to write
"mylabel", "@mylabel@", "#mylabel#" or how to write that variable in the GUI. excuse me that I don't understand a lot

Crimson Wizard

#5
Quote from: BowsetteGamer on Fri 28/07/2023 01:19:54Exactly, but since I tell the GUI to show me the variable instead of the text, I am aware that "mylabel" is the example. but what do i have to write

No, you have a misunderstanding there. GUI's themselves cannot contain variables, they can only contain text. So the question is, how to create a text that shows variable's value (and whatever else you may need).

The "@SCORE@" is also a text, but the engine has a special behavior that automatically replaces "@SCORE@" with formatted score value at runtime. Same goes for "@OVERHOTSPOT@". This is just a placeholder for automatic behavior. But your own variables won't have automatic replacement, so you have to do that yourself, explicitly in script.

So in your script you have to create a text, formatted with the variable's value, and then set the text to the label. This is what String.Format is used for.

Supposing your label is called lblCoins, you write in script:
Code: ags
lblCoins.Text = String.Format("Coins: %d", coins);
// or just
lblCoins.Text = String.Format("%d", coins);
// depending on which format do you want

BowsetteGamer

Quote from: Crimson Wizard on Fri 28/07/2023 04:05:42
Quote from: BowsetteGamer on Fri 28/07/2023 01:19:54Exactly, but since I tell the GUI to show me the variable instead of the text, I am aware that "mylabel" is the example. but what do i have to write

No, you have a misunderstanding there. GUI's themselves cannot contain variables, they can only contain text. So the question is, how to create a text that shows variable's value (and whatever else you may need).

The "@SCORE@" is also a text, but the engine has a special behavior that automatically replaces "@SCORE@" with formatted score value at runtime. Same goes for "@OVERHOTSPOT@". This is just a placeholder for automatic behavior. But your own variables won't have automatic replacement, so you have to do that yourself, explicitly in script.

So in your script you have to create a text, formatted with the variable's value, and then set the text to the label. This is what String.Format is used for.

Supposing your label is called lblCoins, you write in script:
Code: ags
lblCoins.Text = String.Format("Coins: %d", coins);
// or just
lblCoins.Text = String.Format("%d", coins);
// depending on which format do you want


Thanks, this is precisely what I needed, because I thought it was happening like the case of @score@. I'll try any result I'll let you know, thank you very much

newwaveburritos

As an aside, making GUIs like that just for yourself that show your variables in-game like that can be very useful for debugging purposes.  It's a really good trick to know.

SMF spam blocked by CleanTalk