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.

up vote 11 down vote accepted

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).

  • Nice to see I made sense to someone ;) – AbstractChaos Aug 2 '12 at 15:16
  • @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 – Tom Prats Aug 2 '12 at 15:20
  • 2
    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. – Thraka Aug 2 '12 at 15:48
  • 3
    @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. – Laurent Couvidou Aug 2 '12 at 15:54
  • ah i see. it makes sense now. thanks guys – Tom Prats Aug 2 '12 at 16:01

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.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.