0

I'm very new to JavaScript, and programming in general. This question is in the context of an RPG. I have a character object in a combat function, trying to call an ability function based on a) predefined character class, and b) input:

// player example
var player = {
  this.pClass = 0; // denotes character class; could also be 1 or 2
}  

// ability list
var ability = {
  // first character class
  0: {
    1: function() {some action;},
    2: function() {some other action;}
  },
  // second character class
  1: {
  // same deal here
  }
};

var input = prompt("Enter an action number:");

I would like to be able to call the action by passing the player class and selected number, something like:

ability.{player.pClass}.{input}();

Am I approaching this in the right way, at all? I'd prefer not to iterate if...else if statements for every option, if possible. Thanks for the assistance.

3
  • I recommend you start learning JavaScript here jqfundamentals.com/chapter/javascript-basics. You seem to have some misunderstanding of basic syntax and concepts... Commented Apr 27, 2013 at 2:24
  • @elclanrs: No doubt of that. This project is mostly to teach myself JavaScript. Thanks for the reference. Commented Apr 27, 2013 at 2:26
  • Yeah, I wish I had that link when I first started xD. Commented Apr 27, 2013 at 2:42

2 Answers 2

1

You can access an object's properties by using square brackets:

var abilities = {
    fireball: function() { ... }
};

abilities["fireball"];
abilities.fireball; // equivalent, but only if the index is a valid js identifier

While you can use numeric indices, I would suggest a more object-oriented way:

// define what a player is and can do
var Player = function(pClass) {
    this.pClass= pClass;
};

Player.prototype.useAbility = function(name) {
    if (this.pClass.abilities[name]) { // make sure this player can use the ability
        this.pClass.abilities[name]();
    }
};

// define what a class is
var PClass = function(name) {
    this.name = name;
    this.abilities = {};
};

// define "mage" class
PClass.mage = new PClass("mage");
PClass.mage.abilities["fireball"] = function() { ... }
PClass.mage.abilities["hex"] = function() { ... }

PClass.warrior = new PClass("warrior");
PClass.warrior.abilities["riposte"] = function() { ... }

// create a new player
var bartTheFearsome = new Player(PClass.mage);
bartTheFearsome.useAbility("fireball");
bartTheFearsome.useAbility(prompt("enter ability name")); // enter "fireball" or "hex"

If you're new to (object oriented) programming, here's a good place to start.

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

2 Comments

Thanks for the reference link. The Mozilla site has been of great help so far. I like your approach for a more text-oriented game, but my method will ultimately be to click a button to select an action, in which case the name of the reference doesn't matter as much. I may use this alternative elsewhere, however. Thanks again.
Names matter primarily for the programmer. 1 week from now, you're not going to remember that "0" means "mage" and "1" means "warrior". PClass.mage.abilities["fireball"] is descriptive. ability[0][1] isn't.
1

Use the square bracket notation to allow dynamic access of property names:

ability[player.pClass][input]();

But your object literal shouldn't have numeric property names. Try giving them real meaningful names instead. Otherwise, if this is what you intended, use an array instead:

var abilities = [function() {..}, function() {..}];

2 Comments

Thanks. I wasn't sure which approach to take, so that help clarify. However, would your initial response work as well? What's the advantage of referencing by name, rather than a numeric list?
@RobRosson It just makes more sense. Your code can work fine with numeric names, but if you're using them on an object literal it's probably a sign that you could be doing so without.

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.