MV setHandler method to use an Item?

Hi, all!

First of all, I'm not sure if this is the right place to post this, I apologize if not.

I need help with a custom Window_Selectable I'm trying to make.

So basically, I want to make a List of Cards the Player has in his Inventory. And of course the Player should be able to select a Card and add that Card to the Deck or remove it.

I thought that the adding and removing should be done by the cards themself, by calling a common Event with a Script in it.

And here is the problem: How can I actually select and use the Item?

I'm a complete Noob when it comes to programming, so I just followed Poryg's RMMV javascript tutorial a bit and came up with this:



function Scene_DeckBuilder() { this.initialize.apply(this, arguments); } Scene_DeckBuilder.prototype = Object.create(Scene_MenuBase.prototype); Scene_DeckBuilder.prototype.constructor = Scene_DeckBuilder; Scene_DeckBuilder.prototype.initialize = function() { Scene_MenuBase.prototype.initialize.call(this); }; Scene_DeckBuilder.prototype.create = function () { Scene_MenuBase.prototype.create.call(this); this._CardListWindow = new Window_CardList(0, 0, 320, 600); this._CardListWindow.select(0); this._CardListWindow.activate(); this.addWindow(this._CardListWindow); } Scene_DeckBuilder.prototype.update = function () { if (Input.isTriggered("cancel")) SceneManager.pop(); } function Window_CardList () { this.initialize.apply(this, arguments); } Window_CardList.prototype = Object.create(Window_ItemList.prototype); Window_CardList.prototype.constructor = Window_CardList; Window_CardList.prototype.initialize = function (x, y, width, height) { Window_ItemList.prototype.initialize.call(this, x, y, width, height); this._category = 'none'; this._data = searchForTypeId($gameParty.items(), 3); console.log(this._data); this.drawAllItems(); } Window_CardList.prototype.maxCols = function() { return 1; }; function searchForTypeId(itemList, typeId){ var resultList = []; for(var i=0; i<itemList.length; ++i){ if(itemList[I].itypeId == typeId){ resultList.push(itemList[I]); } } return resultList; } //end


And up until now it works like its intended (it lists all Hidden Items A the Player has):


1617458484616.png

The "selection" (or however the blue rectangle behind Card1 is called) is not controllable and doesn't blink.


I know that I have to setup a Handler that determines what happens when "ok" is pressed

(this._CardListWindow.setHandler("ok", method)

But exactly this method is what I'm struggling with. It somehow has to get the ID of the selected Item and use it or at least executes its effects. (also I'm not sure if a Handler solves the problem that the selection is not blinking).

Maybe someone can help me with this problem, I appreciate any help I can get.
Thank you in advance!
Your code formatting's gone a bit awry because you had a forum italics code in there but didn't surround it with code tags. Can you fix that so I can get a full picture of what you've done so far?
Oh, I'm sorry for that. It should be fixed now.
Thanks for the tip!
Okay, so first things first.

The reason your cursor doesn't move is that you've completely overwritten the update function. This means that the "parent" update function from Window_Selectable isn't running, and that's the one that handles input and moving the cursor.

After your if statement that pops the scene, put

JavaScript:
Window_Selectable.prototype.update.call(this);
Thanks for the reply! I put your line after the if statement but now I get an error:
"this.updateTone is not a function"
I'm probably missing a bit, hang on.

OH, sorry. That update function is for the scene, not the window. You need to call Scene_MenuBase.prototype.update(this);
Ok, thanks! Now the Selection works.
Okay, so now the handler. Let's look at how the item menu does it:

The createItemWindow function has

JavaScript:
this._itemWindow.setHandler("ok", this.onItemOk.bind(this));
this._itemWindow.setHandler("cancel", this.onItemCancel.bind(this));

What this does is binds the "onItemOk" function and the "onItemCancel" function to the scene's item window, triggered by the OK and cancel buttons respectively.

Then if you look at those functions:

JavaScript:
Scene_Item.prototype.onItemOk = function() {
    $gameParty.setLastItem(this.item());
    this.determineItem();
};

Scene_Item.prototype.onItemCancel = function() {
    if (this._categoryWindow.needsSelection()) {
        this._itemWindow.deselect();
        this._categoryWindow.activate();
    } else {
        this.popScene();
    }
};

So if you hit the OK key while the item window is active, it called onItemOk, which stores the currently selected item in $gameParty.setLastItem() and then called the determineItem function. For the purposes of explaining what you need to do, we don't need to delve further than that.

If you hit cancel, the other stuff happens. Basically it deselects the item window and activates the category window, unless the flag for needing to select a category is false, in which case it just pops the scene.

So how does that relate to your thing? Well _CardListWindow is your plugin's equivalent of _itemWindow, so you need to add the handler to that:

JavaScript:
this._CardListWindow.setHandler('ok', this.onCardOk.bind(this));

Then you create that function, which contains the code that will execute when the player selects a card.
I added your last line and also copied the function from the Scene_Item:

JavaScript:
Scene_DeckBuilder.prototype.onCardOk = function() {
    $gameParty.setLastItem(this.item());
    this.determineItem();
}

now the error "this.item is not a function" comes up.
Did I miss something, or do I have to alter anything?

Also, big thanks, you already helped me a lot!
Yeah, you're missing that you can only call a function in an object if it exists. This works in Scene_Item because its prototype class, Scene_ItemBase, has one:

JavaScript:
Scene_ItemBase.prototype.item = function() {
    return this._itemWindow.item();
};

If you want this to work in your scene, you'll have to code a function that does a similar thing.

(alternatively, you could make the prototype object of your scene Scene_ItemBase instead of Scene_MenuBase and rename _CardSelectWindow to _itemWindow and it'll work)
Ok, I tried a bit around with coding a similar line, but to no avail. I think I have to learn a bit more about javascript to do that.
So I tried your alternative and replaced all instances of "Scene_MenuBase" with "Scene_ItemBase" and renamed _CardListWindow to _itemWindow.

But now an error comes up: "Cannot read property "isActor" of null".

If that error is a bit more complicated to solve, I wouldn't want to take too much of your time and
call it a day. maybe I come up with something when I dove deeper into javascript.

You helped me a lot, thanks!
It's probably that the item scene also has an "actor" function that yours doesn't.
I stumbled upon something that might work out better:
instead of substituting "Scene_Menubase" with "Scene_ItemBase" I replaced it with "Scene_Item".
Now I can actually use the Item, but in the Background the normal Item Scene with its Windows is there.
If I can get rid of that I should be good to go, right?

Also I have to get rid of an extra step, because after the selection of a card, the game wants me to select a category ( KeyItems, Weapons, ...)
Yeah, if you're doing that you'd have to overwrite the functions from Scene_Item which create windows.
Moved to: Learning Javascript

Original thread: https://forums.rpgmakerweb.com/threads/sethandler-method-to-use-an-item.135113/