- Home /
Scripted Data Line Reading
Hello all! I am currently trying to make a scripted data line for each ID number.
I am making a GUI Shop which when the user presses a button they buy the item(which works perfectly) however I am wanting to write all the details about the item in a line which can be read, for example;
so; User Presses Button
if Items.itemID equals 0
then read the line from Items.itemID with the ID of 0.
So it would read ID = 0, Name = "Wood", Price = 10, Texture = WoodIcon
however
if Items.itemID equals 5
then read the line from Items.itemID with the ID of 5.
So it would read ID = 5, Name = "Steel", Price = 50, Texture = SteelIcon
So the script for the Items would look something along the lines of;
ID = 0, Name = "Wood", Price = 10, Texture = WoodIcon;
ID = 1, Name = "Iron", Price = 15, Texture = IronIcon;
ID = 2, Name = "Gold", Price = 18, Texture = GoldIcon;
ID = 3, Name = "Power", Price = 5, Texture = PowerIcon;
ID = 4, Name = "Vile", Price = 2, Texture = VileIcon;
ID = 5, Name = "Steel", Price = 50, Texture = SteelIcon;
So this makes the script/File easy to looks down and and add new items. So my question is, what is the best way to approach this? The If statement to determine the ID of the Button I know do with a simple Switch statement. If it means reading from an XML file or Database that can work too. how do I write the lines and allow unity to read them? thank you!
sorry I don't know what that is? Do you mean a GUI window?
Yeah. You can make one to edit this stuff using Unity's GUI x_x.
Thank you! that does look really like what I wanted; however with a quick scan of it It doesn't say how I can link each line up to the ID, for example if ID = 0 then price is this etc? any help?
Answer by hathol · Aug 09, 2012 at 12:10 PM
We are using a simple xml file to store some of our configuration data. Feel free to use the code below. GetItemByID returns an xml node that has an attribute "id" with the respective value. You can then use returnedXmlNode.Attributes["yourattribute"].Value to get price etc. out of it.
XmlDocument m_xmldata;
private void Initialize()
{
// load the config file into the XmlDocument
TextAsset textAsset = (TextAsset) Resources.Load("ItemData");
m_xmldata = new XmlDocument ();
m_xmldata.LoadXml ( textAsset.text );
}
public XmlNode GetItemByID(string p_id)
{
foreach(XmlNode node in m_xmldata.ChildNodes)
{
XmlNode currentnode = SearchChildNodesForID(node, p_id);
if(currentnode != null)
return currentnode;
}
return null;
}
private XmlNode SearchChildNodesForID(XmlNode p_node, string p_id)
{
if(p_node.Attributes != null && p_node.Attributes["id"] != null)
if(p_node.Attributes["id"].Value == p_id)
return p_node;
if(p_node.HasChildNodes)
{
foreach(XmlNode node in p_node.ChildNodes)
{
XmlNode currentnode = SearchChildNodesForID(node, p_id);
if(currentnode != null)
return currentnode;
}
}
return null;
}
Quick note: There is an XmlDocument.GetElementById(), but that requires you to have a DTS schema defined and unity doesn't play along with that (at least it didn't for me)
sry, my bad ... forgot the includes :) You need to add "using System.Xml;" on top
ItemData is an xml file, so basically a standard xml layout. For you it could look something like
<Item>
<Item id="0" Name = "Wood" Price = "10" Texture = "WoodIcon" />
<Item id="1" Name = "Wood2" Price = "10" Texture = "WoodIcon" />
<Item id="2" Name = "Wood3" Price = "10" Texture = "WoodIcon" />
<Item id="3" Name = "Wood4" Price = "10" Texture = "WoodIcon" />
</Item>
sry, I was too lazy to copy all the data ;)
No problem :) Put the xml into your resources folder (simply a folder in your assets calles "Resources"), call Initialize (probably in the start function) and then whenever you need data for an i$$anonymous$$m, call GetItemByID with the respective item id(remember that you will need to include System.Xml to work with the returned xml node)
Ah, sorry :) Would be:
XmlNode dataNode = GetItemByID("0");
string name = dataNode.Atributes["Name"].Value;
string price = dataNode.Attributes["Price"].Value;
string texture = dataNode.Attributes["WoodIcon"].Value;
Here's more info about the XmlNode class: http://msdn.microsoft.com/en-gb/library/system.xml.xmlnode(v=vs.100).aspx ... $$anonymous$$SDN is your best friend when it comes to c# ;)
Oh, and in case you need it: System.Convert helps you with converting things from string to int, float etc.