XML Inheritance
This page is mostly theory and will focus on concepts and basic information without putting it into practice.
The purpose of this article is to explain more advanced behaviors and features of XML. You will be able to make your own custom XML documents to practice.
RimWorld's Def inheritance
We use XML to create Defs, and these Defs have a particular behavior of acquiring data from other Defs. This is useful to avoid redundancy and copypasting values all over the place. Analogy: Instead of giving everyone a single portion of salad, you can have a single big bowl of salad at the center of the table, so everyone can take from it.
How to inherit
In RimWorld's case, the keywords Name
and ParentName
are used as attributes, this allows nodes to make a hierarchical relationship where information from the node with Name
is passed to the one with ParentName
.
Do note that keywords, as many other words, are case-sensitive.
Let's see an example:
Here we have two Book
s, one has the attribute Name
equal to BaseFantasyBook
, any Book
with a ParentName
equal to that one will copy all of its child nodes, in this case: price
. That way, the Book
"Healthy candies..." has a price of 20, because it is acquiring that data from its parent, the BaseFantasyBook
.
You may be asking..."but wait, what happens to the original book? It doesn't have an author or a title!"
That's because the original book has the Abstract
attribute's value set to "true", that means that its purpose is only to pass its contents to other books, not to create a book for itself. That's why it's called "abstract", it's like a ghost!
You can also override tags from the parent's. Look at the price tag:
In this case, the price of the child's 30 is overriding and replacing the parent's price of 20.
Though, this doesn't happen with lists, let's see an example of that.
Inheritance in lists
In this case, the second book will have chapters one
,two
,three
, and four
. Why? well, that's just how it works.
If you want to prevent that behavior and only have four
, you need to stop the second book's chapters
from inheriting from the first, abstract book. To do that you just need to set the attribute value of Inherit
to False, right in the chapters' node.
This way, chapters will not inherit anything, while the rest of the nodes still do.
You may be asking yourself what does li
mean, it's a short term for "list item", it's what is used for most lists when we work with XML.