How do games use an XML document to create items?

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:

<item id="7" name="clubIron">
    <property name="Extends" value="clubMaster" />
    <property name="Meshfile" value="Items/Weapons/Melee/Club/Club_Wood_RefinedPrefab" />
    <property name="Material" value="metal" />
    <property name="RepairTools" value="scrapIron" />
    <property class="Action0">
        <property name="Buff" value="criticalBlunt" />
        <property name="Buff_chance" value=".2" />
    </property>
    <property class="Attributes">
        <property name="EntityDamage" value="9,16" />
        <property name="BlockDamage" value="3,13" />
        <property name="DegradationMax" value="100,600" />
        <property name="DamageBonus.head" value="4" />
    </property>
    <property name="CritChance" value=".2" />
    <property name="Group" value="Ammo/Weapons" />
    <property name="DescriptionKey" value="clubIronDesc"/>
</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?

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.

2 Likes

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.

1 Like