Changing switches/variables when items are equipped/removed

Discussion in 'Javascript/Plugin Support' started by AsuranFish, Jun 16, 2019.

  1. AsuranFish

    AsuranFish Veteran Veteran

    Messages:
    112
    Likes Received:
    79
    First Language:
    English
    I’m looking for a way to increase a variable when a certain item is equipped. The problem is, I don’t want the effect to stack if multiple party members equip the same item.

    I was thinking that maybe a switch would work if it’s controlled by having the item equipped and removed, but that also seems problematic. What if two party members equip the item, and then one unequips it. That might turn the switch off.

    What would be the best way to change a variable by having an item equipped, but ensuring the effect doesn’t stack?

    Thank you!
     
    #1
  2. caethyril

    caethyril ^_^ Veteran

    Messages:
    920
    Likes Received:
    577
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    I'd suggest having a common event run when you close the menu (e.g. using Yanfly's Utility Common Events plugin). In that event you could have something like this:
    Code:
    ◆Comment:Uses 1 switch: "is Sword equipped by any party member?"
    :       :Uses 1 variable: +5 iff Sword is equipped by any party member
    ◆Comment:-------------------------------------------------------
    ◆Comment:Initialise!
    ◆If:Sword Equipped is ON
      ◆Comment:If switch is on, that means the bonus is already in effect...
      :       :So undo equip bonus before switching off the switch
      ◆Control Variables:#0001 Equip Bonus -= 5
      ◆Control Switches:#0001 Sword Equipped = OFF
      ◆
    :End
    ◆Comment:Now check each actor:
    :       : - are they in the party
    :       : - if so, do they have Sword equipped?
    :       :If yes, turn on the switch!
    ◆If:Harold is in the party
      ◆If:Harold has equipped Sword
        ◆Control Switches:#0001 Sword Equipped = ON
        ◆
      :End
      ◆
    :End
    ◆If:Therese is in the party
      ◆If:Therese has equipped Sword
        ◆Control Switches:#0001 Sword Equipped = ON
        ◆
      :End
      ◆
    :End
    ◆Comment:...etc...
    ◆Comment:Is the switch on?
    ◆If:Sword Equipped is ON
      ◆Comment:Yes! Increase variable~
      ◆Control Variables:#0001 Equip Bonus += 5
      ◆
    :End
    If you have any event-based situations where equips can change, just call the common event afterwards to update your variable. :)

    Note that this approach also lets other sources contribute to the variable's value, e.g. you could have multiple different weapons, each with their own switch, all contributing to the same variable. Just need a few more branches in the event~ :kaothx:
     
    #2
  3. AsuranFish

    AsuranFish Veteran Veteran

    Messages:
    112
    Likes Received:
    79
    First Language:
    English
    Thanks! In this case, I’ll probably use one switch for multiple items. The variable I want to work with here is for equipment that boost movement speed, which could get kind of derpy if you’re able to stack it.

    I’ll try this out when I get home. Is there a way to do this for multiple characters without each having their own branch? I haven’t added all characters to my game yet, and would like to minimize things I may need to go back and edit later.
     
    #3
  4. caethyril

    caethyril ^_^ Veteran

    Messages:
    920
    Likes Received:
    577
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    You could try this in a Script command:
    Code:
    var swId = 1;        // ID of switch used
    var wIds = [1,2,3];  // IDs of weapons to check for
    var aIds = [1,5];    // IDs of armors to check for
    var value = $gameParty.members().some(function(actor) {
      return wIds.some(function(w) { return actor.hasWeapon($dataWeapons[w]); }) ||
             aIds.some(function(a) { return actor.hasArmor($dataArmors[a]); });
    });
    $gameSwitches.setValue(swId, value);
    Change the numbers in the first three lines as needed~ This script call will check all listed weapon and armor IDs. If you only want to check armors, just leave the weapon ID array blank, i.e.
    Code:
    var wIds = [];  // no weapon check
    Note that this is only a replacement for the middle bit (i.e. "turn on switch as appropriate"), you'll still need the start and end parts. :kaophew:
     
    #4
  5. AsuranFish

    AsuranFish Veteran Veteran

    Messages:
    112
    Likes Received:
    79
    First Language:
    English

    Thanks again! I'm going to give this a shot. :)

    Do I need to change the last line in the script command at all, or should I leave it alone?

    Edit: Got it to work! Thanks so much!
     
    Last edited: Jun 17, 2019
    #5
    caethyril likes this.
  6. caethyril

    caethyril ^_^ Veteran

    Messages:
    920
    Likes Received:
    577
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    You're welcome, happy RPG Making! :kaojoy:
     
    #6

Share This Page