Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: shedcore on Sat 11/07/2015 16:20:43

Title: Using a global variable in a GUI Listbox
Post by: shedcore on Sat 11/07/2015 16:20:43
Hello,

I am trying to build a GUI that holds a variables in a Listbox, ideally looking something like this:

Milk            4   pints   
Chocolate Milk  0   pints
Orange          2   pints
Jersey Milk     1   pints

These variables would change depending on whether the player takes the items or not.

I have tried to build the list using Additem as follows:

Code (ags) Select


Listbox1.Additem ("Milk      %d   pints", Milkamount);


I am being told that "wrong number of parameters in call to Listbox1.Additem" and have no idea what this means!

I have read the manual attached to the software, read all the tutorials I can find and watched all of Densming's excellent videos on youtube, but I cannot seem to find a way around this problem.  I can see on the forums that people have asked a similar question and seem to have solved their own problems, but frankly I don't understand the solutions they have been given and I felt it would make more sense to explain my specific problem.

Any help would be greatly appreciated - I am something of a novice at scripting, but think I can grasp most things that make sense!
Title: Re: Using a global variable in a GUI Listbox
Post by: Crimson Wizard on Sat 11/07/2015 17:31:47
ListBox.AddItem can take only 1 parameter, which is a string.
To deal with situations like this you are combining 2 function calls: a creation of formatted string, and assignment of that final string:

Code (ags) Select

String s = String.Format("Milk      %d   pints", Milkamount);
Listbox1.Additem(s);

or shorter
Code (ags) Select

Listbox1.Additem( String.Format("Milk      %d   pints", Milkamount) );

Title: Re: Using a global variable in a GUI Listbox
Post by: monkey0506 on Sat 11/07/2015 18:08:51
As an additional tip, functions that support string formatting directly always end with "..." as the last parameter:

AbortGame(const string, ...)
Display(const string, ...)
String.Format(const string, ...)

There may be others (don't recall ATM) but these are common examples. If the last parameter isn't "..." then you can just call String.Format in place of the string parameter you want to format. ;)
Title: Re: Using a global variable in a GUI Listbox
Post by: shedcore on Sun 12/07/2015 00:36:01
Excellent!  Works like a charm!

Thank you both.