Okay, I’m having a problem trying to figure out how to convert a string to an int… My code in the game is a little too large to show, so I’m just going to make one…
We have a server that reads the highscore as a string, and then displays the leaderboards on the game as a string. But what we want is when a player has reached another players’ highscore, a clone of the other player will instantiate to show the servers highscore. This requires the game to read that highscore as an int.
The easiest way to convert an string to an int is:
int a = (int)string b;
but it will still give a bug as your string also contains an name and an string of letters can’t be converted to an int only numbers can be converted to an int. There is a work around of course but that would mean you would first remove the name part from the string and I don’t think that is what you want.
so easiest would be to do as remnan3000 told you and that is to separate the score and the name.
Renman, I could do that, but they are being fed from the server. It all comes into the game as one string… So, I would still have to separate from the string no matter if I separated their types or not. I think appels may have found the correct answer. I’m going to implement it now and tell you if it works.
I’m Epictickles friend.
The problem we’re facing is that it’s taking it as a string, with spaces and linebreaks.
For example
“John 200
Steve 15”
So when I split it I get stuff like
“200
Steve”
In one array index.
More specifically, where sTemp is the reading of the highscore
string[] lines = Regex.Split(sTemp, " ");
foreach (string line in lines){
print(line);
this prints:
John
1122
John
201
Jane
16
John
I separated them with a line so you could see each index of the array. I’m going to try what was suggested here.
No, I got everything to work fine now… Except I keep getting an annoying FormatException: Input string was not in the correct format, on line 121.
Here is the code:
IEnumerator GetScores()
{
//this.gameObject.guiText.text="Loading Scores";
//gameObject.guiText.text = "Loading Scores";
print ("Loading Scores");
WWW hs_get = new WWW(highscoreURL);
yield return hs_get;
int index=0; // Index of my loop
print(HighNumbers.Length);
string sTemp=hs_get.text; // putting the highscore text into a string called sTemp
string[] lines = Regex.Split(sTemp, " "); // Splitting it based on spaces - should probably be changed to | but fuck it i'll do that later
foreach (string line in lines){
int i;
HighNumbers[index]=int.Parse(lines[index]); // putting the content of line (which was split based on spaces) into index of HighNumbers
print(HighNumbers[index]+" "+index);
index++;
ScoresLoaded = true;
}
I parsed everything, and they all now return into an array, which I can see in the inspector… I’m just annoyed with the Exception… Any ideas?
Edit: The error comes from line 17 in this code, I didn’t post the whole script.
Well you split at string with both letters and numbers like “Letter 500” right? Then you go through it all with your foreach loop, and thereby also cast “Letter” which wil give you the expection.
To avoid it you could use int.TryParse, which will return false if it cant be cast. In this manner you can sort the wrong stuff.
int numberWeCheck;
bool checkIfCasted = int.TryParse(string1, out numberWeCheck);
If it is casted then it will set numberWeCheck to the string (out is a reference to the variable)