bound function question?

● ARCHIVED · READ-ONLY
Started by Isabella Ava 6 posts View original ↗
  1. Hi there.
    There is something i don't understand with .bind in this trunk of code, could someone plz explain it = |
    Game_Enemy.prototype.makeDropItems = function() {
    if (this._treasure.checked) {
    return this._treasure.item;
    } else {
    return this.enemy().dropItems.reduce(function(r, di) {
    if (di.kind > 0 && Math.random() * di.denominator < this.dropItemRate()) {
    return r.concat(this.itemObject(di.kind, di.dataId));
    } else {
    return r;
    }
    }.bind(this), []);
    };
    };
    Why do we put .bind here? I see no meaning of doing that in this code = |
  2. because you use this in anonymous function, so the default scope are window.
    look the code for: this.itemObject . It inside a anonymous function.

    you can try this if you dont like the bind,
    it should work , but i dont know the code source.

    PHP:
    Game_Enemy.prototype.makeDropItems = function() {
        let that = this;
        if (this._treasure.checked) {
            return this._treasure.item;
        } else {
            return this.enemy().dropItems.reduce(function(r, di) {
                if (di.kind > 0 && Math.random() * di.denominator < that.dropItemRate()) {
                    return r.concat(that.itemObject(di.kind, di.dataId));
                } else {
                    return r;
                }
            }, []);
        };
    };
  3. Thanks @Jonforum for your answer
    Thanks @LTN Games - wow, thanks now you explained it. It's such a great way to use .bind function, never thought of it =O

    By this chance that two of you pro coders are here, i want to ask another question:
    How can i invoke a function that belong to another object within an object?
    for example, i want to invoke this:
    Sprite_Enemy.prototype.checkTreasurePopup = function() {
    this._enemy._treasure.item = this._enemy.makeDropItems();
    this._enemy._treasure.checked = true;
    if (this._enemy._treasure.item) {
    this._enemy._treasure.needPopup = true;
    $gameTemp._trBatNeedPopUp = true;
    };
    };

    within this:
    Game_Action.prototype.apply = function(target) {
    var result = target.result();
    this.subject().clearResult();
    result.clear();
    result.used = this.testApply(target);
    result.missed = (result.used && Math.random() >= this.itemHit(target));
    result.evaded = (!result.missed && Math.random() < this.itemEva(target));
    result.physical = this.isPhysical();
    result.drain = this.isDrain();
    if (result.isHit()) {
    if (this.item().damage.type > 0) {
    result.critical = (Math.random() < this.itemCri(target));
    var value = this.makeDamageValue(target, result.critical);
    this.executeDamage(target, value);
    }
    this.item().effects.forEach(function(effect) {
    this.applyItemEffect(target, effect);
    }, this);
    this.applyItemUserEffect(target);
    }
    };

    a simple .call won't able to do this. What would you do to invoke
    checkTreasurePopup function within Game_Action.prototype.apply (or send
    an argument from Game_Action.prototype.apply to Sprite_Enemy.prototype.checkTreasurePopup function
    to make it invoke itself, perhaps? )
  4. Not as easy as .call you're right about that. Now the issue is more or less what is what. this._enemy referencing? my guess is it's referencing a `Game_Enemy` or a `Game_Actor` in which case both inherit from `Game_BattlerBase`. If this is the case, then the best bet would be to place that function as a method of Game_BattlerBase. Sometimes this is not easy either because other methods may rely on that specific method.

    It's not easy to figure this out without seeing all the code but give this a try. Create a new function in Game_BattlerBase or if you know the exact class this._enemy is referencing then you may create the function in that class.

    PHP:
    Game_BattlerBase.prototype.checkTreasurePopup = function() {
      this._treasure.item = this.makeDropItems();
      this._treasure.checked = true;
      if (this._treasure.item) {
        this._treasure.needPopup = true;
        $gameTemp._trBatNeedPopUp = true;
      };
    };

    Now form what I remember Game_Action.prototype.apply = function(target) the target is actually either a Game_Actor or a Game_Enemy, either way both inherit from Game_BattlerBase as I've already mentioned so this should work but I can't be sure as I'm unsure what's what without seeing all the code.

    Then in your apply() method you can use target, because target will have access to the method inside Game_BattlerBase
    PHP:
    Game_Action.prototype.apply = function (target) {
      target.checkTreasurePopup()
    }

    Hopefully this helps you understand things a bit more and maybe you can figure out your own solution with the info. Cheers!
  5. Thanks @LTN Games That helped me a lots = )