Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: updated example code

...

Code Block
themeEclipse
languagejavascript
titleAdd .isAlly, .isEnemy and .isMe properties to player data objects
linenumberstrue
var primaryTarget = null;
for (var p=0; p<playerData.length; p++) {
  // Add a read-only .id property that returns the player number
  playerData[p].uAddProperty(
    DATA_READONLY,
    "id",
    p
  );
  // Add a read-only .isAlly property that returns true if this player is an ally
  // Note: No setter function (fSet) means read only
  playerData[p].uAddProperty(
    ACCESSOR_NORMAL,
    "isAlly",
    allianceExistsBetween.uCurry(me,p)
  );
  // Add a read-only .isMe property that returns true if this player is me
  playerData[p].uAddProperty(
    ACCESSOR_NORMAL,
    "isMe",
    function() {
      return (this.id==me);
    }
  );
  // Add a read-only .isEnemy property that returns true if this player is an enemy
  playerData[p].uAddProperty(
    ACCESSOR_NORMAL,
    "isEnemy",
    function() {
      return (!(this.isAlly || this.isMe));
    }
  );
  // Add vara player = p;read-write .isPrimaryTarget property
  playerData[p].uAddProperty(
    ACCESSOR_NORMAL,
    "isMeisPrimaryTarget",
    function() {
      return (player==meprimaryTarget == this.id);
    },
    function(bool) {
      primaryTarget = (!!bool) ? this.id : null;
      return (!!bool);
    }
  );
}

See also

  • defineProperty – I based uAddProperty on code examples from this MDN page
  • TypeError – this will be thrown if an existing non-configurable sKey property exists on the object
  • Function.uCurry() – create a new function that's based on an existing function but with some parameters pre-defined