_Soysauce_

Villager
Member
Joined
Sep 12, 2018
Messages
25
Reaction score
6
First Language
English
Primarily Uses
RMMZ
Hi,

I've been using the ButtonPicture plugin that Rpg Maker MZ comes bundled with and by itself, it worked nice.
For those who aren't aware of it, it's a short plugin that allows for plugin calls that will make clicking on a picture run a Common Event.

Then later on I found @Artille 's ButtonPicture.js v1.01, which expands on it by adding the ability to pass a variable ID and value to the Commont Event of your liking.

Then later on I decided to expand on it myself - first off, by letting you choose if you want to simply run a Common Event (as the original plugin did) OR run it and pass values (so Artille's 1.01 version) - as you sometimes might prefer one option over the other. But then, I also wanted to add the ability to call an event on the map itself - and again, have the option to simply run it OR run and pass a variable with a value.

At first it seemed to work fine, as by checking both Rpg Maker's F9 variables debug and Java's console it seemed to correctly assign the variables. But no, it's not working at all - and I believe this might extend to Artille's version too. While it's fine on the surface (even when printing variables during messages), it completely fails any IF check that runs in any event, be it Common or in a map. It DOES seem to assign something, as it wouldn't recognize the variable as "0" anymore, but it also won't assign the specified variable either. I really have no idea what's causing this behaviour.

I've copied the code in the spoiler tag down below in the hope that someone can spot what's haywiring Rpg Maker MZ here - also I'm sorry if it turns out the answer is extremely simplistic - while I did my best to investigate this and asked around, I am far from tech-savvy when it comes to Javascript, and I know next to zero about the internals of Rpg Maker.


JavaScript:
//=============================================================================
// RPG Maker MZ - Button Picture
//=============================================================================

/*:
 * @target MZ
 * @plugindesc Makes a picture clickable & Adds variable control.
 * @author Yoji Ojima
 *
 * @help ButtonPicture.js
 *
 * This plugin provides a command to call a common event when a picture is
 * clicked.
 *
 * Use it in the following procedure.
 *   1. Execute "Show Picture" to display your button image.
 *   2. Call the plugin command "Set Button Picture".
 *   3. Set Variable ID and value to assign to said Picture ID.
 *


 * @command commonevent
 * @text Common event
 * @desc Calls common event.
 *
 * @arg pictureId
 * @type number
 * @min 1
 * @max 100
 * @default 1
 * @text Picture Number
 * @desc Control number of the picture.
 *
 * @arg commonEventId
 * @type common_event
 * @default 1
 * @text Common Event
 * @desc Common event to call when the picture is clicked.
 

 * @command commoneventargs
 * @text Common event with args
 * @desc Calls common event while passing additional arguments.
 *
 * @arg pictureId
 * @type number
 * @min 1
 * @max 100
 * @default 1
 * @text Picture Number
 * @desc Control number of the picture.
 *
 * @arg commonEventId
 * @type common_event
 * @default 1
 * @text Common Event
 * @desc Common event to call when the picture is clicked.
 *
 * @arg VariableId
 * @type variable
 * @default 1
 * @text Variable ID
 * @desc Variable used to attribute a value to the comment event call.
 *
 * @arg VariableValue
 * @type text
 * @default 1
 * @text Variable Value
 * @desc Value given to determine how the common event should react.
 
 
 * @command roomevent
 * @text Map event
 * @desc Calls event on the current map
 *
 * @arg pictureId
 * @type number
 * @min 1
 * @max 100
 * @default 1
 * @text Picture Number
 * @desc Control number of the picture.
 *
 * @arg roomEventId
 * @type number
 * @default 1
 * @text Map Event ID
 * @desc Map event to call when the picture is clicked.
 
 
 * @command roomeventargs
 * @text Map event with args
 * @desc Calls event on the current map while passing additional arguments.
 *
 * @arg pictureId
 * @type number
 * @min 1
 * @max 100
 * @default 1
 * @text Picture Number
 * @desc Control number of the picture.
 *
 * @arg roomEventId
 * @type number
 * @default 1
 * @text Map Event ID
 * @desc Map event to call when the picture is clicked.
 *
 * @arg VariableId
 * @type variable
 * @default 1
 * @text Variable ID
 * @desc Variable used to attribute a value to the map event call.
 *
 * @arg VariableValue
 * @type text
 * @default 1
 * @text Variable Value
 * @desc Value given to determine how the event should react.
 *
 
 */


(() => {
    'use strict';
    
    const pluginName = "ButtonPicture";

    PluginManager.registerCommand(pluginName, "commoneventargs", args => {
        const pictureId = Number(args.pictureId);
        const commonEventId = Number(args.commonEventId);
        const variableId = Number(args.VariableId);
        const variableValue = args.VariableValue;
        const picture = $gameScreen.picture(pictureId);
        if (picture) {
            picture.mzkp_commonEventId = commonEventId;
            picture.mzkp_variableId = variableId;
            picture.mzkp_variableValue = variableValue;}
    });
    
    
    PluginManager.registerCommand(pluginName, "commonevent", args => {
        const pictureId = Number(args.pictureId);
        const commonEventId = Number(args.commonEventId);
        const picture = $gameScreen.picture(pictureId);
        if (picture) {
            picture.mzkp_commonEventId = commonEventId;}
    });
    
    
    
    PluginManager.registerCommand(pluginName, "roomeventargs", args => {
        const pictureId = Number(args.pictureId);
        const roomEventId = Number(args.roomEventId);
        const variableId = Number(args.VariableId);
        const variableValue = args.VariableValue;
        const picture = $gameScreen.picture(pictureId);
        if (picture) {
            picture.mzkp_roomEventId = roomEventId;
            picture.mzkp_variableId = variableId;
            picture.mzkp_variableValue = variableValue;
        }
    });
    
    
    PluginManager.registerCommand(pluginName, "roomevent", args => {
        const pictureId = Number(args.pictureId);
        const roomEventId = Number(args.roomEventId);
        const picture = $gameScreen.picture(pictureId);
        if (picture) {
            picture.mzkp_roomEventId = roomEventId;
        }
    });
    
    

    Sprite_Picture.prototype.isClickEnabled = function() {
        const picture = this.picture();
        return picture && !$gameMessage.isBusy();
    };

    Sprite_Picture.prototype.onClick = function() {
        if(this.picture().mzkp_roomEventId)
        {
            if(this.picture().mzkp_variableId)
                $gameVariables.setValue(this.picture().mzkp_variableId,this.picture().mzkp_variableValue);
            $gameMap.event(this.picture().mzkp_roomEventId).start();
            
        }
        else
        {
            $gameTemp.reserveCommonEvent(this.picture().mzkp_commonEventId);
            if(this.picture().mzkp_variableId)
                $gameVariables.setValue(this.picture().mzkp_variableId,this.picture().mzkp_variableValue);
        }   

    };

    Spriteset_Base.prototype.mzkp_isAnyPicturePressed = function() {
        return this._pictureContainer.children.some(sprite =>
            sprite.isPressed()
        );
    };

    const _Scene_Map_isAnyButtonPressed =
        Scene_Map.prototype.isAnyButtonPressed;
    Scene_Map.prototype.isAnyButtonPressed = function() {
        return (
            _Scene_Map_isAnyButtonPressed.apply(this, arguments) ||
            this._spriteset.mzkp_isAnyPicturePressed()
        );
    };
    
    
})();


Thanks in advance for any help!
 

Eliaquim

Hakuen Studio
Regular
Joined
May 22, 2018
Messages
3,345
Reaction score
2,643
First Language
Portuguese - Br
Primarily Uses
RMMZ
Hi there!

Let me see if I understood you correctly:

When you click on a picture, you want it:
- Run a specific common event ID
- Run a common event ID, defined by a variable value
- Run a specific regular map event
- Run a regular map event defined by a variable value

You want to be able to decide what of the options above you will use, that is it?
 

_Soysauce_

Villager
Member
Joined
Sep 12, 2018
Messages
25
Reaction score
6
First Language
English
Primarily Uses
RMMZ
Hi there!

Let me see if I understood you correctly:

When you click on a picture, you want it:
- Run a specific common event ID
- Run a common event ID, defined by a variable value
- Run a specific regular map event
- Run a regular map event defined by a variable value

You want to be able to decide what of the options above you will use, that is it?


Yes, I'd like for the ButtonPlugin to be extended to support 4 different plugin calls.
I believe you wrote them down correctly but I'll repeat what the calls are supposed to do because I'm unsure of the terminology when it comes to code (real sorry)

- Pick a picture number and a specific common event ID to run
- Pick a picture number, a specific common event ID, a variable and a value to assign to the latter
- Pick a picture number and a specific regular map event ID to run
- Pick a picture number, a regular map event, a variable and a value to assign to the latter


The basis of this seem to work fine in the plugin I posted, but for some reason while all the debug tools tell me the variable's value assigned by plugin call is correct, it cannot be picked up by IF in any event.
 

Eliaquim

Hakuen Studio
Regular
Joined
May 22, 2018
Messages
3,345
Reaction score
2,643
First Language
Portuguese - Br
Primarily Uses
RMMZ
Yes, I'd like for the ButtonPlugin to be extended to support 4 different plugin calls.
I believe you wrote them down correctly but I'll repeat what the calls are supposed to do because I'm unsure of the terminology when it comes to code (real sorry)

- Pick a picture number and a specific common event ID to run
- Pick a picture number, a specific common event ID, a variable and a value to assign to the latter
- Pick a picture number and a specific regular map event ID to run
- Pick a picture number, a regular map event, a variable and a value to assign to the latter


The basis of this seem to work fine in the plugin I posted, but for some reason while all the debug tools tell me the variable's value assigned by plugin call is correct, it cannot be picked up by IF in any event.
I think I understood it now.

What is that variable value? It is a number?
Because I'm seeing that you are converting the variable ID argument to a number, but not the variable value argument:

1685498281736.png

Maybe that is the issue?
 

_Soysauce_

Villager
Member
Joined
Sep 12, 2018
Messages
25
Reaction score
6
First Language
English
Primarily Uses
RMMZ
Ohhh, I see it now.

I was trying to store both numbers and letters into variables, and assumed it worked fine since it seemed so when printing them out. But I guess that if it's not converted into a number, the basic Rpg Maker's IF check goes awry when handling a number into a non-numbered variable.

At least I guess that's what happens - I have little code knowledge, but if that's all there is to it I'm just gonna drop the letter support and tweak the code as you suggested to convert the Variable Value argument to a number.
 

Eliaquim

Hakuen Studio
Regular
Joined
May 22, 2018
Messages
3,345
Reaction score
2,643
First Language
Portuguese - Br
Primarily Uses
RMMZ
Ohhh, I see it now.

I was trying to store both numbers and letters into variables, and assumed it worked fine since it seemed so when printing them out. But I guess that if it's not converted into a number, the basic Rpg Maker's IF check goes awry when handling a number into a non-numbered variable.

At least I guess that's what happens - I have little code knowledge, but if that's all there is to it I'm just gonna drop the letter support and tweak the code as you suggested to convert the Variable Value argument to a number.
I guess you are trying to use the same plugin command argument as both letters and numbers. The thing is, arguments are all strings by default.
So even if launch a number into the argument, and do not convert it, it will be considered a string.

But you need to know that, when comparing values on javascript, you can use "==" or "===":

== → Will compare the values, regardless of their types. Meaning "1"(string) will be equal to 1(number). So "1" == 1 true

===
→ On that one, the above will return false because it compares values and their types.
"1" === 1 false
So, maybe you are assigning the values right if you want to use strings. But are comparing them wrong.

When comparing variables on the conditional branch, the RPG Maker uses the "==="(Also known as a strict comparison). Variables comparison are the Case 1

Here is the conditional branch code:

JavaScript:
Game_Interpreter.prototype.command111 = function(params) {
    let result = false;
    let value1, value2;
    let actor, enemy, character;
    switch (params[0]) {
        case 0: // Switch
            result = $gameSwitches.value(params[1]) === (params[2] === 0);
            break;
        case 1: // Variable
            value1 = $gameVariables.value(params[1]);
            if (params[2] === 0) {
                value2 = params[3];
            } else {
                value2 = $gameVariables.value(params[3]);
            }
            switch (params[4]) {
                case 0: // Equal to
                    result = value1 === value2;
                    break;
                case 1: // Greater than or Equal to
                    result = value1 >= value2;
                    break;
                case 2: // Less than or Equal to
                    result = value1 <= value2;
                    break;
                case 3: // Greater than
                    result = value1 > value2;
                    break;
                case 4: // Less than
                    result = value1 < value2;
                    break;
                case 5: // Not Equal to
                    result = value1 !== value2;
                    break;
            }
            break;
        case 2: // Self Switch
            if (this._eventId > 0) {
                const key = [this._mapId, this._eventId, params[1]];
                result = $gameSelfSwitches.value(key) === (params[2] === 0);
            }
            break;
        case 3: // Timer
            if ($gameTimer.isWorking()) {
                const sec = $gameTimer.frames() / 60;
                if (params[2] === 0) {
                    result = sec >= params[1];
                } else {
                    result = sec <= params[1];
                }
            }
            break;
        case 4: // Actor
            actor = $gameActors.actor(params[1]);
            if (actor) {
                const n = params[3];
                switch (params[2]) {
                    case 0: // In the Party
                        result = $gameParty.members().includes(actor);
                        break;
                    case 1: // Name
                        result = actor.name() === n;
                        break;
                    case 2: // Class
                        result = actor.isClass($dataClasses[n]);
                        break;
                    case 3: // Skill
                        result = actor.hasSkill(n);
                        break;
                    case 4: // Weapon
                        result = actor.hasWeapon($dataWeapons[n]);
                        break;
                    case 5: // Armor
                        result = actor.hasArmor($dataArmors[n]);
                        break;
                    case 6: // State
                        result = actor.isStateAffected(n);
                        break;
                }
            }
            break;
        case 5: // Enemy
            enemy = $gameTroop.members()[params[1]];
            if (enemy) {
                switch (params[2]) {
                    case 0: // Appeared
                        result = enemy.isAlive();
                        break;
                    case 1: // State
                        result = enemy.isStateAffected(params[3]);
                        break;
                }
            }
            break;
        case 6: // Character
            character = this.character(params[1]);
            if (character) {
                result = character.direction() === params[2];
            }
            break;
        case 7: // Gold
            switch (params[2]) {
                case 0: // Greater than or equal to
                    result = $gameParty.gold() >= params[1];
                    break;
                case 1: // Less than or equal to
                    result = $gameParty.gold() <= params[1];
                    break;
                case 2: // Less than
                    result = $gameParty.gold() < params[1];
                    break;
            }
            break;
        case 8: // Item
            result = $gameParty.hasItem($dataItems[params[1]]);
            break;
        case 9: // Weapon
            result = $gameParty.hasItem($dataWeapons[params[1]], params[2]);
            break;
        case 10: // Armor
            result = $gameParty.hasItem($dataArmors[params[1]], params[2]);
            break;
        case 11: // Button
            switch (params[2] || 0) {
                case 0:
                    result = Input.isPressed(params[1]);
                    break;
                case 1:
                    result = Input.isTriggered(params[1]);
                    break;
                case 2:
                    result = Input.isRepeated(params[1]);
                    break;
            }
            break;
        case 12: // Script
            result = !!eval(params[1]);
            break;
        case 13: // Vehicle
            result = $gamePlayer.vehicle() === $gameMap.vehicle(params[1]);
            break;
    }
    this._branch[this._indent] = result;
    if (this._branch[this._indent] === false) {
        this.skipBranch();
    }
    return true;
};


The only way for you to compare variables that has strings values on the conditional branch is by using the script method, with the non strict comparison(==):
1685561280758.png

This way, "1" will be equal to 1. But "1" will not be equal to "A". I think if you do that way, your conditional branches will proper compare, and you may use the plugin command argument without the need of converting it to a number.
 

_Soysauce_

Villager
Member
Joined
Sep 12, 2018
Messages
25
Reaction score
6
First Language
English
Primarily Uses
RMMZ
Thanks a lot, I get it now - I believe at the end of the day it might be easier for me to just have the code properly convert the values to numbers, though I'm glad to know exactly how Rpg Maker handles IF cases in the future.

Thanks again!
 

Latest Threads

Latest Profile Posts

Resisting the urge to bust out watching spooky things all over. I still have days until it's Halloween month, gotta ration!
Made a free sample pack + leftovers. Free Sample Pack 1
I'm curious, how many hours in RPG Maker do you have? I'm clocked in at 2100 hours on MV and the vast majority of it is for one game. I wish I could track how much I used XP back in the day, I was on it a lot.
Why is it so early? Can I have twenty more minutes of sleep, please?
DK
Do you like this design?

GIF.gif

Forum statistics

Threads
134,810
Messages
1,250,845
Members
177,606
Latest member
Buffinto
Top