MZ (solved) I assign an attribute to Game_Picture... aaaaand it's gone

I want to write a small plugin to check if the mouse is hovering over a picture. That's my code so far:

Code:
const alias_SpritePicture_update = Sprite_Picture.prototype.update;
Sprite_Picture.prototype.update = function() {
    alias_SpritePicture_update.call(this);
    const picture = this.picture();
    if (picture) {
        picture._hovered = this.isBeingTouched();
    }
}

Game_Picture.prototype.isBeingTouched = function() {
    return !!this._hovered;
}

In the debugger, when I inspect the Game_Picture object, I can see that _hovered is set correctly. However, when I access it, regardless whether $gameScreen.picture(i)._hovered or ...isBeingTouched(), it's always false or undefined.

I really don't get it.

1655313990349.png
Hi there!

I didn't understand why you are checking the _hovered with "!!". You are not initializing this property on Game_Picture? If not, since it is a boolean property, maybe it is better if you initialize that property on the Game_Picture initializes?

JavaScript:
// Alias the function
Game_Picture.prototype.initialize = function() {
    // call Alias
    this._hovered = false
};

//Alias the function
Game_Picture.prototype.show = function(name, origin, x, y, scaleX, scaleY, opacity, blendMode) {
    //call alias
    this._hovered = false
};

Or maybe another plugin is overwriting it?

Besides that, instead of doing the above, you can use the Sprite_Picture methods to check for mouse/touch inputs/hovers over the picture(Inherited from the Sprite_Clickable).

On the update function, you can check how it works:

JavaScript:
Sprite_Clickable.prototype.processTouch = function() {
    if (this.isClickEnabled()) {
        if (this.isBeingTouched()) {
            if (!this._hovered && TouchInput.isHovered()) {
                this._hovered = true;
                this.onMouseEnter();
            }
            if (TouchInput.isTriggered()) {
                this._pressed = true;
                this.onPress();
            }
        } else {
            if (this._hovered) {
                this.onMouseExit();
            }
            this._pressed = false;
            this._hovered = false;
        }
        if (this._pressed && TouchInput.isReleased()) {
            this._pressed = false;
            this.onClick();
        }
    } else {
        this._pressed = false;
        this._hovered = false;
    }
};

So, maybe aliasing the onMouseEnter function, checks for the picture, and set the hovered to true there:

JavaScript:
// Alias the function
Sprite_Picrure.prototype.onMouseEnter = function() {
    // call alias
    if(this.picture()){
        this.picture()._hovered = true
    }
};

// Alias the function
Sprite_Picrure.prototype.onMouseExit = function() {
    // call alias
    if(this.picture()){
        this.picture()._hovered = false
    }
};

Or are you trying to do something different that you need to not rely on the default methods?
I didn't understand why you are checking the _hovered with "!!"
Oh, that's just my personal preference.

If not, since it is a boolean property, maybe it is better if you initialize that property on the Game_Picture initializes?
I did that, didn't change anything.

Besides that, instead of doing the above, you can use the Sprite_Picture methods to check for mouse/touch inputs/hovers over the picture(Inherited from the Sprite_Clickable)
Yup, this is what I'm doing. I'm calling Sprite_Picture.isBeingTouched() and copying the result to Game_Picture. At least, I'm trying.

I still don't understand, why my debugger returns false/undefined when I access $gameScreen.picture(1)._hovered.


EDIT:
Okay, I solved it. I just cannot say by 100% how. What I can say, I call "show Picture" every 10 frames to make an animation, and I think that this somehow confused my plugin. Still don't know why the debugger showed me "false" when it was literally true, but adding a few wait commands with 1 frame solved it for me.

This is my code to test it.

Code:
for (let i = 0; i <= 50; i++) {
  const picture = $gameScreen.picture(i);
  if (picture) {
    console.log(picture); // logs picture with attribute _hovered to true
    console.log(picture._hovered); // logs false
  }

}

1655323436894.png

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-i-assign-an-attribute-to-game_picture-aaaaand-its-gone.148571/