this._categoryWindow._index does not work

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I was trying to access the item category window (Window_ItemCategory) index and it seems not to work. I am making a new function where when I select a button, it handles a function that has this block:


this._categoryWindow._index = 1;


It should work, but I don't know why it doesn't return the index to 1 when it's on somewhere. For example, we have Items, Weapons, Armors and Key Items. We know they are 0, 1, 2 and 3. When I click the button and the selection is on Key items, and click the button that says the index should be on 1, it doesn't return it to Weapons. It just doesn't set the index.
 

sagebrushfire

Villager
Member
Joined
Jun 12, 2016
Messages
25
Reaction score
12
First Language
English
Primarily Uses
Most likely it's because of the this keyword you have at the beginning. 


JavaScript has this thing called scope. It's honestly kind of hard to understand and even harder to explain but the quick and dirty is that the scope is literally exactly what it sounds like: it's the scope of your current piece of code. It tells you what the this keyword will mean. 


Take a look at this code from one of the RMMV core files: 


Scene_Item.prototype.createCategoryWindow = function() {
this._categoryWindow = new Window_ItemCategory();
this._categoryWindow.setHelpWindow(this._helpWindow);
this._categoryWindow.y = this._helpWindow.height;
this._categoryWindow.setHandler('ok', this.onCategoryOk.bind(this));
this._categoryWindow.setHandler('cancel', this.popScene.bind(this));
this.addWindow(this._categoryWindow);
};


This is using javaScript's prototype keyword as well, which I won't even begin to try and explain. It suffices to say that we have this object called Scene_Item and it's a function. We are modifying the core components of that function. In JavaScript, functions are objects too and that means they can have properties. 


This function is intended to be used with the new keyword, so it's considered a constructor function. You would use it like this: 


var MyVar = new Scene_Item();


Once you do that, all of those statements inside the function will run and they are basically creating properties inside of the new instance of that function you made. While we are modifying a specific function that belongs to Scene_Item (the createCategoryWindow() function), the this keyword is directly referencing the Scene_Item object, and more specifically it is referencing the instance of that object. When I created the MyVar variable and assigned it as a "new" Scene_Item, I created an instance of the Scene_Item object, which is what most other languages would call a class. 


You could then access one of the properties of your new instance like this: 


var test = MyVar._categoryWindow; // Assign the category window object from MyVar to a new variable called test.


As I mentioned, the this keyword refers to the new object that the constructor created. This is one of many ways to construct new objects in JavaScript. It's also one of the messiest and hardest to understand, which is why I generally avoid it. 


Outside of the construction of this new object, the this keyword has an entirely different meaning. Once you've created a Scene_Item object, you no longer would use it. If you wanted to change the index property of a Scene_Object's category window, you would need to access that scene object. Using my example, I created a new scene object called MyVar so I would change it like this: 


MyVar._categoryWindow.index = 1;




Do you see what I mean? Unless you're actually changing a line of code inside of one of those RMMV core scripts because you do want to change how the constructor works, you would need to access the scene object that was created and probably not using the this keyword. 


More Detail


Given no context at all, this will be the same as the window object (the mother of all objects, parent of everything).


console.log(this); // In most browser consoles this will display the window object & all it's details.




When used inside of a function, this will refer to the object that houses that function, like so: 


var MyNewObject = {};

MyNewObject.name = "Sam";

MyNewObject.greetings= function(){
console.log("My Name is" + this.name);
};

MyNewObject.greetings(); // prints "My Name is Sam" in the console. 




When used in a constructor function with the new keyword, this will refer to the new object that is being created: 


var CoolObject = function(nameInput){

this.name = nameInput;

this.greeting = function(){
console.log("My name is " + this.name);
};
};

var MyNewObject = new CoolObject("Sally");

MyNewObject.greeting(); // Logs "My name is Sally"


Because that is a constructor function, those properties do not just inherently exist inside of it. You can't do CoolObject.name, it won't work. The name property is using the this keyword and will only exist when the function is run with the new keyword so that it creates a new object. 


Hopefully that helps solve your problem. Unless you were intentionally messing with the prototype constructors, you probably need to find the actual Scene_Object object that you're trying to modify to change its property. 
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,859
Messages
1,017,030
Members
137,566
Latest member
Fl0shVS
Top