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

Question How do games use an XML document to create items?

Discussion in 'Scripting' started by Kaivian, May 14, 2020.

  1. Kaivian

    Kaivian

    Joined:
    Jan 18, 2018
    Posts:
    35
    So I'm familiar with the concepts of serialization, scriptable objects and XML. What I'm wondering is how some games I've played (with xml documents) allow whole new items (and other objects) to be created from the XML. Like, 7 days to die (unity engine), has XML like this:


    Code (CSharp):
    1.  
    2. <item id="7" name="clubIron">
    3.     <property name="Extends" value="clubMaster" />
    4.     <property name="Meshfile" value="Items/Weapons/Melee/Club/Club_Wood_RefinedPrefab" />
    5.     <property name="Material" value="metal" />
    6.     <property name="RepairTools" value="scrapIron" />
    7.     <property class="Action0">
    8.         <property name="Buff" value="criticalBlunt" />
    9.         <property name="Buff_chance" value=".2" />
    10.     </property>
    11.     <property class="Attributes">
    12.         <property name="EntityDamage" value="9,16" />
    13.         <property name="BlockDamage" value="3,13" />
    14.         <property name="DegradationMax" value="100,600" />
    15.         <property name="DamageBonus.head" value="4" />
    16.     </property>
    17.     <property name="CritChance" value=".2" />
    18.     <property name="Group" value="Ammo/Weapons" />
    19.     <property name="DescriptionKey" value="clubIronDesc"/>
    20. </item>

    Which you could change to make your own custom "club" item with item id=(something unused).

    So I understand how to read in these values, but then that I have them, how do I "create" a new "prefab" (or whatever it is), so that this item can be instantiated into the game world?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    For starters, it's much more common in games to use JSON. It's smaller, easier to write, easier to read (IMO), more performant to read in games, and probably most importantly, easier to slap JSON-loaded data onto a data-holding class using tools like JsonUtility, LitJson, or Json.NET (my favorite). Once the data is in that class, it's really easy to loop through it and spawn GameObjects, add components, etc.
     
    Joe-Censored and Kurt-Dekker like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    They probably start with a base prefab, apply specified settings to existing components always on the base prefab, and add additional components and/or child GameObjects as needed. The Club_Wood_RefinedPrefab line implies a Resources.Load call, or similar loading mechanism.
     
    Kurt-Dekker likes this.