Displaying an integer as a list box item

Started by Stupot, Tue 11/08/2015 04:06:28

Previous topic - Next topic

Stupot

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

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

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

Is there another easy way to do this?

Thanks.

Gilbert

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
String tmpstr = String.Format("%d", playerOneScore);
listPlayerOne.AddItem(tmpstr);


I think even this works:
Code: ags

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





Stupot

Quote from: Gilbert on Tue 11/08/2015 04:18:11
I think even this works:
Code: ags

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 :-)

Stupot

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

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.

Khris

#4
You have to use an array:

Code: ags
String response[150]; // creates response[0] - response[149]

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

  // later
  Display(response[variable]);

Stupot

Quote from: Khris on Wed 12/08/2015 08:32:23
You have to use an array:

Code: ags
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.:-)

Khris

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 }.

SMF spam blocked by CleanTalk