Hey a few days ago i decided to post a thread on the forum about Xml and i got some answers but i didn't understand it so i decided to go out and look and then i came to http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer where they had a script that i understood (a little) and i was like this looks great BUT how can i add monsters/Cards(see in the pastebin link)? i want to add Health, ManaCost, Attack and name. the script is http://pastebin.com/w3XJYDh3 Can someone please explain it to me in dummy language? All help is appreciated.
Card is the class representing a single card. If you want to add e.g. Health decide what type it should be e.g. int and then just add a field or property to the class that is public. Code (csharp): class Card { .... public int Health; .... } This will cause the serializer to pick it up correctly. Code (csharp): <Card> <Health>5</Health> </Card> If you add Code (csharp): ... [XmlAttribute("health")] public int Health; the xml has to look like Code (csharp): <Card health="5"> </Card> What of these to styles you prefer is up to you.
You skipped a step. You created a Card Container class, but no Card class. Look at the "Preparing the Monster class" section (in your case that would be your Card class) on the wiki page you used as reference. That's where you add your Health, Mana, etc. just like how they added Name/Health.
ok so i did the first option and created the public class Card { public string Name; public int Health; public int ManaCost; public int Attack; } Now this is the part that i am confused about should i make an = after Name and all them and define the value, or make a new class, and if i have to make a new class how would i write that one?
Think of a class as the blueprint, It will just define what a "Card" should look like. An actual object of this class is called an instance. Only Instances have values (at least for that demonstration). To actually get an instance from a class you would use new in C#. Code (csharp): ... var card1 = new Card(); card1.Name = "MyCard"; card1.Health = 5; ... As you are trying to load things from xml you would rarely need to do this yourself, all your cards will be inside your CardContainer after you loaded the xml. As for filling it, you would just write the cards in xml (The XmlSerializer does all the code I have given you above for you). What exactly are you trying to accomplish with xml? From the current questions asked, might I suggest using GameObjects / Prefabs and a Card Component instead, which is easier in the beginning and also have the advantage of displaying and manipulating it in the editor itself.
Basically any unity tutorial out there. Make Card derive from MonoBehaviour, Code (csharp): class Card : MonoBehaviour { public int Health; public int ManaCost; public int Attack; } add an empty gameobject to a scene, put the Card component on it and store it as a prefab. Now you have an instance of a card that can be named and manipulated in the editor. If you need to reference it just put a variable of type Card into you other components class and link it using the inspector by just dragging the gameobject on it. Code (csharp): public Card CardToUse; As long as you don't need to change cards after the game has been deployed this the easieast way for a beginner to obtain something configurable.
Ohh thx that works but one last question how would i add a special ability to a card would i have to make a new script for each card that do something special?