How do I use variables inside a GUI?

Started by MistrrW, Wed 14/12/2016 23:44:55

Previous topic - Next topic

MistrrW

Hi,
I have just started playing with AGS. I checked the manuals and watched all the videos, and I can't find the answer.

How do I call a variable inside the text of a GUI?  I want to display the name of the planet you are on in the status bar.  I have a variable called PlanetName, but how do I get it there?  I got it to display the score by using @SCORE@, buy how can I use my own variables?

Thanks.

MistrrW

I'm talking about labels, not text boxes that ask for user input, btw.

dayowlron

Add the variable in the Global Variables section and you can reference it from anywhere, however it wont automatically update a label on a GUI.
What I would do is create a function that sets the global variable and updates the label on the GUI, then anytime the planet name needs to change call the function instead of just setting the variable.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Crimson Wizard

#3
Not sure which part exactly was causing trouble, but since nobody mentioned this yet -

You can set any text by using Label's Text property:
Code: ags

lblMyLabel.Text = PlanetName;


if you need to put non-string variable, like integer, the conversion is done usually using String.Format function, for example:
Code: ags

lblMyLabel.Text = String.Format("%d", myNumber);


Where to put these commands depend on situation. The simpliest way is to put them into repeatedly_execute in the GlobalScript.asc, and that would ensure that your label is updated every game tick. If you do not need to do this that often (or feel the need to optimize your script a bit), then you can follow dayowlron's suggestion, and create a function that both sets variable and updates labels, something like
Code: ags

function SetPlanetName(String new_name)
{
    PlanetName = new_name;
    lblMyLabel.Text = PlanetName;
}

MistrrW

That's got it! 

Thank you so much. I was focussed on the place to enter the text and didn't think to call the name of the label.

Much appreciated!

SMF spam blocked by CleanTalk