MV Getting the current active event ID / interpreter

Does anyone know a way to get at the Game_Interpreter that is [i]currently running[/i]? I mean the one that is actually in the middle of executing a command at this exact moment, in other words, its [icode]executeCommand function is below the current function on the stack.

Some more context: I have some code that shows the name of the current event, and also some code that would like to access self switches of the current event. I'm currently using $gameMap._interpreter.eventId() for this purpose, but I'd like to make it work in parallel events as well. But parallel events don't use the main map interpreter at all, so I have no idea how to find the ID of the currently-running event.

If all else fails I could probably alias some method (probably executeCommand) to store the information somewhere accessible and delete it when it exits. I'd like to know if a means already exists first, though.

Also, as a side note: what if I want to get the ID and name of the currently-executing common event? I don't think it's relevant for my case here though, since common events don't have self switches.

(But speaking of self switches, does anyone know what happens if you try to use them in a troop event?)
Hey,

How do you get from the currently active interpreter to the code you want to run?

If you are executing an event script command or plugin command, you can use this to refer to the interpreter that is executing it (And this._eventId and this._mapId to determine the event the page belongs to, if applicable).

By default the interpreter does not save information about common- or troop events. An easy alternative to an extra plugin might be to put an identifying comment at the beginning of each common event and have your code check for that.

The "Control Self Switch" command will check the interpreters _eventId and _mapId properties to determine whose self switches should be manipulated.
When the event ID is set to 0 (as would usually be the case for troop events) then using that command would do nothing.

Edit: In addition to parallel processes, the "Call Common Event" command will also invoke a new interpreter instance to run the common event list. It will also copy the map- and event-ID from the interpreter that used the "Call Common Event" command.
(Edit3: Assuming you are still on the same map)

Edit2: Most of the time you could also modify the update function instead of executeCommand, if still required.
Well, the code is certainly not in a script command or plugin command. One of the cases is in Window_Base.prototype.convertEscapeCharacters (typically called via Window_Message). The other is in an eval called from a custom function in Game_Message. That function is called from Game_Interpreter.prototype.setupChoices, so this case might be the easier of the two, as it could ultimately pass the interpreter (or at least the event ID) through…

Note: For what it's worth, the second case is Aloe_ConditionalChoices, which also does something that looks absolutely insane in the name of supporting Yanfly's self variables and self switches. I have absolutely no idea if that part of the code works on parallel events or what, if anything, I could do to make it work there, given I don't use Yanfly.
Ah, that makes sense.

If you only need to support the message related commands, then modifying setupChoices (and maybe command101/command105) sounds like a good alternative to me.

(Assuming this could look something like this)
Spoiler
JavaScript:
Game_Message.prototype.clear = (alias => function() {
    this.setReferenceEvent(0, 0);
    alias.call(this);
})(Game_Message.prototype.clear);

Game_Message.prototype.setReferenceEvent = function(mapId, eventId) {
    this._referenceMapId = mapId;
    this._referenceEventId = eventId;
};

Game_Message.prototype.referenceEventName = function() {
    if (this._referenceEventId > 0 && this._referenceMapId === $gameMap.mapId()){
        return $gameMap.event(this._referenceEventId).event().name;
    } else {
        return "";
    }
};

Game_Interpreter.prototype.command101 = (alias => function() {
    if (!$gameMessage.isBusy()) {
        $gameMessage.setReferenceEvent(this._mapId, this._eventId);
    }
    return alias.call(this);
})(Game_Interpreter.prototype.command101);

Game_Interpreter.prototype.setupChoices = (alias => function(params) {
    $gameMessage.setReferenceEvent(this._mapId, this._eventId);
    alias.call(this, params);
})(Game_Interpreter.prototype.setupChoices);

Keep in mind that while those commands will configurate $gameMessage, they are not responsible for calling Window_Base.prototype.convertEscapeCharacters, so no interpreters will be executing commands when that function is run.


I don't know how exactly Yanflys self variables plugin works, but Aloes conditional choices seems to go a similar route, if you look at how it modifies Game_Interpreter.prototype.setupChoices towards the end.
Aloes conditional choices seems to go a similar route, if you look at how it modifies Game_Interpreter.prototype.setupChoices towards the end.
Yeah, that's what I noticed, so did a thing to pass the event ID along to where it was needed. Though, that only worked for hidden choices – disabled choices were evaluated from a window function. But I did get disabled choices working too, probably (haven't tested it).

Keep in mind that while those commands will configurate $gameMessage, they are not responsible for calling Window_Base.prototype.convertEscapeCharacters, so no interpreters will be executing commands when that function is run.
Oh, that's a good point… this makes me suspect that your approach may break if two different parallel events both request a message…
The code above is still incomplete.
Since $gameMessage can only hold one message at a time, the idea was that whichever interpreter sets the message could also set the event associated with it.

If an interpreter does a message command while another message is already showing ($gameMessage.isBusy), it would defer the command and try again on the next frame. You would have to be careful to not set an event either in that case, but in general this should still work.
I'm pretty sure multiple parallel events showing messages would sow havoc in the message log plugin I was working on, too… but I wouldn't expect it to be a very common scenario, so as long as it doesn't actively break things, it should be okay.

I already got the self switches working on parallel events in Aloe_ConditionalChoices. I think I probably have enough here to get the event name working in my other plugin as well.

Original thread: https://forums.rpgmakerweb.com/threads/getting-the-current-active-event-id-interpreter.178497/