VXAce Assigning Item Target ID to Variable

I'd like to have an item work differently based on who the target is, and I cannot find the proper way to set the variable.
The item calls a common event, which uses filler text until I get this sorted;

@>Conditional Branch: Variable [0015:ItemTargetID] == 1
@>Text: -, -, Normal, Bottom
: : Target ID was 1
@>
: Else
@>Conditional Branch: Variable [0015:ItemTargetID] == 2
@>Text: -, -, Normal, Bottom
: : Target ID was 2
@>
: Branch End
@>
: Branch End
@>

What I need is a way to assign the ID of the item's target to variable 15, outside of combat. The closest to an answer that I've found is:

$gameVariables.setValue(0015, a.id)

However, due to being out of combat, a.id crashes the game. All references to checking an item's target that I've found are for MV, so I don't think I can pluck much relevant code for this case. I've also concluded that the item's damage formula can't be used, again due to being outside of combat.

More information that may help explain my goal;
I intend to set the variable just before the first conditional branch.
Text will be replaced with boosting the stats of a specific party member when used, otherwise nothing will happen. (I can work this part out myself)
The scope of the item is One Ally, useable only from the Menu. It is not a key item, and is consumed upon use. (I'm unsure if those affect anything, but it may be worth mentioning)
the non-scripted method works slightly different:
create an invisible taarget state (no icon, no message, no effect) and have that added by item effects.
then in the common event, check every actor if it has that state. If yes, do to the actor what you want and then remove the target state.
The damage formula is still evaluated even outside of combat, you can design healing spells for instance that works outside of combat.

$gameVariables.setValue(0015, a.id)
This will fail because $gameVariables.setValue is not the way of modifying a variable in RGSS.
Also, note that a refers to the user while b refers to the target, in your case, you might want to use b.id.

Use this formula instead:
0 * $game_variables[15] = b.id

This will set variable 15 to the target's id. I put a multiplication by 0 because otherwise the damage formula will return the value of the variable.
Thanks for the quick responses!
0 * $game_variables[15] = b.id
I plugged this in and got an error;
Script 'Window_Message' line 347: NoMethodError occured.
undefined method 'upcase' for nil:NilClass

This didn't happen when i changed it to
$game_variables[15] = b.id * 0
but then debug mode said the variable was unaffected.

I haven't modified any of the scripts, though I have added some Yanfly stuff under Materials that hasn't given this message before. I'm fairly new to Ruby and RPG Maker so I'm not sure how to diagnose this.

Update: I noticed I had typed $game_variable instead of $game_variables, upon fixing this the error message returned
For reference, here is my item:
1751006936477.png

And here is my common event:
1751006955508.png


The item simply change the variable and then when calling the common event, I display the value of the variable in a message, it gives me the proper value.

If it still fails on your side, can you try this on an empty project first? If it works on the empty project and not yours, then there is a script conflicting with this method (though I find this unlikely).

Andar's answer might be a better solution as it is more stable and does not do out-of-scope effects in the damage formula.
In a new project, it worked flawlessly. I don't know what causes that error, so I'll try to avoid damage formulas for now.

It took a bit to understand, but I got Andar's solution working! I can now happily say that nobody except the rat benefits from eating cheese 😛
[0015:ItemTargetID
Just as an added comment to prevent a totally different error:

NEVER use leading zeroes for numbers in scripts.

The ID is 15 and NOT 0015.

The reason is that the double zero is the converter for octal numbers.
and octal 0015 is decimal 13 - using the leading zero would cause the wrong ID to be used here.
Just as an added comment to prevent a totally different error:

NEVER use leading zeroes for numbers in scripts.

The ID is 15 and NOT 0015.

The reason is that the double zero is the converter for octal numbers.
and octal 0015 is decimal 13 - using the leading zero would cause the wrong ID to be used here.
Oooo, thanks for the warning! I'm mostly learning from posts where everyone seems to understand, so there aren't many warnings like this. I'll go through and make sure I don't have any others. I assume it's also an issue if it's a single zero?
no, only for the double zero.

on the other hand you might have already seen the 0x for hexadecimal numbers, like 0xFF is actually decimal 255.

the first zero just causes all compilers and all interpreters (not just RGSS, but everything like javascript, C, C++, C#, Pascal and whatever else) to check if the second letter after that is one of the defined converters, otherwise the single leading zero is ignored.

There are other possible number system converters, but only hexadecimal is really used anymore, and octal is often mentioned due to accidental use instead of intended use (like you did, and for similar reasons)

Original thread: https://forums.rpgmakerweb.com/threads/assigning-item-target-id-to-variable.178571/