I’m looking for some guidance on how to write a script to read in an rss feed or other xml file off the web and then parse the information into variables. i have two uses for this if anyone has a better solution as well. Item 1 is a stock ticker that reads in latest stock quotes from an rss feed and adjusts elements wihtin unity to reflect the change.
item 2 would allow a user to play the game and save the file to an xml file online. Some one else could then continue the progress by reading in several variables from the xml file. Hope that makes sense. Any help you can offer is appreciated.
I haven’t played with it to know how it would work with XML, but check out WWW:
http://unity3d.com/support/documentation/ScriptReference/WWW.WWW.html
You could also access a PERL script which would process the XML, instead of accessing the XML directly, if that was useful.
(And .NET can be used for stuff too.)
Thanks for the links. That’s exactly what I needed. One last question which I’m sure is a stupid one. What is the best way to convert a string to number? I don’t want to loose the decimals so a float would be best. I’ll post the finally code once I get it all ironed out as it might be useful to somebody down the line. Thanks for the help.
.NET has a convert class for mostly anything to anything. A couple things to keep in mind though. This is very very very slow, so us it minimally. It also will throw an error if the string isn’t a number, so you need to keep this in mind.
The following will return a float from a string. (Tested in Javascript)
System.Convert.ToSingle(NumericalStringToConvert);
Good question. It got me thinking… Now that Unity (version 2.0) is .NET 2.0 compatible it should have TryParse() on all the data types. So instead of using Convert.ToSingle(), I suggest using TryParse().
float value;
string s = "100.50";
if ( float.TryParse(s, out value) == false )
{
value == 0;
}
Apparently its the fastest way to extract numeric values from strings…
Didn’t think about TryParse. That is probably a better way to go. Easier to handle bad strings.
The simplest way is the built-in parseFloat, but it doesn’t like it if the string has non-numerical characters in it.
–Eric
Yeah, I’m very excited that Unity is using the latest Mono (.Net 2.0). Lots of new things to learn!
float x = 0;
float.TryParse("FOO", out x);
Is all that’s needed – if TryParse fails, then ‘x’ is unchanged. So you can initialize ‘x’ to the default value – and no need to have the added conditional and set it again.
As for the XML – you can use System.Xml classes to work with the Xml, and I believe, if your Xml and your Classes are structured to handle it, you could also use XmlSerialization to speed up the process of converting your XML documents to workable classes – the built-in .net XmlSerializer classes is most likely a good bet, if you can use it (ie; have control over the xml that gets read [you wrote it yourself]). Otherwise, just some simple parsing is needed.
Your descriptions don’t really sound like the parsing of the XML is ‘real time’ and it’s a ‘one time’ thing (or done infrequently) – so you could probably take advantage of Reflection to increase the portability of your overall work so it’s usable in other projects which work with different XML formats and internal classes.
Examples of all this should be found fairly easy with google.com (codeproject.com, msdn, etc)
how did you get the RSS to read in correctly - when i use WWW it always gives me an empty string even when im positive im giving a valid adress