# Best practice for organizing/storing character/monster data in an RPG?

Synopsis: Attempting to build a cross-platform RPG app in Adobe Flash Builder and am trying to figure out the best class hierarchy and the best way to store the static data used to build each of the individual "hero" and "monster" types. My programming experience, particularly in AS3, is embarrassingly small.

My ultra-alpha method is to include a "_class" object in the constructor for each instance. The _class, in turn, is a static Object pulled from a class created specifically for that purpose, so things look something like this:

// Character.as
package
{
public class Character extends Sprite
{
public var _strength:int;
// etc.
public function Character(_class:Object)
{
_strength = _class._strength;
// etc.
}
}
}

// MonsterClasses.as
package
{
public final class MonsterClasses extends Object
{
public static const Monster1:Object={
_strength:50,
// etc.
}
// etc.
}
}

// Some other class in which characters/monsters are created.
// Create a new instance of Character
var myMonster = new Character(MonsterClasses.Monster1);


Another option I've toyed with is the idea of making each character class/monster type its own subclass of Character, but I'm not sure if it would be efficient or even make sense considering that these classes would only be used to store variables and would add no new methods. On the other hand, it would make creating instances as simple as var myMonster = new Monster1; and potentially cut down on the overhead of having to read a class containing the data for, at a conservative preliminary estimate, over 150 monsters just to fish out the one monster I want (assuming, and I really have no idea, that such a thing might cause any kind of slowdown in execution).

But long story short, I want a system that's both efficient at compile time and easy to work with during coding. Should I stick with what I've got or try a different method?

As a subquestion, I'm also assuming here that the best way to store data that will be bundled with the final game and not read externally is simply to declare everything in AS3. Seems to me that if I used, say, XML or JSON I'd have to use the associated AS3 classes and methods to pull in the data, parse it, and convert it to AS3 object(s) anyway, so it would be inefficient. Right?

As a subquestion, I'm also assuming here that the best way to store data that will be bundled with the final game and not read externally is simply to declare everything in AS3. Seems to me that if I used, say, XML or JSON I'd have to use the associated AS3 classes and methods to pull in the data, parse it, and convert it to AS3 object(s) anyway, so it would be inefficient. Right?

I suppose you could do that, but doing that would be a hassle to change the numbers, especially for a development team of anything more than 1. As in, if you are the only developer and are sure there will never be any other developers then I guess hard-coding all the values would work out okay. But that's generally a bad coding practice because it's more cleanly organized to keep code and data separate. That helps even if it's just you, but is pretty imperative if there is anyone other than you who will be working on the game.

As for the overall question, I would code your monsters as a class that operates on whatever strength etc. you pass in and then read in a JSON file where the values for the different monsters is defined. Then instantiating a specific monster would look like var monster = new Character("monster3") or maybe var monster = new Character(data) where the JSON would be something like:

{
"id": "monster3",
"image": "demon.jpg",
"strength": 50
}


Basically, I'm recommending your first approach but with the data defined in an external data file instead of hard-coded as a class.

• Thank you. With this advice I think I'll go with some schema-constrained XML. Dec 17 '12 at 20:11
• After some experimentation, this approach is actually creating a lot of problems for me. I'll update the question with the issues. Jan 3 '13 at 17:29
• ok I await your edits, I'm very curious what your problems could be. Jan 3 '13 at 17:53
• I'm reverting the question and reaccepting your answer for now. I didn't realize you could embed XML documents directly instead of using URLLoader, which was creating a whole new set of problems. I still need to read up on embedding, but I'll only reopen this question again if I hit another stumbling block I just can't get past. Jan 3 '13 at 18:34
• Well using URLLoader is better than embedding for flexibility in others editing the values (half the reason to do it that way) but yeah that's a separate question, best asked on the main StackOverflow since that isn't specific to games. Jan 3 '13 at 20:29