[Bug] Crash on access of equipment data during gameplay

User: bblizzard
Bug: Crash on access of equipment data during gameplay.

How to Replicate the Bug:
  • Give an actor any initial equipment piece except the very first weapon (or armor). As example, let's say we set the weapon with ID 10 as initial equipment.
  • Change the maximum number of weapons (or armors) so that the initial equipment doesn't exist anymore. Following the initial example, let'c change the maximum to 9 (or lower).
  • In the database the actor's initial equipment will be displayed as "None" (which is correct), but the ID in actors.json will remain 10.
  • When running the game in this state and the method Game_Actor.equips is used somewhere, the game can crash (e.g. equipment menu, battle when calculating the paramPlus for actors, etc.). In our example when checking the contents of the result of the equips() call of the affected Game_Actor instance, the weapon will be "undefined" instead of "null" (all the other free equipment slots will have "null").
Other: There are multiple points in the code where the non-existent equipment item could be checked to prevent this issue from happening. It can either be done in the equips loop within Game_Actor.initEquips, it can be done in Gsme_Item.setEquip, it can be done in Game_Item.object or it can be done in the all the type-check calls of Game_item (isSkill, isItem, isWeapon and isArmor).

Alternatively the editor functionality could be changed so that it checks all actors' initial equipment when the weapon or armor maximum has been reduced and properly sets the IDs to 0.

It's possible that a similar issue is present with Skill Learnings in the Class tab of the database and reducing the maximum of Skills.
It's also possible that this is present with some other data where the maximum can be changed (e.g. elements or weapon/armor types).
To me, this feels like an editor bug. Having actors with equip ids that point into nothingness/past the valid array length seems like wrong data, and the corescript runtime code could get a lot more complicated if it has to assume everywhere the indexes could be invalid like that. So I feel like the editor's "Change Maximum..." button should have gone through all actors and wiped the references to this id in the JSON when resizing to prevent this invalid data ever being saved like this.
Should the editor also then go through your entire game and tell you all the places where you have used that equipment so you can go and change it?

This is not an editor bug. It is something you have specifically done that's caused an issue to happen. Surely if you have a weapon defined in slot 10, then you go to change the maximum to 5, you are going to see that you're going to lose a weapon and that might cause problems?

I've seen this happen often with enemies and troops - someone will add an enemy to a troop and then later on delete them from the enemies tab. That also causes a crash when in battle. It is not the editor's fault that you deleted something without updating the places where it was used.

In the above scenario, the correct process would be to change the initial equipment to use something other than the item you're going to delete, and then delete the item.
Interesting angle!

For what it's worth, don't find it as obvious as you say that the editor isn't supposed to naturally handle this. With event instructions I'd kind of expect it to remain in-place and crash, because to what is it even supposed to change those anyway - but it seems to me like it'd be easy enough to go through e.g. the other main database items directly referring to it and clean it out. And given how generally easy to use RPG Maker is, at least I personally would think this to be a nice addition to make it more foolproof. But of course it's not my decision, I hope I didn't make it sound like that.

It is not the editor's fault
I find that mostly relevant for pondering if this is actually a "feature request" (which I suppose is fair enough), but not that much beyond. The actually interesting point to me seems to be whether this would be useful enough to add in to have less confused users in the future, versus the effort to implement it. I personally think it could be, so now I'm wondering what the majority of other users would think on this and possibly the devs.
Yeah, I'd consider it more of a feature request than a bug. It's a situation that you caused by an action you took. If you think the editor should go through and "clean up" other database tabs, what should it do with them? Set them to nothing? Set them to a default? And why should it then not check the rest of your project? But if it DID, you'd end up with delays every time you deleted something from a slot or reduced the maximum number, while it did its checks. There are a lot of things you can do to cause your game to crash. I'm just not convinced it's the editor's job to detect them all and warn you or stop you from doing them. The designer has to accept some responsibility and in some cases do things in a certain order to make sure they're done properly.
Your point on whether it should clear them or use them default made me wonder: maybe a warning after the reduction of slots after doing a check for just the obvious direct references from other database items would be good? (Like, a warning that lists the orphaned item references.) Otherwise, I think just setting them to none would be the most intuitive. In any case, I think anything would be an improvement really.

As for scanning the entirety of a project, you already pointed out that it could take a long time - and I'm a pragmatist: most actions in the editor already don't easily cause crashes, so if for this one it checks "just" the obvious stuff and warns about it, then there's even less accidentally crash-causing actions which I think is a potential win. I don't think aiming for perfect safety is practical, but I think it could maybe do a little more in this case. Does that make any sense? I hope I explained it well enough.
Yeah, it does. But if it were implemented that way, people would still complain that it only does a half-baked job 😀
I mean, complaints would happen in any case, true. I just feel like here it might be easy enough to avoid some of the possibly user-unexpected crashes, so that seemed useful to me - I see a usability improvement -> I (often) like, I'm a simple person I guess :LZYpraise:
@Shaz Nope, it's definitely a bug. It displays "None" which implies ID 0, but in the data it remains the previous ID. Either it should display something else (maybe "Deleted" or "Out of range"?) or set the ID to 0 because they are not the same things from an engine perspective. Either that or the default scripts should be changed so that it actually is regarded as "None" and stops crashing (as I suggested in the initial post). Let me put it this way: I used MV to check the data and MV didn't display is properly. I was only able to figure out the issue by opening the JSON file manually and looking at it. That means it is MV's bug and not human error. (I did check the initial equipment after all!)

You said it's not MV's job to ensure data consistency. While I partially do agree, MV should at least make an attempt at that. Most developer software has a lot of safe-guards and data consistency checks implemented. RM is a tool that helps you develop your game and pointing out these kind of issues during the development process is a help. Of course I don't think MV should go through all database entries, maps and events and check for consistency each time the maximum size is changed. xD HOWEVER, as a separate feature request, having the ability to check the entire game for consistency would be nice. (IIRC MV already has a feature that checks which resources are used anywhere, right? Shouldn't be too hard to implement.) But that's slightly off-topic.

Original thread: https://forums.rpgmakerweb.com/threads/bug-crash-on-access-of-equipment-data-during-gameplay.119724/