Toggle plugin on/off with control switch

Discussion in 'Javascript/Plugin Support' started by vampyyri, Mar 5, 2016.

  1. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English
    I have a plugin of a meter that should only appear when the control switch at 20 is set to on. How to I achieve this? Currently, the meter appears in every map.
     
    Last edited by a moderator: Apr 27, 2016
    #1
  2. Shaz

    Shaz Veteran Veteran

    Messages:
    37,574
    Likes Received:
    11,274
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    What is the plugin?  Got a link?  Or is it one you've written yourself?
     
    #2
  3. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English


    I wrote it myself. If I could see an example, I might be able to get it.
     
    #3
  4. Shaz

    Shaz Veteran Veteran

    Messages:
    37,574
    Likes Received:
    11,274
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    If it's something that's displayed on the map, you should add some logic when the map is loaded/displayed the first time, to make the meter visible or invisible based on the value of the switch.


    Whether this is easy or hard depends on how you've created the meter - if it's a single sprite or a collection of sprites, or a window like the gold window.


    I am not aware of any plugins that have stuff displayed on the map that toggle it on/off according to the value of a switch, so I can't link you to any examples.
     
    Last edited by a moderator: Mar 5, 2016
    #4
  5. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English
    Its a modded gold window and its a drawn rectangle that updates in size. 


    I have control switches to handle it being on /off so in the code, how do i check for that? Ive tried $gameSwitches But maybe im not using it correctly. There are several functions that make the meter work so im not sure how to properly encompass them in an if statement.
     
    #5
  6. Andar

    Andar Veteran Veteran

    Messages:
    28,333
    Likes Received:
    6,445
    Location:
    Germany
    First Language:
    German
    Primarily Uses:
    RMMV
    Only the display part of the plugin should be skipped if the switch is off. Theoretically you might reduce a minor bit of lag by skipping everything, but that would also prevent the correct data from existing the moment the display is activated again. So it's better if everything else (including variable definitions and handling) is always on.
     
    #6
  7. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English


    The part that draws it is not the same function that calculates the width of the rectangle for the meter, which sets off the random encounter battle. However, I have thought that as well, so... how do I do a conditional check or switch statement that takes from the control switch that is set in editor, throughout many maps? I've tried: if ($gameSwitches[20] == "ON"/true), within the calculating and drawing functions.
     
    #7
  8. Shaz

    Shaz Veteran Veteran

    Messages:
    37,574
    Likes Received:
    11,274
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    If it's a modded gold window, you just need to set the visible parameter.  That's it.


    so find the function that loads/draws the map, and tell it to make the window visible if the switch is on.  It's as simple as


    this._myWindow.visible = $gameSwitches.value(20)


    Syntax might not be right there - see what the correct method is to set other windows' visibility, and to refer to a window owned by Scene_Map.  The message window or the gold window would be good candidates to look at.
     
    #8
  9. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English


    Here is the code, so where should I make changes? I have tried out adding that line in a few spaces -- instead of visible, it's visibility -- but it does not work. 

    Code:
    var Imported = Imported || {};
    Imported.EncounterMeter = true;
     
    var Yanfly = Yanfly || {};
    
    /*:
     * @plugindesc Create encounter meter for map encounters.
     * @author 
     * 
     * @param Opacity
     * @desc The opacity value used for the gold window.
     * Default: 255
     * @default 255
     */
    
    Yanfly.Parameters = PluginManager.parameters('EncounterMeter');
    var is_refresh = false;
    var gauge_value = 0;
    var gauge_max = 47;
    var wait_count = 20;
    var isGauge_full = false;
    
    Scene_Map.prototype.createDisplayObjects = function () {
        this.createSpriteset();
        this.createMapNameWindow();
        this.createWindowLayer();
        this.createAllWindows();
        this.createGauge();
    };
    
    Game_Player.prototype.moveStraight = function (d) {
        if (this.canPass(this.x, this.y, d)) {
            this._followers.updateMove();
            is_refresh = false;
        }
        Game_Character.prototype.moveStraight.call(this, d);
    };
    
    var alias_map = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function () {
        alias_map.apply(this);
        if (!is_refresh) {
            if (gauge_value < (gauge_max)) {
                gauge_value += 1;
            } else {
                isGauge_full = true;
            }
            this._encounterMeter.refresh(gauge_value, gauge_max);
            is_refresh = true;
            wait_count = 20;
        } else {
            if (wait_count < 1) {
                if ((gauge_value > 0) == true) {
                    gauge_value -= 1;
                    isGauge_full = false;
                }
                this._encounterMeter.refresh(gauge_value, gauge_max);
                wait_count = 20;
            } else {
                wait_count -= 1;
            }
        };
    };
    
    Game_Player.prototype.executeEncounter = function () {
        if (isGauge_full) {
            if (!$gameMap.isEventRunning() && this._encounterCount <= 0) {
                this.makeEncounterCount();
                var troopId = this.makeEncounterTroopId();
                if ($dataTroops[troopId]) {
                    BattleManager.setup(troopId, true, false);
                    BattleManager.onEncounter();
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        };
    };
    
    Scene_Map.prototype.updateEncounter = function () {
        if ($gamePlayer.executeEncounter()) {
            SceneManager.push(Scene_Battle);
            gauge_value = 0;
            isGauge_full = false;
        }
    };
    
    Scene_Map.prototype.createGauge = function () {
        this._encounterMeter = new Encounter_Meter(gauge_value, gauge_max);
        this._encounterMeter.opacity = Number(Yanfly.Parameters['Opacity']);
        this.addChild(this._encounterMeter);
    };
    
    Window_Base.prototype.drawEncounterArea = function (percent, max, x, y, width) {
        width = width || percent;
        var color1 = this.hpGaugeColor1();
        var color2 = this.hpGaugeColor2();
        var color3 = this.normalColor();
        this.drawGauge(x, y, width, 0, color3, color3);
        this.drawGauge(x, y, ((percent / max) * width), 50, color1, color2);
    };
    
    function Encounter_Meter() {
        this.initialize.apply(this, arguments);
    }
    
    Encounter_Meter.prototype = Object.create(Window_Base.prototype);
    Encounter_Meter.prototype.constructor = Encounter_Meter;
    
    Encounter_Meter.prototype.initialize = function (percent, max, text, x, y) {
        var width = this.windowWidth();
        var height = this.windowHeight();
        Window_Base.prototype.initialize.call(this, x, y, width, height);
        this.refresh(percent, max);
    };
    
    Encounter_Meter.prototype.windowWidth = function () {
        return 240;
    };
    
    Encounter_Meter.prototype.windowHeight = function () {
        return this.fittingHeight(1);
    };
    
    Encounter_Meter.prototype.refresh = function (percent, max) {
        var width = this.contents.width - this.textPadding();
        this.contents.clear();
        this.drawEncounterArea(percent, max, 0, -15, width);
    };
    
    Encounter_Meter.prototype.open = function () {
        this.refresh();
        Window_Base.prototype.open.call(this);
    };
     
    #9
  10. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English


    I've tried adding this in several places and I cannot get it to work. I posted the plugin code above if you would like to take a look.
     
    #10
  11. Shaz

    Shaz Veteran Veteran

    Messages:
    37,574
    Likes Received:
    11,274
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    In Scene_Map.prototype.update, add this line - shouldn't really matter if you put it at the start or at the end.

    Code:
    this._encounterMeter = $gameSwitches.value(20);


    Let me know if it works.
     
    #11
  12. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English
    Hey there, sorry for the long time. What I have now is "TypeError: undefined is not a function." Any idea what that problem could mean?
     
    #12
  13. Shaz

    Shaz Veteran Veteran

    Messages:
    37,574
    Likes Received:
    11,274
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    can you copy/paste the whole function?
     
    #13
  14. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English
    var alias_map = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function () {
        alias_map.apply(this);
        this._encounterMeter = $gameSwitches.value(20);
        if (!is_refresh) {
            if (gauge_value < (gauge_max)) {
                gauge_value += 1;
            } else {
                isGauge_full = true;
            }
            this._encounterMeter.refresh(gauge_value, gauge_max);
            is_refresh = true;
            wait_count = 20;
        } else {
            if (wait_count < 1) {
                if ((gauge_value > 0) == true) {
                    gauge_value -= 1;
                    isGauge_full = false;
                }
                this._encounterMeter.refresh(gauge_value, gauge_max);
                wait_count = 20;
            } else {
                wait_count -= 1;
            }
        };
    };
     
    #14
  15. Shaz

    Shaz Veteran Veteran

    Messages:
    37,574
    Likes Received:
    11,274
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    and if you comment out that line, does the error go away?  I don't really see anything wrong with that.
     
    #15
  16. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English
    Yes it does, but the meter is still on the intro screen.
     
    #16
  17. Shaz

    Shaz Veteran Veteran

    Messages:
    37,574
    Likes Received:
    11,274
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    does it tell you what line the error occurs on?  What is on that line?


    Scene_Map should not run if $gameSwitches is not defined - have you added any plugins that do weird things like making the map load before the game data is set up (like a plugin that lets you use a map as the title screen)?
     
    #17
  18. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English


    I didn't think so. The intro screen is still the normal generic one.


    TypeError: undefined is not a function
        at Scene_Map.update (file:///[Directory]/js/plugins/Encounter.js:49:30)
        at Function.SceneManager.updateScene (file:///[Directory]/js/rpg_managers.js:1673:25)
        at Function.SceneManager.updateMain (file:///[Directory]/js/plugins/TDDP_FluidTimestep.js:108:18)
        at Function.SceneManager.update (file:///[Directory]/js/plugins/TDDP_FluidTimestep.js:86:18)
     
    #18
  19. Shaz

    Shaz Veteran Veteran

    Messages:
    37,574
    Likes Received:
    11,274
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Can you post the whole Encounter.js plugin?  Preferably with line numbers showing, if that's an option.
     
    #19
  20. vampyyri

    vampyyri Villager Member

    Messages:
    15
    Likes Received:
    0
    First Language:
    English
    Code:
    var Imported = Imported || {};
    Imported.Encounter = true;
     
    var Yanfly = Yanfly || {};
    
    /*:
     * @plugindesc Create encounter meter for map encounters.
     * @author 
     * 
     * @param Opacity
     * @desc The opacity value used for the gold window.
     * Default: 255
     * @default 255
     */
    
    Yanfly.Parameters = PluginManager.parameters('Encounter');
    var is_refresh = false;
    var gauge_value = ;
    var gauge_max = 47;
    var wait_count = 20;
    var isGauge_full = false;
    
    Scene_Map.prototype.createDisplayObjects = function () {
        this.createSpriteset();
        this.createMapNameWindow();
        this.createWindowLayer();
        this.createAllWindows();
        this.createGauge();
    };
    
    Game_Player.prototype.moveStraight = function (d) {
        if (this.canPass(this.x, this.y, d)) {
            this._followers.updateMove();
            is_refresh = false;
        }
        Game_Character.prototype.moveStraight.call(this, d);
    };
    
    var alias_map = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function () {
        alias_map.apply(this);
    	this._encounterMeter = $gameSwitches.value(20);
        if (!is_refresh) {
            if (gauge_value < (gauge_max)) {
                gauge_value += 1;
            } else {
                isGauge_full = true;
            }
            this._encounterMeter.refresh(gauge_value, gauge_max);    //Line 49
            is_refresh = true;
            wait_count = 20;
        } else {
            if (wait_count < 1) {
                if ((gauge_value > ) == true) {
                    gauge_value -= 1;
                    isGauge_full = false;
                }
                this._encounterMeter.refresh(gauge_value, gauge_max);
                wait_count = 20;
            } else {
                wait_count -= 1;
            }
        };
    };
    
    Game_Player.prototype.executeEncounter = function () {
        if (isGauge_full) {
            if (!$gameMap.isEventRunning() && this._encounterCount <= ) {
                this.makeEncounterCount();
                var troopId = this.makeEncounterTroopId();
                if ($dataTroops[troopId]) {
                    BattleManager.setup(troopId, true, false);
                    BattleManager.onEncounter();
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        };
    };
    
    Scene_Map.prototype.updateEncounter = function () {
        if ($gamePlayer.executeEncounter()) {
            SceneManager.push(Scene_Battle);
            gauge_value = ;
            isGauge_full = false;
        }
    };
    
    Scene_Map.prototype.createGauge = function () {
        this._encounterMeter = new Encounter_Meter(gauge_value, gauge_max);
        this._encounterMeter.opacity = Number(Yanfly.Parameters['Opacity']);
        this.addChild(this._encounterMeter);
    };
    
    Window_Base.prototype.drawEncounterArea = function (percent, max, x, y, width) {
        width = width || percent;
        var color1 = this.hpGaugeColor1();
        var color2 = this.hpGaugeColor2();
        var color3 = this.normalColor();
        this.drawGauge(x, y, width, , color3, color3);
        this.drawGauge(x, y, ((percent / max) * width), 50, color1, color2);
    };
    
    function Encounter_Meter() {
        this.initialize.apply(this, arguments);
    }
    
    Encounter_Meter.prototype = Object.create(Window_Base.prototype);
    Encounter_Meter.prototype.constructor = Encounter_Meter;
    
    Encounter_Meter.prototype.initialize = function (percent, max, text, x, y) {
        var width = this.windowWidth();
        var height = this.windowHeight();
        Window_Base.prototype.initialize.call(this, x, y, width, height);
        this.refresh(percent, max);
    };
    
    Encounter_Meter.prototype.windowWidth = function () {
        return 240;
    };
    
    Encounter_Meter.prototype.windowHeight = function () {
        return this.fittingHeight(1);
    };
    
    Encounter_Meter.prototype.refresh = function (percent, max) {
        var width = this.contents.width - this.textPadding();
        this.contents.clear();
        this.drawEncounterArea(percent, max, , -15, width);
    };
    
    Encounter_Meter.prototype.open = function () {
        this.refresh();
        Window_Base.prototype.open.call(this);
    };
     
    #20

Share This Page