MZ Help with running an erase event script call from a plugin command

Hi folks,

I have a custom plugin set up to add some commands to streamline certain things. It is mostly working fine, but I am having an issue with getting any "erase event" script call to work when run within the plugin.

Here is my code for the command:
JavaScript:
PluginManager.registerCommand('SpawnEventCommands', 'EventEraseSelf', (args) => {
    const eventId = this._eventId;
    Ritter.unspawnEvent(eventId, true);
    $gameMap.event(eventId).erase();
});

This runs a plugin command from an event spawner plugin to delete the spawned event from memory, but I still need to also erase the event normally after that to fully remove the event.

Getting this._eventId is working, and using that in the ritter script call works fine, but using $gameMap.event(eventId).erase(); gives a crash (Cannot read property 'erase' of undefined). I have also tried it with this.erase(); but that doesn't seem to work at all when run from a plugin command.

Changing the order of these script calls doesn't fix the problem, so it is not an issue of the ritter script erasing the event before the 'erase' script is run. I get the same crash either way, and when only running the 'erase' command and not the ritter one.

Is there some way I can run a "erase event" script within a plugin command to erase the event which is running said command?

Thanks!
Hello,

There shouldn't be any need to run an erase event script call before or after Ritter._unspawnEvent, as the function handles removing the event internally. Is there a reason you're trying to erase the event manually? Normally, having a null event in memory won't cause issues, as long as you don't have thousands of them on the map at once, which the spawner is designed to prevent.

All unspawned events are recycled using object pooling logic, so it's generally best to avoid manually erasing spawned events.

If you're experiencing any problems after unspawning an event, I'd be happy to take a closer look.
Hello,

There shouldn't be any need to run an erase event script call before or after Ritter._unspawnEvent, as the function handles removing the event internally. Is there a reason you're trying to erase the event manually? Normally, having a null event in memory won't cause issues, as long as you don't have thousands of them on the map at once, which the spawner is designed to prevent.

All unspawned events are recycled using object pooling logic, so it's generally best to avoid manually erasing spawned events.

If you're experiencing any problems after unspawning an event, I'd be happy to take a closer look.
Hmm, that's odd - I though it would work that way at first, but when I just run Ritter.unspawnEvent(this._eventId, true); it doesn't erase the event, it just erases the "saved" info. I have to run an erase event command as well otherwise the spawned event just stays there.

The command is successfully stopping the event from respawning if I reload the map, but isn't erasing the event itself.

When I run the Ritter.unspawnAll(); script, that does erase events and remove the save info, however.
Arrow functions do not have their own context (this).

Define the plugin command as a full function instead: function(args) {.
Arrow functions do not have their own context (this).

Define the plugin command as a full function instead: function(args) {.
Hmm, I am a bit confused. The this._eventId; is working, and is returning the correct ID. When used in that first script call (the ritter one), it works just as though I ran it as a script call in the event itself (getting the correct ID and using it in the script call). It's just the 2nd script call that isn't working.
The only way "this" is receiving value is if you are within a scope that gave it context, meaning, you are most likely using it inside a script call (or there is another layer of scope of code that you are not showing us).

I believe this was why caethyril was letting you know that arrow functions do not have any context for "this".

Can you show us a screenshot of where this code is (is it in a plugin, or is it in a script call)?

In any case, try running a script call separately of this code in the event to see if Ritter's plugin is working properly:
JavaScript:
Ritter.unspawnEvent(this._eventId, true);
$gameMap.refresh();

Edit: not sure if the .refresh() is needed so test with and without it.
The only way "this" is receiving value is if you are within a scope that gave it context, meaning, you are most likely using it inside a script call (or there is another layer of scope of code that you are not showing us).

I believe this was why caethyril was letting you know that arrow functions do not have any context for "this".

Can you show us a screenshot of where this code is (is it in a plugin, or is it in a script call)?

In any case, try running a script call separately of this code in the event to see if Ritter's plugin is working properly:
JavaScript:
Ritter.unspawnEvent(this._eventId, true);
$gameMap.refresh();
Ahh, I see now. I thought the script call was working in my plugin command, but it wasn't. That was why I thought I needed to add the erase event script call as well. Running it in the event is erasing the event properly now.

Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/help-with-running-an-erase-event-script-call-from-a-plugin-command.180982/