MZ Get the Actor ID of the Caster in Javascript

I can't seem to find this in google. I want to return the actor ID of the actor who performed a skill. I made a common event that will be run if the skill is casted. Inside the common event is a JavaScript function.

Here is my code:
JavaScript:
var skillCasterID; //this variable will store the caster id if a skill is casted

function getID()
{
    console.log('this function will be executed inside the common event when the skill is casted.');
    skillCasterID = ?;
}

I still don't know if this is the right approach. Thanks for the help.
To get the battler that's currently using an action, you'd normally use:

JavaScript:
BattleManager._subject

But, I just checked, and BattleManager doesn't have a subject by the time that a common event that's attached to a skill happens. So, you could use this small plugin:

JavaScript:
const Fionn_BattleManager_initMembers = BattleManager.initMembers;
BattleManager.initMembers = function() {
    Fionn_BattleManager_initMembers.apply(this);
    this._lastSubject = null;
}
const Fionn_BattleManager_startAction = BattleManager.startAction;
BattleManager.startAction = function() {
    Fionn_BattleManager_startAction.apply(this);
    if (this._subject) this._lastSubject = this._subject;
}

And then you can check the actor ID in your common event like this:

JavaScript:
console.log(BattleManager._lastSubject._actorId);
To get the battler that's currently using an action, you'd normally use:

JavaScript:
BattleManager._subject

But, I just checked, and BattleManager doesn't have a subject by the time that a common event that's attached to a skill happens. So, you could use this small plugin:

JavaScript:
const Fionn_BattleManager_initMembers = BattleManager.initMembers;
BattleManager.initMembers = function() {
    Fionn_BattleManager_initMembers.apply(this);
    this._lastSubject = null;
}
const Fionn_BattleManager_startAction = BattleManager.startAction;
BattleManager.startAction = function() {
    Fionn_BattleManager_startAction.apply(this);
    if (this._subject) this._lastSubject = this._subject;
}

And then you can check the actor ID in your common event like this:

JavaScript:
console.log(BattleManager._lastSubject._actorId);
Thanks! This is working.

I have a question though. What does BattleManager.initMembers and BattleManager.startAction do?
Common Events technically run as an effect after a skill is used. So you need the Last Actor ID, which will be saved as part of the Last Action Data which is an array of values like this [0,0,0,0,0,0]

$gameTemp._lastActionData ➡ [0-Last Skill, 1-Last Item, 2-Actor ID Act, 3-Enemy Index Act, 4-Actor targeted, 5-Enemy Targeted]

So $gameTemp._lastActionData[2] will return the ID of the last Actor ID that acted.
This is pretty useful information, but easy to forget. I keep it written down as a comment in various places for reference.
$gameTemp._lastActionData

I had a feeling this information would be accessible somewhere without a plugin. Great tip!
Common Events technically run as an effect after a skill is used. So you need the Last Actor ID, which will be saved as part of the Last Action Data which is an array of values like this [0,0,0,0,0,0]

$gameTemp._lastActionData ➡ [0-Last Skill, 1-Last Item, 2-Actor ID Act, 3-Enemy Index Act, 4-Actor targeted, 5-Enemy Targeted]

So $gameTemp._lastActionData[2] will return the ID of the last Actor ID that acted.
This is pretty useful information, but easy to forget. I keep it written down as a comment in various places for reference.
This is also working. Thanks guys! This thread can be lock now.
Even though the question has already been answered, I figured that it's worth mentioning that you actually can do this without JavaScript as well. If you do a Control Variables event command, and tick Game Data and then tick Last and then select Last Actor ID to Act, it'll save the actor ID into the variable.
@Fionn23 If your problem is solved you can report your post to let mods know. We may miss a post where you say the thread can be closed, but we won't miss a report.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/get-the-actor-id-of-the-caster-in-javascript.147962/