Mod API add event that fires before character's main inventory changes size

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
User avatar
undermark5
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu May 21, 2020 10:55 pm
Contact:

Mod API add event that fires before character's main inventory changes size

Post by undermark5 »

I'm the author of a mod called SpillProof, currently I set all armor bonuses to 0 and listen for armor slot changes to manually add and remove slots via the character's bonus slot value in an attempt to prevent items from spilling when you accidentally unequip armor (the items stacks are moved to a script inventory before reducing the bonus and added back from the script inventory after the bonus is applied).

I propose/request an event that fires before a characters main inventory size changes and has a buffer of the item stacks that are going to be removed from the inventory and spilled to the floor. Then my mod would be able to react to those events instead and I wouldn't need to remove the armor slot bonuses (making it difficult to see how many slots given armor gives you because the tooltip no longer has this information present) plus, it would allow me to react to other mods adding and removing slots without needing specific compatibility built into them (For example, Nullius adds equipment that adds and removes slots via the inventory slot bonuses value, which is where I'm currently experiencing compatibility issues)

Items spilling from accidentally removing armor are frequently complained about by players, and while I understand that preventing it in a fair and balanced way in engine may be impossible, that doesn't need to mean that mods can't do it however the author felt was fair and balanced. Having such an event makes implementing such a mod much easier.

sarge945
Burner Inserter
Burner Inserter
Posts: 16
Joined: Wed Apr 26, 2023 9:45 am
Contact:

Re: Mod API add event that fires before character's main inventory changes size

Post by sarge945 »

Can't you use a combination of on_player_dropped_item and on_player_armor_inventory_changed to detect this already?

You can probably compare the tick values of all the events, and any that match the same time as the inventory change event get added to a container.

I have only made 2 mods so far, so I am still pretty novice at this, feel free to correct me if I'm wrong.

I agree that more events would be nice and are always welcome, as it massively improves the development experience for modders.

User avatar
undermark5
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu May 21, 2020 10:55 pm
Contact:

Re: Mod API add event that fires before character's main inventory changes size

Post by undermark5 »

I'm not 100% certain that "on_player_droped_item" actually fires for when inventory spills, it might, but with that approach, it would mean reacting to every single item that is spilled out of the inventory, rather than stacks. Vanilla has a fair number of items that can stack to 100, and a handful that can stack to 200, plus space science can stack to 2000. Imagine the number of items that I'd have to react to and "pick up" if the player happened to have an entire inventory row full of space science (20000 items). I can't imagine that would be performant.

Currently, as stated, I've reduced the armor inventory bonus for all armors to 0, thus they never change the size of the inventory by being (un)equipped, I listen for the "on_player_armor_inventory_changed" event and see what the current type of armor equipped is and add/remove the appropiate bonus amount to "character_inventory_slots_bonus", adding/removing items at the appropriate time to avoid a spill. The items don't get added to a container, but exist within a inaccessible script inventory (the way you get them back is by equipping armor that gives a bonus again). Using this mechanism, I only iterate over as many inventory slots are being added or removed rather than reacting to thousands of items being dropped.

Regardless, there exists a similar event for script inventories "on_pre_script_inventory_resized" or "on_script_inventory_resized", but because the character inventory is not a script inventory, those events can't be used.

There are ways to detect what I want to accomplish, and I have them working, but they are somewhat of a hack, which is why a way to listen to changes to the character's inventory size (regardless of the cause) would be helpful.

Post Reply

Return to “Modding interface requests”