MZ (Possible engine bug!) $gameParty.lastItem() does not work as expected, and neither does Control Variables

Greetings, I was trying to achieve a pretty simple task (getting the item ID of the party's last used item via JS), and noticed a strange issue.

I was using the script call $gameParty.lastItem().itemId() ...this has been directly suggested in threads like this one, for example to achieve my goal.

However, it doesn't actually work. I tried it again in a brand new empty project with zero plugins just to be sure. Same result. And that made me start digging. It led me down a bit of a rabbit hole, which makes me believe I discovered an engine bug.

For those wanting to try it at home, this is what I tried:
  1. Open new project in MZ 1.10.0, install no plugins, change nothing about the database.
  2. Create a treasure chest on the default map that gives the player Item 23, the "HP Increase".
  3. Playtest, grab the item from the chest, use it on the party leader.
  4. Open the console and type $gameParty.lastItem().itemId()
  5. The console informs me that VM157:1 Uncaught TypeError: $gameParty.lastItem(...).itemId is not a function
  6. I used a different script call instead and try again, this time typing $gameParty.lastItem().id
  7. The console returns 23, as expected.
My test project was using Core Script version 1.9.0, so I updated it to 1.10.0, but the same happens as with Core Script 1.9.0 so it still works the exact same way in the latest engine version.

My curiosity then led me into MZ's default javascript files, and in rmmz_objects.js, I observed the following:
  • this._lastItem = new Game_Item(); in Game_Party creates a new Game_Item item object when setting its _lastItem property. It's important to note that this Game_Item is really just a reference to the database and doesn't contain anything but the item's _itemId and _dataClass. This isn't some kind of exception, it's handled this way across several situations when the game stores info about the last used anything, really.
  • It gets a little convoluted when the function setLastItem(item) is used by Scene_Item (and probably other situations) to assign the _lastItem to the party. _lastItem only stores such a Game_Item object.
  • On the other hand, the function lastItem() returns the object() of the Game_Item, a function that returns the database data of the _itemId stored in the Game_Item.
  • This explains why $gameParty.lastItem() doesn't have access to the function itemId(), because it's a function of the Game_Item object, which isn't what lastItem() returns (it returns the database info) despite what one might expect. To use itemId() this way, you'd have to call $gameParty._lastItem.itemId() instead.
So far, you could say that even if it's unintuitive, everything works as it is intended to work. But the issue arises when instead of using a script call to retrieve the last used item's ID, you decide to use the event editor. Let me explain:
  • When using the event editor to retrieve the last used item's ID into a variable, it gets the ID from $gameTemp.lastActionData(type) which contains an array of IDs sorted by type of the action (in this case, the "action" is the item that was used at index 1 of that array ([0, 23, 1, 0, 1, 0] in my testing example).
  • Some of you may have noticed the issue in this moment: $gameTemp does not persist between closing and opening the game or even just going to the title screen, even if you save and load the saved game.
  • $gameParty._lastItem.itemId() and $gameParty.lastItem().id on the other hand ARE savefile-persistent and will result in the correct ID being returned.
  • If your players found themselves in a situation where they use an item, then save and close the game, and after continuing their savefile, the game tries to make a decision based on their last used item but you used the event editor to get this value in an ID... in the moment it needs to check for it, this value would always be 0.
  • And thus, using the script call for retrieving the last used item's ID is the only reliable way to do it.
  • I tested this by adding an event to my default map that uses both methods to retrieve and tell me the ID, and only the method via the $gameParty script call gave me the correct result if I use the item, save the game, and then load back in before talking to that event.
Demonstration of what you would get after loading a saved game:
variable_test.JPG

variable_test2.JPG


I'm honestly not sure what to make of this other than feeling a little sorry for the person in that old thread from 2022 trying to do what they were told to do and still having an issue. I'm not blaming the people who tried to help in that thread either, because this is a pretty unintuitive situation. And if I hadn't looked into the itemId() situation, I don't think I would have been aware of how a pretty difficult to debug situation could arise from this discrepancy in how the ID is retrieved internally.

So...What now?
If anything, maybe anyone trying to google or search for this script call could come across this thread and save themselves some headaches in the future. Maybe creating a patch plugin that addresses the issue with the Control Variables command might be a good next step. Ideally, the next engine update would do something about it, though I've never had to report an (assumed) engine bug before so I'm not sure where and how I would have to report my findings.

Maybe the community has some thoughts on this? Is Control Variables working as advertised and I'm just not understanding how to use it correctly, is this an extremely niche edge case that never happens to anyone ever anyway, or is it an actual problem in y'all's opinion? I mean, don't know about other devs but I generally prefer if my events still work even if the player took a break before triggering them!

And please tell me this isn't a known issue, otherwise I'll feel really stupid for not finding anything about it via the Search!

Anyway,
Cheers!
Hmmm cool, I think the reason is that they were thinking those 'last things' for control variables are for battle (can't exit mid battle). Then in the menus it's used for automatically selecting the last index in item list.

Original thread: https://forums.rpgmakerweb.com/threads/possible-engine-bug-gameparty-lastitem-does-not-work-as-expected-and-neither-does-control-variables.182454/