Question about "this" keyword usage in Script Call

Hello, I am Raith and this is my first time joining and posting a question in RPGMakerWeb despite of almost a decade digging in this forum.

I am trying to create a crop-planting system similar to one found in Pokemon Ruby/Sapphire/Emerald, and I am trying to use regular event Script Call without any plugin help. So far, the code below is working - choosing what to plant, creating a dynamic choice message according to what seed you have, reducing the amount of seed in posession, activating self-switch for the next page etc.

However, I seemed to have bad experience on "this" keyword usage in the Script Call and desperately need your help.

The "this" problem is first noted when I use $gameSelfSwitches.setValue([this._mapId, this._eventId, "A"], true) for the choice response. That code is not working. Instead, the array must be stored first into a variable before finally working nicely
The second "this" problem is when I use "this" to declare "this._selectedCrop" variable. That code is working, the value is even passable to the second page after any switch is activated (which is great). However, that value is unchangeable in the choice response codes, which created headache for me.

Below is my code:

Code:
//The working variables
var crop_1 = $dataItems[13];
var crop_2 = $dataItems[14];
var cancel = "Cancel"

//the problematic variables
var identifier = [this._mapId, this._eventId, "A"];
this._selectedCrop = 0;

//Creating the array of choice for plantable seed (working nicely)
var crops = [cancel];
 if ($gameParty.hasItem($dataItems[13]) >= 1) {crops.unshift(crop_1.name)};
 if ($gameParty.hasItem($dataItems[14]) >= 1) {crops.unshift(crop_2.name)};

//Creating the choice message (working nicely)
$gameMessage.setChoices(crops, crops.length - 1, crops.length - 1);

//Creating the choice response
$gameMessage.setChoiceCallback(function(responseIndex){
  if(responseIndex == crops.indexOf(crop_1.name)) {this._selectedCrop = 1; $gameParty.loseItem(crop_1, 1, false); $gameSelfSwitches.setValue(identifier, true);};
  if(responseIndex == crops.indexOf(crop_2.name)) {this._selectedCrop = 2; $gameParty.loseItem(crop_2, 1, false); $gameSelfSwitches.setValue(identifier, true);};
});
this.setWaitMode('message');

Oh JavaScript masters, please enlighten me!
From what I understand you're doing this in a script call box, in an event.
The only way the this keyword will point to the Event (like for ._eventId) is if you use it in a script box, inside of a Move Route command.
Otherwise, if you're in an event and use the Event Command Script Box, if you use this like it's used in your code, it will refer to the Window object.

To get the current map id, use $gameMap._mapId
To get the current event, it's probably easier to go into a move route, select 'This Event' from the dropdown, and in a script box INSIDE of the move route, put (for example)
$gameVariables.setValue(1, this._eventId)

Anyway, to sum up, using this in a regular Script Call Event Command will point to the Window Object, not the event.
Refer to the Move Route technique mentioned above to have this target the current event calling it.

EDIT: Just to be clear, when using the Move Route Technique, this will get scoped to whatever event you select in the dropdown menu on the move route, not necessarily the event that's running the move route
The this will not point to an event, but actually to its interpreter. It is possible to target an event through $gameMap.event(this._eventId) inside of the script command.

Nevertheless, this is a keyword that means current object. "this dog has 4 legs" means a concrete dog and in the same sense you use this. If you want to learn more, I'd recommend you to take a free online C++ tutorial, since C++ tutorials explain how object oriented programming works and the concept is the same everywhere.
I don't think I'm understanding.....what are you hoping to refer to here with the this keyword?
From what I understand you're doing this in a script call box, in an event.
The only way the this keyword will point to the Event (like for ._eventId) is if you use it in a script box, inside of a Move Route command.
Otherwise, if you're in an event and use the Event Command Script Box, if you use this like it's used in your code, it will refer to the Window object.

To get the current map id, use $gameMap._mapId
To get the current event, it's probably easier to go into a move route, select 'This Event' from the dropdown, and in a script box INSIDE of the move route, put (for example)
$gameVariables.setValue(1, this._eventId)

Anyway, to sum up, using this in a regular Script Call Event Command will point to the Window Object, not the event.
Refer to the Move Route technique mentioned above to have this target the current event calling it.

EDIT: Just to be clear, when using the Move Route Technique, this will get scoped to whatever event you select in the dropdown menu on the move route, not necessarily the event that's running the move route

Ah I see, so an event is divided into Window Object and the local "Move Route" part, with two different thises.

That solved the first problem! Finally event page is updated automatically when the crop reached certain age. Thank you!

However, it can't solve the second problem, though. The second problem relate to the most important part to keep the event interaction normal (e.g event message for plant age info, plant species info, etc.). Putting it all into move route script sounds kind of... mundane.

So how do you change the this' variable value for Window Object? I mean the this in this line:

Code:
if(responseIndex == crops.indexOf(crop_1.name)) {this._selectedCrop = 1; $gameParty.loseItem(crop_1, 1, false); $gameSelfSwitches.setValue(identifier, true);};

It seemed that every copied event behave independently when I use the that this code, and that this-related value is passable to the next event page.
Actually, I just learned something from Poryg. Or rather, re-realized something. Yeah, if you use, in a regular script box
$gameMap.event(this._eventId) //this will target the event that this script box is being written in.

then, because of encapsulation, the scope of this get's changed to the event that this script box is being written in.

So
$gameMap.event(this._eventId)._eventId //this will get you the Event Id. I know it looks weird.
$gameMap.event(this._eventId)._selectedCrop //I don't know where this property is coming from, but I assume you're attaching it to the event.

I'm not sure where all of your properties are coming from, and I'm actually wondering where the code you showed in your original post comes from? Is that inside of your own JavaScript file?
I ask because the code you provided is too many lines to fit in a single Event Script Call box, and variables created inside of one are terminated at the end of the call.
(so if I create a variable in one script box, it won't be available in another script box, even if it comes right after)
The this will not point to an event, but actually to its interpreter. It is possible to target an event through $gameMap.event(this._eventId) inside of the script command.

Man, you enlightened me. Never know about this. Thank you!

Actually, I just learned something from Poryg. Or rather, re-realized something. Yeah, if you use, in a regular script box
$gameMap.event(this._eventId) //this will target the event that this script box is being written in.

then, because of encapsulation, the scope of this get's changed to the event that this script box is being written in.

So
$gameMap.event(this._eventId)._eventId //this will get you the Event Id. I know it looks weird.
$gameMap.event(this._eventId)._selectedCrop //I don't know where this property is coming from, but I assume you're attaching it to the event.

Gotta try that and inform you all if it is worked

I'm not sure where all of your properties are coming from, and I'm actually wondering where the code you showed in your original post comes from? Is that inside of your own JavaScript file?
I ask because the code you provided is too many lines to fit in a single Event Script Call box, and variables created inside of one are terminated at the end of the call.
(so if I create a variable in one script box, it won't be available in another script box, even if it comes right after)

Those properties is written from zero. Those lines of code is the "tidy, eye-friendly" one, used for sharing. The actual code pasted in the script box is the one that is not separated by any line break, so it is actually can fit the script call box (try it!). With that technique, I was able to implement some crazy ideas without using RMMV plugins (eg random party member recruiter, dynamic quest system, dynamic party dismisser, etc). Believe me, most of the complicated events I made for my game are come from such style of messy code writing.
Lol I know you can cram code into the script box, but I try only to use that when necessary, because it's just messy, and you don't want to have to work with or edit messy code.

You know it is actually much cleaner and easier to work with if you just put it in a JS file and enable it as a plugin. Plus you can alias/edit/add to game objects to add custom properties and handlers. It's much nicer, much more flexible and easier to edit/implement.
PLUS
You can edit and debug quickly by just saving documents and restarting the game. You can still do that with edits in the Maker but I feel it's much more unwieldy.

You seem against the idea of using plugins, why is that?
"this dog has 4 legs" means a concrete dog

more to the point, it means "this very dog, where I, flea, am standing on."
not "a" dog, but "the one I take my reference from"..... not "a" dog, because "that dog over there who I can't possibly reach" is also "a" dog, and it also has 4 legs.
You seem against the idea of using plugins, why is that?

Well, not really against it, just minimizing the risk of plugin conflict. I have a lot of active plugins right now, I'm avoiding to create/use new one unless I'm dealing with potentially complex code.

Updated the code here and there, added the new one in the move route script call. Still used the Game Interpreter's this without problem. The coded plant is now growing and finally harvestable.

However, I still can't make this part work. I wonder why:

if(responseIndex == crops.indexOf(crop_1.name)) {this._selectedCrop = 1; $gameParty.loseItem(crop_1, 1, false); $gameSelfSwitches.setValue(identifier, true);};

Unless I want to end up with one species of plant for the entire game, I need to figure it out.
I'm still assuming the ._selectedCrop property was created and attached to the event.
Change that bold line to
$gameMap.event(this._eventId)._selectedCrop = 1

See if that works
I'm still assuming the ._selectedCrop property was created and attached to the event.
Change that bold line to
$gameMap.event(this._eventId)._selectedCrop = 1

See if that works

Unfortunately it does not work. It return "undefined".
If I delete that, the event work again without problem.

It must be noted that, the default properties declaration of $gameMap.event(this._eventId)._selectedCrop = 0; in the beginning of the code is still recognized and maintained by the next event page. The same goes for this._selectedCrop = 0;. So if I assign the _selectedCrop value as "Strawberry", the next event page will recognize the planted seed as "Strawberry".

It seemed only every this in the setChoiceCallback's if statement that is not working. Here's the code:

$gameMessage.setChoiceCallback(function(responseIndex){
if(responseIndex = 'insert array index here') {'insert this-related response here'
});

the this, when placed in that code, probably has entirely different meaning. The this is not the dog again, but the this become a cat, I guess.
when I use "this" to declare "this._selectedCrop" variable

why are you asking the meaning of "this", right after using it to declare a variable, and acknowledging it working correctly?
how can you identify the correct functionality then? 0o

specifically..... why are you using a key word without knowing how to use it?
I gotta be honest, I really don't have a clear idea of what it is you're trying to accomplish, overall. Remember, no one knows what your project is or looks like. Even though I've asked a few times, I still have absolutely no real idea where the ._selectedCrop is supposed to belong to.

I haven't played these Pokemon games, so I have nothing to relate it to.

But I'm gonna tell you this.
Until you stop doing all of your scripting in Event Script boxes, you're always going to run into problems that are difficult to help you solve.
Until you stop doing all of your scripting in Event Script boxes, just stop using the this keyword, you're going to keep running into scope issues.
Right now, you could very well be successfully assigning something and checking it in the wrong location, such as on the Window.

I understand you've gotten stuff to work this way, but it's not as effective as doing it as a plugin, and it' gonna create problems.
the this, when placed in that code, probably has entirely different meaning.
of course.

here is another way to explain "this":
This is not a keyword, but a context-sensitive placeholder. you never change "this" by yourself, but if you move to a different context in the code then "this" automatically changes to the object of the new context.

"This" is basically used for inheritance and for shortcuts - so that a method knows to always switch to the current object no matter where it was called or inherited, or to prevent the programmer from writing the entire object structure that he wants to reference from the inside.

And that is why you can't simply place "this" whereever you want - you need to understand the context and what will be replaced by this before you can use it.
of course.

here is another way to explain "this":
This is not a keyword, but a context-sensitive placeholder. you never change "this" by yourself, but if you move to a different context in the code then "this" automatically changes to the object of the new context.

"This" is basically used for inheritance and for shortcuts - so that a method knows to always switch to the current object no matter where it was called or inherited, or to prevent the programmer from writing the entire object structure that he wants to reference from the inside.

And that is why you can't simply place "this" whereever you want - you need to understand the context and what will be replaced by this before you can use it.

Thank you all! your explanation confirmed my suspicion. Guess I must find another way than using this to make my code work.
I declare this problem solved.

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/question-about-this-keyword-usage-in-script-call.114656/