ok for example i have a string i pull from php it look something like
xp:11|HP:99|level:99
what kind of script line would i need to pull the "99"after level out and convert it into a float?
ok for example i have a string i pull from php it look something like
xp:11|HP:99|level:99
what kind of script line would i need to pull the "99"after level out and convert it into a float?
You have a lot of choices. In C#, you could use string.Split. Youâd have to use it twice. First, split on the character â|â to get an array. Then split each of those on the character â:â to get an array of arrays.
Probably, something like this:
var s = "xp:11|HP:99|level:99";
var d = s.Split('|').Select(p => p.Split(':')).ToDictionary(a => a[0], a => a[1]);
var ninetyNine = d["HP"];
Thatâs off the top of my head, though. If itâs right, itâs all me. If itâs wrong, itâs the bottle of wine.
I would use indexOf to find it, then use substring to get it, then convert it to a float with parse.
excuse my lack of knowledge but i donât see where the 99 that is extracted then becomes a float?
In MaxGuernseyIIIâs post? They donât, ninetyNine should hold a string â99â, the dictionary has the string representations (presumably not every value will even be a float/int/whatever)
You can surely figure out the rest on your own.
@fire7side Yeah. You have a lot of choices.
You can use that method (get an index for the key, add the length of the key plus a colon, get the index of the next pipe, subtract the two to get the length of the number, call Substring to get the part with the number in it, hope to hell there arenât any spaces). You can write a regex. If you feel saucy, you can get a compiler compiler and make an LR(1) language that parses it out. You can write a regular expression that translates it to JSON or XML, then use the appropriate serializer. You can write a scanner that walks through all the properties and uses reflection to write the values into an instance of some class. Given the sample, you can even assume the length of all the characters and fields (I donât know why you would, but you can) and just hardcode all the indeces for the Substring calls.
Yeah, these type of questions are so open ended. Usually there are 3 or 4 answer ways of doing it and the OP tries to do some kind mixture, which doesnât work.
Yeah, they each have their advantages and disadvantages. Split works best if itâs the whole thing needing to be split up and itâs in a logical order, and the character isnât repeated inside what needs to be split. I hate comma separated for that reason. The pipe would be a lot better. Maybe thatâs what he was really asking. It usually is best to use split for dialog, etc.
@fire7side You also have to take into account implementation-difficulty, which is different when youâve been coding for thirty-four of the forty years youâve been alive than it is when you are just getting started.
On that note, how important is it to you, @Resilo , to use that exact format?
Could you switch over to something that is more well-supported by the framework, like JSON?
Also, why hasnât Unity made character-separated values something they support natively? That seems like a tool that could really help non-developers and developers bridge the gap.
its used to pull character stats from a database so currently the php script echos the stats in that readable text format it can be changed to be simpler if needed
Yeah. So the format is out of your control. Iâd do one of the things suggested herein. The question of how to convert a string to a float is a job for StackOverflow.
I agree that you can easily google the answer, but try:
float.Parse or float.TryParse