bound function question?

Isabella Ava

Veteran
Veteran
Joined
Sep 13, 2016
Messages
635
Reaction score
756
First Language
English
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 = |
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
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;
            }
        }, []);
    };
};
 

LTN Games

Indie Studio
Veteran
Joined
Jun 25, 2015
Messages
704
Reaction score
631
First Language
English
Primarily Uses
RMMV

Isabella Ava

Veteran
Veteran
Joined
Sep 13, 2016
Messages
635
Reaction score
756
First Language
English
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.i****()) {
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? )
 

LTN Games

Indie Studio
Veteran
Joined
Jun 25, 2015
Messages
704
Reaction score
631
First Language
English
Primarily Uses
RMMV
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!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,867
Messages
1,017,062
Members
137,575
Latest member
akekaphol101
Top