0

I am programming an RPG, and I want the Character object to create objects that control their "powers"

Each charcter has a String variable powerOne, PowerTwo etc. Ex. powerOne = "Attack"

I need a way to do this:

 "powerOne" powerOne = new "powerOne"

(Need the computer to use string in place of the actual power because they are replaced once characters learn new ones.)

That way, I can always reference the power in the rest of the code as powerOne. Ex. powerOne.dealDamage()

2
  • Your design is wrong. Using Interfaces you can accomplish the same thing. So learn 'Interfaces' and 'Abstraction'. Commented Jan 7, 2014 at 22:57
  • I don't understand what you need. Commented Jan 7, 2014 at 22:58

3 Answers 3

1

String powerOne = new String("");

when they learn their new 'power',

powerOne = "Attack".

Although you're rather unclear as to what powerOne is. Is it a String or it's own class poweredOne? If it is a class, why not make a setName function or something along those lines! although I'd suggest making a Power class in this case. Try to make your code look something like this...

Power powerOne = new Power("Attack!",10); // assuming you construct it with the String //name, and int Damage.

Feel free to ask any questions I'm more than happy to help.

Sign up to request clarification or add additional context in comments.

1 Comment

A character can learn a max of four powers by replacing weaker ones (a la pokemon). I want the game to create an object based on the premade classes of powers/abilities by using the string variable instead of an actual predefined name. The variable will be read off of a file at the object's creation, and then create the power objects for use in battle. The only way I know to do it is by makeing an interface that has if statements that check the string variable, but that would be really inefficient...
0

Sounds like your 'powers' are objects, not strings.

Make an interface:

public interface Power {
    void dealDamage();
}

Then make all your individual power classes such as Attack, etc implement the Power interface.

Then in your Character, you can have a List of objects that derive from the Power interface.

2 Comments

Each seperate power is a class. I want to create an object, but since the characters are constantly replacing their known or equipped powers, there is no way to create an efficient method unless it checks every single power in the game compared to your string... that's what I'm trying to avoid.
You could also use a hash table to map the name of the power against the power object's instance. Then you can add or remove powers from the map easily, and also find the appropriate power object's instance quickly when you want to call dealDamage(); You can also get a list of the keys from the map if you want to know what powers a character has.
0

You should use an interface called CharacterPower containing a method called dealDamage into it.

Yould could keep the name of the power into a String variable and then query a CharacterPowerFactory that would get you the right instance of CharacterPower using its name.

Someting like:

String powerOne = "Attack";

CharacterPower power = CharacterPowerFactory.getInstance().createPower(powerOne);
power.dealDamage();


/**
 * Your factory creating powers based on names.
 */
public class CharacterPowerFactory {
    private static final CharacterPowerFactory SELF = new CharacterPowerFactory();
    private CharacterPowerFactory(){}

    public static CharacterPowerFactory getInstance() {return SELF;}

    public CharacterPower createPower(String powerName) {
        if("Attack".compareTo(powerName) == 0) {
            return new AttackPower();
        }

        return null;
    }
}

/**
 * Your Attack power implementation
 */
public class AttackPower implements CharacterPower {
    public void dealDamage(){
        // Dealing the damage
    }
}

/**
 * Your interface definition
 */
public interface CharacterPower {
    void dealDamage();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.