Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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>

and then states in java your mob class would look like:

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 so if it appears like i dont know what im talking about then just point out of edit my stupidity out.

share|improve this question

2 Answers 2

up vote 9 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).

share|improve this answer
    
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 –  TMP 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 –  TMP 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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