5
\$\begingroup\$

In this question, the first answer explains a way of defining components of a gaming engine in XML, like this:

<?xml version="1.0" encoding="UTF-8"?>
<mobs>
  <mob>
    <personality>Aggressive</personality>
    <intelligence>20</intelligence>
  </mob>
</mobs>

Then, states in Java that your mob class would look like this:

public class Mob {
  private IPersonality personality;
  private Integer intelligence

  //**  Getters & Setters **//
}

What exactly is the XML doing, here? I'm not really familiar with XML, at all.

\$\endgroup\$

2 Answers 2

11
\$\begingroup\$

The XML describes one instance of a mob, i.e. the traits of one specific mob (it's personal personality and intelligence).

The Java class describes the general structure of all mobs (their traits: personality and intelligence).

Typically, a game engine would serialize the XML data at runtime to create actual Java Mob objects (= instances of the Mob class).

\$\endgroup\$
4
  • \$\begingroup\$ Nice to see I made sense to someone ;) \$\endgroup\$ Commented Aug 2, 2012 at 15:16
  • \$\begingroup\$ @lorancou what's the benefit of having it serialize it versus actually writing the java yourself? i think that's what i have done in the past because i didn't even know this was an option \$\endgroup\$
    – Tom Prats
    Commented Aug 2, 2012 at 15:20
  • 2
    \$\begingroup\$ Serialization is supposed to be an automated save-"output to file" and load-"read from file" of your class instances. Generally a serialization framework (I'm not a Java programmer) will look at the structure of the class and figure out how to translate it back and forth from the desired file type, in this case XML. So you don't have to write all the code to do read and write on each individual property of the class to and from the XML file. \$\endgroup\$
    – Thraka
    Commented Aug 2, 2012 at 15:48
  • 3
    \$\begingroup\$ @TMP It allows non-programmers to create their own mobs. It also prevents a recompile for just a few tweaks in some mobs properties. In other world, this is a data-driven design. \$\endgroup\$ Commented Aug 2, 2012 at 15:54
6
\$\begingroup\$

Maybe I didn't explain fully,

You implement the loading of the XML to the Class instance ensuring meaning in theory it could just as easily be any data format, json, flat file, Database its how you use the data that decides what it means. The example i provided was instantiating a new instance of IPersonality based on the String within the tags

 public IPersonality getPersonality(String personalityName) {
      if(personalityName.equals("Aggressive")) {
        return new AggressivePersonality();
      }
      else if(personalityName.equals("Passive")) {
        return new PassivePersonality();
      }
      else {
         //Maybe allow for no personality (We all know monster like that ;) )
         return null; 
      }
    }

Here I saying if I find the name aggressive this mob the aggressive personality component.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .