Using Script to Check What Weapon Type an Actor is Using

Although I am not well-versed in scripting, I can read and understand the basics, even though I usually don't know the proper way to write them out. The Script Call List on this forum is down, and I can't look at it right now. I'm utilizing ChatGPT for help, but I know it's only somewhat reliable.

I'm checking which type of weapon (NOT the weapon ID itself, but the TYPE of weapon) an actor is using. I'm using the following script:

Code:
var CHECKWeaponType = $gameActors.actor(2).weaponId()

As far as I understand it, this should return a number ID for the weapon type of Actor #2, and place that integer in the variable "CHECKWeaponType"

Then, I use another condition to check if "CHECKWeaponType" is #19 (which is the weapon ID I'm checking for in my game right now) to have some other things happen if that is true, however, nothing seems to happen. Could anyone tell me if my script is wrong?
The Script Call List on this forum is down, and I can't look at it right now.
No, it's not - it's a Google Sheet and it's displaying just fine for me 😉

I'm utilizing ChatGPT for help, but I know it's only somewhat reliable.
I would say so unreliable as to be worthless, but I won't bother trying to convince anyone.

Code:
var CHECKWeaponType = $gameActors.actor(2).weaponId()
As far as I understand it, this should return a number ID for the weapon type of Actor #2
No - I don't know where you got that from, but that function doesn't exist in MZ's code base at all.

Then, I use another condition to check if "CHECKWeaponType" is #19 (which is the weapon ID I'm checking for
Why? You said very explicitly
I'm checking which type of weapon (NOT the weapon ID itself, but the TYPE of weapon)

So why are you doing anything with the ID when you said that's not what you're checking?

I Googled "rpg maker mz script call weapon type" and this thread comes up:

That has the correct script call
To check if a specific actor x has weapon type n equipped (actors are numbered as in the database Actors tab):
Code:
$gameActors.actor(x).isWtypeEquipped(n);

If you do multiple different checks on the weapon type so you want to store it into a variable, you could do
Code:
var weaponType = $gameActors.actor(2).weapons()[0]?.wtypeId;

Then weaponType will have the type of the weapon, or be undefined if they're unarmed.
To answer all your questions, I'll repeat what I said in the OP: "I am not well-versed in scripting" 😉

I understand the Script List is a Google Sheet. It's coming back 403 error for me right now.

I agree with you. My opinion of ChatGPT is lowering the more I use it and hear about it. AI is just not at a place where it is useful for anything outside of very basic info searching, and even then, it's questionable.

I tried to use the script you suggested, and inserted a text box to read back the variable during combat to see if it worked:
1717619297443.png
#126 is the variable "CHECKWeaponType" and the text box reading always returns a value of 0, no matter which weapon I have equipped. Thus, it is not working for me. Perhaps I am using it wrong somehow? 😕

Thank you for your time.
To answer all your questions, I'll repeat what I said in the OP: "I am not well-versed in scripting" 😉

I understand the Script List is a Google Sheet. It's coming back 403 error for me right now.

I agree with you. My opinion of ChatGPT is lowering the more I use it and hear about it. AI is just not at a place where it is useful for anything outside of very basic info searching, and even then, it's questionable.

I tried to use the script you suggested, and inserted a text box to read back the variable during combat to see if it worked:
View attachment 307582#126 is the variable "CHECKWeaponType" and the text box reading always returns a value of 0, no matter which weapon I have equipped. Thus, it is not working for me. Perhaps I am using it wrong somehow? 😕

Thank you for your time.
The var in the script call makes the variable only available within that scope (the eval).

Naming it as a currently available Control Variable with the same 'name' does not work since what you declared is the actual variable. The Control Variable event command uses a string to store the naming of it for convenience of referencing in the editor (which would require a different script to obtain it).

To get variable 126, you would use $gameVariables.value(126).

If using event commands, you'll need to use the script portion of the Control Variable event command (choosing variable 126) and put that specific script in there. Control Variables are global and can be accessed anywhere.

Or, you can use this script call by itself:
JavaScript:
$gameVariables.setValue(126, $gameActors.actor(2).weapons()[0]?.wtypeId)

Edit: I will say that the option chaining (?.) might not work if you have an older NWJS version. In which case, you will need to use other available code.
The var in the script call makes the variable only available within that scope (the eval).

Naming it as a currently available Control Variable with the same 'name' does not work since what you declared is the actual variable. The Control Variable event command uses a string to store the naming of it for convenience of referencing in the editor (which would require a different script to obtain it).

To get variable 126, you would use $gameVariables.value(126).

If using event commands, you'll need to use the script portion of the Control Variable event command (choosing variable 126) and put that specific script in there. Control Variables are global and can be accessed anywhere.

Or, you can use this script call by itself:
JavaScript:
$gameVariables.setValue(126, $gameActors.actor(2).weapons()[0]?.wtypeId)

Edit: I will say that the option chaining (?.) might not work if you have an older NWJS version. In which case, you will need to use other available code.
I do not know why I did not think to use the script section of the Control Variable command. Now it works properly. Thanks everyone for your help.
To answer all your questions, I'll repeat what I said in the OP: "I am not well-versed in scripting" 😉
That's fine, and I wasn't trying to make you feel bad or anything, but I was asking questions about non-scripting things - e.g. why you're saying you don't want the weapon's ID in one sentence but then you're talking about the weapon's ID in another.

That speaks to whether I understand what you're doing in order to provide the correct answers.

Edit: I will say that the option chaining (?.) might not work if you have an older NWJS version.
It works in the version that MZ ships with.

I'm glad you got it working! Good luck with your project.
It works in the version that MZ ships with.
MV 1.6.3 problems I tells ya... lol. You're right, I was thinking MV because OP was using var (not that MZ can't use it either of course, just used to seeing it in MV projects)

Original thread: https://forums.rpgmakerweb.com/threads/using-script-to-check-what-weapon-type-an-actor-is-using.169270/