MZ Object is not being assigned to a variable?

So... a simple thing is not working, and I'm not sure why.

Basically, I'm trying to grab a reference to a given Game_Interpreter object, for later use. In the function for executing a command, I stick a single line of code, to set a plugin variable to the current interpreter instance via the "this" keyword.

However, the variable is still "undefined" even after that line. There is no error, and as far as I can tell, nothing that should be preventing me from doing what I'm trying to do.

Odd thing is, the variable SHOULDN'T be undefined at all, I initialize it to an empty object at the very start of the plugin.

I would VERY much like to figure this out, since my plugin does need access to the currently running interpreter instance to do what it's designed to do without any extra requirements for use.

If any more detail is required, I'm grabbing the interpreter instance every time a command is to be executed, since the only time I actually need access to the interpreter is while running a plugin command. And unless I miss my guess, there is generally only one main interpreter active at a given time, and there is no "universal" one. This means I need to have a reference to the specific one currently executing the plugin command. I do intend to restrict the reference grab to ONLY when this plugin's commands are being ran, but for now I'm just trying to get the basic stuff working.

As always, any and all assistance is appreciated! Thanks in advance!
how should anyone be able to tell you what you did wrong, without seeing what you did?

please give the code you made, preferably in code-tags to preserve formatting, then someone can tell you where you went wrong.
MZ runs plugin commands in the context of the calling interpreter. If you register your plugin command as a function with a closure (i.e. not an arrow function) then you should be able to reference the interpreter in that function via this.

Otherwise, like Andar says, we'll need to see your code.
JavaScript:
const GI_exCmd = Game_Interpreter.prototype.executeCommand;
Game_Interpreter.prototype.executeCommand = function() {
    Drak.EvExt.interpreter = this;
    GI_exCmd.call(this);
}

I mean, that is it. One line of code. Singular, as explained. Not sure how seeing the code would help.

Sorry if I sound snippy... I explained that it was a single line of code, what it was, what it's supposed to do.... the code is exactly what I said it is. No more, no less. It's a stupidly simple thing.... that isn't working. I make stuff as stupidly simple as possible before finally asking for help, to make sure that I'm not getting in my own way when trying to do something.

The problem is that, even though I'm setting a variable to an object.... it is still nothing after the code runs. And I mean IMMEDIATELY after the code runs. I checked it between the line in question and the following alias call.
Looks like four lines of code to me, but OK. Ideas:
  1. Did you read my previous post? Does that solve your need for this workaround?

  2. From what you've said, I assume that Drak.EvExt.interpreter is returning the value undefined, rather than throwing an error because Drak.EvExt is undefined. Is that assumption correct?

  3. Are you sure the plugin file is loading? Do you see any errors in the console?

  4. Where are you checking and/or attempting to use the value? [Answered by your edit.] So the actual test case is this:
    JavaScript:
    const GI_exCmd = Game_Interpreter.prototype.executeCommand;
    Game_Interpreter.prototype.executeCommand = function() {
        Drak.EvExt.interpreter = this;
        console.log(Drak.EvExt.interpreter);  // > undefined
        GI_exCmd.call(this);
    }
    That suggests one of three possibilities to me:
    • Assignment is not sticking, e.g. due to an accessor or proxy;
    • The method is being invoked with this = undefined;
    • Something is deleting that property's value in the interim (not sure if that's possible).

  5. I assume interpreter is a data descriptor (value) rather than an accessor (get/set), correct? If so then I'm out of ideas for now.
Here, I tried to just execute the code in the console window (no need to put it as a plugin).
Screenshot_1397.jpg

I changed it to window.interpreter (so it can be accessed globally)

Then I created an event that basically does this.
Screenshot_1398.jpg

It works just fine, I guess?

Your problem must be something else.
Looks like four lines of code to me, but OK. Ideas:
  1. Did you read my previous post? Does that solve your need for this workaround?

  2. From what you've said, I assume that Drak.EvExt.interpreter is returning the value undefined, rather than throwing an error because Drak.EvExt is undefined. Is that assumption correct?

  3. Are you sure the plugin file is loading? Do you see any errors in the console?

  4. Where are you checking and/or attempting to use the value? [Answered by your edit.] So the actual test case is this:
    JavaScript:
    const GI_exCmd = Game_Interpreter.prototype.executeCommand;
    Game_Interpreter.prototype.executeCommand = function() {
        Drak.EvExt.interpreter = this;
        console.log(Drak.EvExt.interpreter);  // > undefined
        GI_exCmd.call(this);
    }
    That suggests one of three possibilities to me:
    • Assignment is not sticking, e.g. due to an accessor or proxy;
    • The method is being invoked with this = undefined;
    • Something is deleting that property's value in the interim (not sure if that's possible).

  5. I assume interpreter is a data descriptor (value) rather than an accessor (get/set), correct? If so then I'm out of ideas for now.
Five if you count the closing bracket on it's own line. And I included the rest because I know SOMEONE would complain if I didn't. The snark amused by dad-joke-loving self. lol

I did solve my problem, but I feel like going through the points and answering your questions anyway.

1. Did not try that, but with the identity of the culprit it wouldn't have helped.
2. Yes, the code was returning undefined, which is what was confusing me so much, since not so much as an error was thrown. Just "Set this to this" replied with "lol, no".
3. Considering I was stepping through the code and seeing that it was undefined, the plugin was obviously loading fine. (Couldn't help my own snark. lol)
4. That was part of my initial investigation, I just didn't feel the need to include my console.log tests, since they didn't really add anything to the clarification of the problem.
5. Yes, it is a data descriptor. Just a variable to store a chunk of data that I might need later. Nothing fancy.

The culprit was actually the Drak.EvExt part. There was a part that was overlooked during one of the additions to the plugin, where Drak.EvExt was being set as a variable value instead of a data object. Originally, that was all I needed, but when I needed a bit more data stored in between functions, I forgot that it was a variable, not an object. So it was attempting to set a property that didn't exist for that variable's type, instead of what I was laboring under the impression had been changed to its own object.

I kept not noticing it because I thought I had already made the relevant change and it was in a different section of the plugin that I wasn't looking in at the time, and absolutely nothing was being stated anywhere about trying to stick some data in something that doesn't exist. My code LOVES to throw a fit when I do something like that, so when it wasn't doing that this time I didn't think that was the culprit.

TL;DR - I was in my own way again, but this time because I hadn't done something I thought that I HAD done, and it didn't feel like throwing a fit this time, just ignoring me. lol
The culprit was actually the Drak.EvExt part. There was a part that was overlooked during one of the additions to the plugin, where Drak.EvExt was being set as a variable value instead of a data object.
Ah, it was a primitive rather than an object, that makes sense. Thanks for sharing the answer! 👋

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/object-is-not-being-assigned-to-a-variable.146522/