Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified Item and Inventory "Remove" function as well as removed non-functional code #477

Closed
wants to merge 3 commits into from

Conversation

DoopieWop
Copy link
Collaborator

Both functions use a loop to remove the item. As far as I can tell this really isn't necessary. In both cases I simply use the item's gridX and gridY member values to find their entry in the Inventory "slots" table to delete them there.

In Item "Remove" function, there is code that would be run when the loop couldn't find the item in the Inventory "slots" table. I've personally never had this happen, additionally the code that is supposed to run, if the loop doesn't find the item, doesn't make sense to me.

First, the Inventory "slots" table (the table that stores its items, with their grid X and Y values as the keys) is emptied.

inv.slots = {}

Next. a loop using the Inventory "Iter" function is supposed to loop through all the items stored in the inventory, then populate the "slots" table (but only within the x, y and width & height values of the to-be-removed item?) with the adequate data. However because "Iter" internally uses the "slots" table (which at this point is empty) effectively nothing happens.

for k, _ in inv:Iter() do
if (k.invID == inv:GetID()) then
for x = self.gridX, self.gridX + (self.width - 1) do
for y = self.gridY, self.gridY + (self.height - 1) do
inv.slots[x][y] = k.id
end
end
end
end

Lastly the changed inventory is meant to be synced to the inventory owner, however the if-condition expects a player object, while the inventory member value "owner" actually returns a character ID (or in general a number, for instance for container inventories), so this if-condition actually causes an error, if it were to be executed.

if (IsValid(inv.owner) and inv.owner:IsPlayer()) then
inv:Sync(inv.owner, true)
end

To me this appears to be outdated code, leftover from the transition from Nutscript to Helix, so I'd just remove it.

@alexgrist
Copy link
Member

Next. a loop using the Inventory "Iter" function is supposed to loop through all the items stored in the inventory, then populate the "slots" table (but only within the x, y and width & height values of the to-be-removed item?) with the adequate data. However because "Iter" internally uses the "slots" table (which at this point is empty) effectively nothing happens.

This is due to #468 which removed some usages of GetItems() including in the META:Remove function.

if (failed) then
local items = inv:GetItems()
inv.slots = {}
for _, v in pairs(items) do

The items table is likely a cache of the items prior to the slots being cleared which is looped.

@DoopieWop DoopieWop closed this Jan 18, 2025
@DoopieWop
Copy link
Collaborator Author

I misunderstood the way items are stored in the inventory slots table, my changed remove functions would not work with items larger than 1x1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants