Checking which weapon is being currently being used to Attack

Damascus7

Veteran
Veteran
Joined
Jul 21, 2019
Messages
100
Reaction score
23
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?
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,087
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
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/
 

Damascus7

Veteran
Veteran
Joined
Jul 21, 2019
Messages
100
Reaction score
23
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.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,087
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
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). :)
 

Damascus7

Veteran
Veteran
Joined
Jul 21, 2019
Messages
100
Reaction score
23
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")
}
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,087
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
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")
}
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:
 

Damascus7

Veteran
Veteran
Joined
Jul 21, 2019
Messages
100
Reaction score
23
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>



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


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

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 damn confused why it's weapon 3004 when there are only 11 in my database.
 
Last edited:

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,087
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
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. :)
 

Damascus7

Veteran
Veteran
Joined
Jul 21, 2019
Messages
100
Reaction score
23
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!
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,087
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
You're welcome, happy RPG Making! :kaojoy:
 

Damascus7

Veteran
Veteran
Joined
Jul 21, 2019
Messages
100
Reaction score
23
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));
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,087
Reaction score
1,508
First Language
EN
Primarily Uses
RMMZ
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:
 

Damascus7

Veteran
Veteran
Joined
Jul 21, 2019
Messages
100
Reaction score
23
First Language
English
Primarily Uses
RMMV
Awesome it works perfectly! Thank you so much!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,860
Messages
1,017,040
Members
137,569
Latest member
Shtelsky
Top