Checking which weapon is being currently being used to Attack

Discussion in 'Javascript/Plugin Support' started by Damascus7, Jul 22, 2019.

  1. Damascus7

    Damascus7 Villager Member

    Messages:
    26
    Likes Received:
    8
    First Language:
    English
    Primarily Uses:
    RMMV
    I'm working with Yanfly's Skill Core, and want to put in a call tag just for the basic Attack skill, to run after the Attack action is carried out by an actor.

    I'd like for the script to check which weapon or weapon type is being used to attack. Or put another way I guess, I want the script to check the weapon type equipped by the actor that's attacking. I'm basically putting together a system where attacking with a certain weapon will raise the skill level for that weapon. It's easy enough for other skills that only one actor can use, but every actor can take the Attack action, so how can I have the script differentiate based on who's attacking?
     
    #1
  2. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,055
    Likes Received:
    659
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    Try this:
    Code:
    a.weapons()[0] ? a.weapons()[0].wtypeId : 0
    This should return the weapon type ID (defined on the Types tab in the database) for the skill user's first equipped weapon, or 0 if no weapon is equipped. Alternative:
    Code:
    a.equips()[0] ? a.equips()[0].wtypeId : 0
    This'll check their first equip slot instead! If you need to check the specific weapon ID, rather than just its type, replace wtypeId with id.

    A safer format, since enemies typically don't have equipment:
    Code:
    (a.isActor() && a.weapons()[0]) ? a.weapons()[0].wtypeId : 0
    This says "if the skill user is an actor and has a weapon equipped, return that weapon's weapon type ID, else return 0". :kaopride:

    In case you have trouble getting it working or want an alternative, Galv happens to have a Weapon Proficiency plugin:
    https://forums.rpgmakerweb.com/index.php?threads/galvs-weapon-proficiency.73160/
     
    #2
    Engr. Adiktuzmiko likes this.
  3. Damascus7

    Damascus7 Villager Member

    Messages:
    26
    Likes Received:
    8
    First Language:
    English
    Primarily Uses:
    RMMV
    Thank you! So if I wanted the script to check if a sword was equipped, I would want the script to read:

    if (a.weapons()[0] ? a.weapons()[0].wtypeId : 0 === 1) {function to execute}

    I'm pretty new to Java so I hope this isn't way off.
     
    #3
  4. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,055
    Likes Received:
    659
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    Stick the whole thing in brackets, then check for equality:
    Code:
    if ((a.weapons()[0] ? a.weapons()[0].wtypeId : 0) === 1) {
      // stuff
    }
    Without brackets it would return the weapon type ID when there is a weapon, else false (because 0 === 1 is always false). :)
     
    #4
  5. Damascus7

    Damascus7 Villager Member

    Messages:
    26
    Likes Received:
    8
    First Language:
    English
    Primarily Uses:
    RMMV
    Okay I think I must be missing something, cause I've been trying to run this script and it's not working. I decided to go with id over wtypeId, and am running this script when the actor attacks with a weapon that's id 5, and it's not working.

    Code:
    if ((a.weapons()[0] ? a.weapons()[0].id : 0) === 5) {
    $gameMessage.add("check")
    }
     
    #5
  6. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,055
    Likes Received:
    659
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    Just tried with something almost identical and it works for me:
    Code:
    <Before Eval>
    if ((a.weapons()[0] ? a.weapons()[0].id : 0) === 1) {
      console.info(a.weapons()[0].id);
    }
    </Before Eval>

    In case it helps, you can press F8 to show the console during playtest. Any error messages should display in the console window in red text.
    • Are you using that where a is defined, e.g. in an appropriate skill notetag? A screenshot of your skill in the database might help.
    • What is weapon 5 in your database, and are you certain the skill user is equipped with that weapon?
    • You could try a test message, e.g.
      Code:
      $gameMessage.add(a.weapons()[0] ? a.weapons()[0].id : 0);
      If no message appears, then some part of the statement is likely not defined, implying you are not using the script call in an appropriate context and/or the assigned trigger for the script call is not firing.
    • Are you using any plugins other than Yanfly's Skill Core? If so:
     
    #6
  7. Damascus7

    Damascus7 Villager Member

    Messages:
    26
    Likes Received:
    8
    First Language:
    English
    Primarily Uses:
    RMMV
    Here's the code I have under the Attack skill:
    Code:
    <Post-Damage Eval>
    if ((a.weapons()[0] ? a.weapons()[0].id : 0) === 5) {
      $gameMessage.add("check");
    }
    <Post-Damage Eval>
    [​IMG]

    [​IMG]

    And I definitely have a Spear as weapon 5, which is equipped by the actor.
    [​IMG]
    [​IMG]

    But still not working. Here's the plugins as I have them installed:
    [​IMG]

    I don't know if I'm using the developer window correctly, but I'm not getting any error messages in it.

    EDIT: Okay, I used the test message you put in, and the number it returned for me is... 3004?????

    When I have the script check if I have weapon #3004(?????!!??) equipped, the script works as intended. But now I'm really dayum confused why it's weapon 3004 when there are only 11 in my database.
     
    Last edited: Jul 28, 2019
    #7
  8. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,055
    Likes Received:
    659
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    You're using Yanfly's Item Core, which implements independent items. Either disable independent weapons by setting the plugin's Max Weapons parameter to 0 in the Plugin Manager, or try this instead:
    Code:
    <Post-Damage Eval>
    if ((a.weapons()[0] ? a.weapons()[0].baseItemId : 0) === 5) {
      $gameMessage.add("check");
    }
    <Post-Damage Eval>
    Independent items causes each new item to be given its own ID, and the default starting point is 3000 (also customisable via the plugin parameters). If you don't need each item instance (e.g. each Spear) to be potentially different from the next, I'd recommend setting Max Items, Max Weapons, and Max Armors all to 0 in the Plugin Manager. :)
     
    #8
  9. Damascus7

    Damascus7 Villager Member

    Messages:
    26
    Likes Received:
    8
    First Language:
    English
    Primarily Uses:
    RMMV
    Ah yes, that did the trick! The whole thing is working just as intended now. Thank you for all your help!
     
    #9
  10. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,055
    Likes Received:
    659
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    You're welcome, happy RPG Making! :kaojoy:
     
    #10
  11. Damascus7

    Damascus7 Villager Member

    Messages:
    26
    Likes Received:
    8
    First Language:
    English
    Primarily Uses:
    RMMV
    Sorry to come back again...

    Turns out I have to use independent items in order to use Yanfly's Attachable Augments, so I need a new way to check what weapon is equipped without having to write a separate script for each individual weapon.

    I am basically looking to set a variable equal to half the Atk value granted by the equipped weapon.

    So up until now, I've been using this, with the Attack value of weapon 5 being 10, so it grants 5 points:

    Code:
    if ((a.weapons()[0] ? a.weapons()[0].id : 0) === 5) {
    $gameVariables.setValue(1, $gameVariables.value(1) + 5);
    }
    So now since I can't use wtypeId anymore, I need something else. I found this as a code in an old thread: a.weapon[atk]. But I'm not sure how to use it correctly. I tried to following code but it didn't run.

    Code:
    $gameVariables.setValue(1, $gameVariables.value(1) + (a.weapon[atk] / 2));
     
    #11
  12. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,055
    Likes Received:
    659
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    Oh drat, I knew I'd overlooked something! :kaosigh:

    Item Core has a DataManager.getBaseItem function, which should return the base item complete with all its stats etc. Then I believe you can check the base item's properties as usual, e.g.
    Code:
    var weapon = DataManager.getBaseItem(a.weapons()[0]);
    var value = weapon ? Math.floor(weapon.params[2] / 2) : 0;
    $gameVariables.setValue(1, value);
    Haven't tested, but hope it works! :kaopride:

    I assume the code from the old thread you found is either outdated, plugin-dependent, or incorrect. :kaoswt:
     
    #12
  13. Damascus7

    Damascus7 Villager Member

    Messages:
    26
    Likes Received:
    8
    First Language:
    English
    Primarily Uses:
    RMMV
    Awesome it works perfectly! Thank you so much!
     
    #13
    caethyril likes this.

Share This Page