Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Stupot on Tue 11/08/2015 04:06:28

Title: Displaying an integer as a list box item
Post by: Stupot on Tue 11/08/2015 04:06:28
Sorry if this is easy peasy, but I can't work out what to do.

I'm making a scoreboard that writes each new score underneath the previous one. I thought I could do this with an unclickable list box, adding each new score as a new item on the list.

However, I can't get the score to display an integer. I thought it should work in the same way as displaying numbers through labels or display windows, like this:
Code (ags) Select

listPlayerOne.AddItem("%d", playerOneScore);

but it doesn't seem to like it at all.

Is there another easy way to do this?

Thanks.
Title: Re: Displaying an integer as a list box item
Post by: Gilbert on Tue 11/08/2015 04:18:11
From the manual, this function is listed as:
QuoteListBox.AddItem(string newitem)
instead of ListBox.AddItem(string newitem, ...). The ", ..." means that a function will accept additional parameters for formatting. As there is no ", ..." for the parameters of this function, it is clear that this function does not support formatting.

You may just do:
Code (ags) Select
String tmpstr = String.Format("%d", playerOneScore);
listPlayerOne.AddItem(tmpstr);


I think even this works:
Code (ags) Select

listPlayerOne.AddItem(String.Format("%d", playerOneScore));




Title: Re: Displaying an integer as a list box item
Post by: Stupot on Tue 11/08/2015 04:53:52
Quote from: Gilbert on Tue 11/08/2015 04:18:11
I think even this works:
Code (ags) Select

listPlayerOne.AddItem(String.Format("%d", playerOneScore));


It does indeed. Thank you.
I tried messing around with String.Format, but I was clearly doing it wrong and I thought it would involve something a lot more complex.

Thanks Gilbert :-)
Title: Avoiding reams of if/else if statements.
Post by: Stupot on Wed 12/08/2015 07:16:36
This is a new question, but I don't want to clog up the forum any more with my ignorance, so I'll post it here.

I have a particular int variable which can basically be any number. Certain numbers trigger certain responses, each diferent.

I could just write:
Code (ags) Select

if (variable == 1){Display("response 1");}
else if (variable == 2){Display("response 2");}
.
.
.
elseif (variable == 150) {Display("response 150");}


but I know that's undesirable. And there are a lot of possible responses (over 100), so I'd rather just have a list of the responses and a more simple way to call the correct response depending on the value of the variable. I know this is not only possible, but basic bread and butter coding for most people, but again I find myself stumped and unable find the relevant information.

Thanks in advance for any help.
Title: Re: Displaying an integer as a list box item
Post by: Khris on Wed 12/08/2015 08:32:23
You have to use an array:

String response[150]; // creates response[0] - response[149]

function room_Load() {
  response[1] = "response 1";
  response[2] = "response 2";
  ...
}

  // later
  Display(response[variable]);
Title: Re: Displaying an integer as a list box item
Post by: Stupot on Wed 12/08/2015 08:45:32
Quote from: Khris on Wed 12/08/2015 08:32:23
You have to use an array:

String response[150]; // creates response[0] - response[149]

function room_Load() {
  response[1] == "response 1";
  response[2] == "response 2";
  ...
}

  // later
  Display(response[variable]);

Ahh, I can see how that would work.
I'll try it out. Thanks, Khris.:-)
Title: Re: Displaying an integer as a list box item
Post by: Khris on Wed 12/08/2015 12:04:17
For some reason is was using == instead of =, code fixed.

Btw, regarding your example code: you don't have to wrap a single command in { and }.