0
\$\begingroup\$

This question already has an answer here:

I'm toying with the idea of a character based game. The characters, robots, monsters, heroes (and weapons I suppose) will all have various attributes as per a D&D game for example.

They player should be able to configure those attributes to personalize their character, so maybe they'll be given 100 points at the start to allocate how they see fit to things like strength, speed, etc...

Also, I'd like the player to be able to add additional options, like maybe shields or individual spells, things like that.

Is there a standard design pattern for things like that?

I'm hacking my way through a prototype right now, and I've been considering an Options object that accepts an Option in the constructor, with methods addOption() and getOption.

For my implementation of Option I did the following:

class Option{

  final String optionKey;
  String optionString;
  int optionInt;
  long optionLong;
  float optionFloat;
  char optionType;

  public Option(String optionKey, Object option){

    this.optionKey = optionKey;

    if(option instanceOf String){

      optionString = (String) option;
      optionType = 's';
  }else if(option instanceOf int){

    optionInt = (int) option;
    optionType = 'i';
    }else if(option instanceOf long){

    optionLong = (long) option;
    optionType = 'l';
    }else if(option instanceOf float){

    optionFloat = (float) option;
    optionType = 'f';
    }


  }

public String getOptionKey(){
  return optionKey;
}

public String getOptionValue(){
  return optionValue;
}

public void setOptionValue(String optionValue){
  this.optionValue = optionValue;
}

public void getOptionType(){
  return optionType;
}

}

But now I'm thinking this is a bit much, and perhaps I should be using an options class that encapsulates a Hashmap instead. So maybe something like this:

class Options{

HashMap<String, Object> options;


public Options(String key, Object value){
    options = new HashMap<String, Object>();
    options.put(key, value);
}

public void putOption(String key, Object value){

    options.put(key, value);
}

public void getStringOption(String key){

    return (String) options.get(key);
}

public void getFloatOption(String key){

    return (float) options.get(key);
}

//....... and so on...

}

I'm thinking the version directly above is better, is there a standard for this type of thing?

\$\endgroup\$

marked as duplicate by Josh Dec 22 '14 at 19:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ You're trying something like Component which many game engine do. check out this link. \$\endgroup\$ – Emadpres Dec 21 '14 at 13:31
  • \$\begingroup\$ Before you code away on the details of a flexible attributes system, make sure you have a good idea of the requirements for your game. Developing a game from the software is perhaps not so productive. For example, if you only need to support two attributes (strength and speed), you don't need a generic system. Do your game requirements necessitate a complicated design? \$\endgroup\$ – Fuhrmanator Dec 22 '14 at 15:15
  • \$\begingroup\$ @Fuhrmanator Well I think so. It's my intention that there should be loads of attributes the player can modify. And I'd like the player to be able to improve characteristics of some of the weapons too. So there would be minimum fixed attributes to begin with. But the I was thinking if they buy some sort of extension, like improved speed, or add something totally new, like flying ability it'd be a nice way to store and access them. \$\endgroup\$ – bot_bot Dec 22 '14 at 15:41