Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Xml help.

Discussion in 'Scripting' started by ThatExGuy, Jan 16, 2014.

  1. ThatExGuy

    ThatExGuy

    Joined:
    Jan 12, 2014
    Posts:
    11
    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.
     
    Last edited: Jan 16, 2014
  2. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    329
    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):
    1.  
    2. class Card
    3. {
    4. ....
    5.     public int Health;
    6. ....
    7. }
    8.  
    This will cause the serializer to pick it up correctly.
    Code (csharp):
    1.  
    2. <Card>
    3. <Health>5</Health>
    4. </Card>
    5.  
    If you add
    Code (csharp):
    1.  
    2. ...
    3.  [XmlAttribute("health")]
    4. public int Health;
    5.  
    the xml has to look like
    Code (csharp):
    1.  
    2. <Card health="5">
    3. </Card>
    4.  
    What of these to styles you prefer is up to you.
     
  3. Brotherhood

    Brotherhood

    Joined:
    Dec 8, 2012
    Posts:
    73
    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.
     
  4. ThatExGuy

    ThatExGuy

    Joined:
    Jan 12, 2014
    Posts:
    11
    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?
     
  5. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    329
    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):
    1.  
    2. ...
    3. var card1 = new Card();
    4. card1.Name = "MyCard";
    5. card1.Health = 5;
    6. ...
    7.  
    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.
     
  6. ThatExGuy

    ThatExGuy

    Joined:
    Jan 12, 2014
    Posts:
    11
    Ok thanks can you link me a tutorial for the easy option?
     
  7. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    329
    Basically any unity tutorial out there.

    Make Card derive from MonoBehaviour,

    Code (csharp):
    1.  
    2. class Card : MonoBehaviour
    3. {
    4.    public int Health;
    5.    public int ManaCost;
    6.    public int Attack;
    7. }
    8.  
    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):
    1.  
    2. public Card CardToUse;
    3.  
    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.
     
  8. ThatExGuy

    ThatExGuy

    Joined:
    Jan 12, 2014
    Posts:
    11
    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?