New account registration is temporarily disabled.

[RMMV] CLASS VARIABLE VALUES RESET TO UNDEFINED UPON SCENE CHANGE

Posts

Pages: 1
I've encapsulated my plugin (mostly) in a class, and in its constructor, I have this:

this._maps =                        ['503'];

console.log("Maps: " + this._maps);


That code runs just fine as soon as the title screen is booted up; the developer tools window logs "Maps: 503" as it should. But as soon as I start a new game, apparently that variable just resets itself to undefined; when I log it in my aliased isMenuEnabled function, the dev tools window logs "Maps: undefined".

There is literally nothing in that plugin that sets that variable to undefined.

I'm confused. Could someone explain to me why this happened? If it's normal behavior in RMMV, how do I avoid my class variables being reset when the scene changes? This resetting would make sense if the variables were pointing to things that RMMV itself manages, but yeah.

Marrend
Guardian of the Description Thread
21806
Do you use the "this" pointer both times you try to call the "_maps" variable? If so, under what conditions is it used?

You must realize that use of the "this" pointer is similar to using "self" in VX/Ace. So, it's trying to call an object of whatever class it's currently in. Therefore, you could be calling, for example, $Scene_Title._maps (or what-have-you) in the first instance, whereas the other instance might be calling, for example, $Game_Interpretor._maps (or what-have-you).

When in doubt, see what's calling the "._maps" variable, and make sure it's the same object that way. So, maybe a little something like...

this._maps = ['503'];
console.log("This: " + this);
console.log("Maps: " + this._maps);


...for the first instance, and the second instance might look something like...

console.log("This: " + this);
console.log("Maps: " + this._maps);

Yes, I used the "this" pointer in both instances. And I know what it's for; I have a fair foundation in object-oriented computer programming xD

First, the pointer was used when the variable was initialized in the object constructor. Then, as I mentioned before, it is called in the class' isMenuEnabled function.

All this should be happening within the same object; I only instantiated one in my script.
Marrend
Guardian of the Description Thread
21806
Hrm. At the moment, the only thing that is coming to mind is a possible scope issue. Is _maps only ever called through the "this" pointer (at which point, I have no clue, sorry I couldn't be of more help), or is there a call to <class_object>._maps somewhere where it might not be "seeing" the variable you actually want to reference?
Well, the two uses of this._maps are in different scopes; one in the constructor (called first, of course), the other in the _isMenuEnabled function. Since both functions are in the same class, I thought I could access _maps in all of the class' other functions called after the constructor.

And I confirmed it's the same object the way you said; in both instances, it logged the object as "Object object" (surrounded by staple brackets).

Maybe I misunderstood how that last part works. If I can't just initialize a variable in the constructor so I can use it in any other part of the class, then how do I make it so? This isn't like C#, where I can just initialize a variable in the class' highest-level scope.

Pages: 1