Using addState function won't work repeatedly

So here's an odd one, and I'm not sure if it's a bug in my code or a quirk of RPGM MV I don't understand.

I have a block of states called starsigns. They modify your stats and each actor and enemy gets one (and only one). I just have 3 right now and all they do is mess with your max HP (for testing purposes).

I've added a "Change Character" option to top of the menu (it'll be moved later). Choosing it and an actor brings you to this submenu:

menuProblem1.PNG


"Change Name" brings up the renaming window. "Change Appearance" calls up SomeRandomDude's character creator plugin. Both work as expected. "Change Starsign" brings you to this screen:

menuProblem2.PNG


I've aliased the Window_Selectable.prototype.select function like so:

Code:
var _Window_Selectable_select = Window_Selectable.prototype.select;
Window_MenuStarsign.prototype.select = function(index) {
    _Window_Selectable_select.call(this, index);

    tempActor = $gameParty._menuActorId;
    tempExt = index + firstSign;
    tempStarsignHelpWindow.setText($dataStates[tempExt].description);

    for (i = firstSign; i <= lastSign; i++) {
        $gameActors.actor(tempActor).removeState(i);
    }

    if ((tempExt <= lastSign) && (tempExt >= firstSign))
        {$gameActors.actor(tempActor).addState(tempExt);
        //alert(tempExt);
    }

    tempCharacterPage.refresh();
};

tempCharacterPage is the smaller status window in that last screenshot. The idea is that each time the player moves the menu cursor his previous starsign is immediately removed (by the for loop) and whatever one the cursor is over gets assigned. Then the character page is refreshed so he can immediately see what his stats will look like if he makes that choice.

Here's where it gets weird. This works, but only once per sign per character. Harold gets the water sign the first time I move the cursor to that option, but if I move it away and then back he's left with nothing. I have to restart the entire game to give it to him again. Even stranger is that the addState function is the only part that fails after the first time; the help window description always gets updated and the removeState loop always works. I've tried variations on the code, even going through the updateCursor function instead of select, and I always get the same results.

So, any ideas what's going on? I'm trying to attach a demo project but the forum is claiming the file size is too much. If it's not obvious what I'm doing wrong from what I've said so far I'll look for a place to host it.
You don't need a demo unless someone asks you for one. So save yourself the time.

You've got an alert in there that you've commented out. What happens when you leave the comment in? If it only shows once then not again, move it up above the test to see what it is before going into the test.

I doubt it'd make much of a difference, but does the { need to be on the same line as the conditions?
You've got an alert in there that you've commented out. What happens when you leave the comment in?

It triggers every time I move the cursor and reports the correct value. So the if clause is working properly.

Also, it tells me that when the menu first opens the first option gets selected twice for some reason. But that won't be a problem if I can get addState to work.

I doubt it'd make much of a difference, but does the { need to be on the same line as the conditions?

No, that just got moved there accidentally as I was deleting some commented-out stuff.
have you got any other plugins that change the way states behave?
Now that you mention it, yes. Yanfly's In-Battle Status is what allows the states to have help descriptions. Other plugins in use are:

Yanfly's Core Engine (currently increasing the game window resolution)
Yanfly's Battle Engine Core (required for the In-Battle Status plugin)
Yanfly's Main Menu Manager (this is how I added the character creation submenu)

I haven't added SomeRandomDude's character creator to the demo yet and I'm still having the problem, so that's clearly not a factor.
If you disable all of those, does it work?
I just disabled everything but the menu manager (still need it to call the submenu) and, no, I'm getting the same result. I think I know enough now to add the option to the main menu myself so I can get rid of the manager as well in a few minutes, but I don't see how that one would be affecting states.
Maybe (and it's quite a big maybe, but worth a try) the state is being added but the window just isn't refreshing? Just after the addState function, try putting an if statement to check if the state is added and an alert inside the statement so that you'll be notified if the state is added.
Nope, I checked.

In other news: RL stuff kept me from doing this earlier, but I've changed the plugin to not need the menu manager anymore. As I expected, it didn't help. Even with my plugin running alone in a fresh project I have the same problem. As there's nothing in my plugin that changes the addState function or the basic nature of states I'm seriously starting to think that this is an RPGM bug.

I'm attaching the full plugin if anyone wants to take a look. It's still in a work-in-progress state, though.

It's easy to set up if you want to actually play with it. First, start a fresh project with it as the only plugin. Next, pick a small, continuous block of states and make a status effect in each one, like so:

menuProblem3.PNG


I suggest giving each status an obvious effect (like changing Max HP) and/or a unique icon for playtesting purposes. Finally, set the "firstSign" and "lastSign" parameters to tell the plugin where the starsigns begin and end in your state list (52 and 54 in the above example).

And that's it. From there you just start the game, open the menu, and select the new "Change Character" option at the top.

A few notes:

1) The "Change Appearance" option depends on another plugin so don't bother with it.

2) All actors get the first starsign by default at the start of the game.

3) On opening the starsign menu the first option is immediately selected twice. Given the nature of the bug this means you'll lose your sign immediately.

4) The current option also gets selected again when you confirm or cancel. This means you can't keep your sign when you leave the menu. Yeah, the menu's pretty much a total bust right now.

5) Each enemy gets a random starsign at the start of battle, and I'm pretty sure THIS part works correctly. The name of the sign is appended to the enemy's name so you know who has what.
Okay, after rooting around in the rpg_objects code for a bit I found a solution. Using the addNewState function instead of addState works. I don't know why it works - and I'm open to explanations - but I'm tired of banging my head against this particular wall right now so I'm going to run with it unless someone gives me a good reason not to.

Thanks to anyone who worked on this.

Original thread: https://forums.rpgmakerweb.com/threads/using-addstate-function-wont-work-repeatedly.79983/