In $gameVariables.setValue(20, 0) you are setting variable 20 to the value of 0.
Of course it will return 0 when you do a console log. Or am I missing something?
Completely intended. What I want is a default value of 0 so I can catch if the player cancelled the selection. It's the second console log that shouldn't return a 0, especially when the selection window is still active.
But I found a solution:
Initially, what I wanted was a weapon selection screen in combat, that would be triggered by a skill.
I had found a plugin that allowed weapon selection, but it only worked on the map.
So what I did first was create one Hidden Item A (the category I use for item selection) for each eligible weapon.
I'm well-organized in my database, so the code was:
JavaScript:
for (var wpnId = 11; wpnId <= 60; wpnId++) {
if ($gameParty._weapons[wpnId]) {
$gameParty.gainItem($dataItems[wpnId+150], 1);
}
$gameVariables.setValue(20, 0);
}
The eligible weapons are those with an ID between 11 and 60, their matching hidden items have their IDs spread between 161 and 210 (which is why the 3rd line says
wpnId+150).
If the party has one of these weapons (doesn't count any weapon that's equipped), it's added to the item selection.
At the end of the Apply Effect, variable 20 is set to 0, which will allow me to know if the user cancelled the selection.
This code went into a Custom Apply Effect for a state that's given to the user upon using the "change weapon" skill, which then calls a common event.
The common event handles the item selection, letting the user pick a Hidden Item A and storing it into variable number 20. This choice window will contain nothing but the eligible weapons in the party's inventory.
Then, I need to look for whoever in the party has the state given by the initial skill, and give them a second state.
JavaScript:
for(i=0; i<$gameParty.members().length; i++) {
if($gameParty.members()[i].isStateAffected(101)) {
$gameParty.members()[i].addState(102);
}
}
This triggers another Apply Effect:
JavaScript:
target.removeState(101);
if($gameVariables.value(20)==0) {
target.gainMp(1);
target.removeState(102);
} else {
target.changeEquip(0, $dataWeapons[$gameVariables.value(20)-150]);
}
for (var i = 161; i <= 215; i++) {
$gameParty.loseItem($dataItems[i], 1);
}
If variable 20 is equal to 0, that means the selection was cancelled and I'm refunding the cost. I'm also removing the state, because it also seals weapon switching and cancelling it should remove the restriction.
If the variable isn't 0, a selection was made, so I'm getting the matching weapon ID and i'm making the wearer of the state equip it.
No matter which one happens, I'm removing the Hidden Items A that were used for selection, so that the party doesn't have any left. I always clear them after this kind of selection.
That was a headache and a half, but I love getting complicated things to work.