Project Zomboid

Project Zomboid

Reorder Containers - Backpack Orders [B42/41]
G.I.Bill Jun 8, 2023 @ 2:21pm
Support for additional controller container switching modes
Since I have some programming skills, I thought I'd take a crack at coming up with a fix to get the mod to support the other two controller modes, and save you some time. LOL By comparing what the base code was doing to handle the different modes, I was able to get it to work correctly for mode 2, where the left bumper moves down to the next container and right bumper moves up in the currently selected inventory pane. The code change for that is the following:

ISInventoryPage.pre_reorder_onJoypadDown = ISInventoryPage.onJoypadDown ISInventoryPage.onJoypadDown = function(self, button) local shoulderSwitch = getCore():getOptionShoulderButtonContainerSwitch() if getCore():getGameMode() == "Tutorial" then shoulderSwitch = 1 end b local inv = getPlayerInventory(self.player) local loot = getPlayerLoot(self.player) if shoulderSwitch == 1 then if button == Joypad.LBumper then return handleJoypadDown(self, inv, button) elseif button == Joypad.RBumper then if ReorderContainers_Mod.canReorderBackpacks(loot) then return handleJoypadDown(self, loot, button) end end elseif shoulderSwitch == 2 then if button == Joypad.LBumper then if self == loot and ReorderContainers_Mod.canReorderBackpacks(loot) then return handleJoypadDown(self, loot, button) elseif self == inv then return handleJoypadDown(self, inv, button) end elseif button == Joypad.RBumper then if self == loot and ReorderContainers_Mod.canReorderBackpacks(loot) then return handleJoypadDown(self, loot, button) elseif self == inv then return handleJoypadDown(self, inv, button) end end end return self:pre_reorder_onJoypadDown(button) end

I did also try getting it to work on mode 3, where you hold down the left or right bumper to select the left or right pane then use the D-pad up/down to change containers, but that one so far has me confounded. I should probably mention that I've never done any Project Zomboid modding nor Lua scripting prior to this point, but I have done a lot of programming and scripting in other languages, so that allowed me to pick up the gist of it pretty quickly. I believe supporting mode 3 requires overriding two other methods onJoypadDirUp and onJoypadDirDown, but my first attempt at doing that resulted in the controller functions going a little haywire. If I figure it out I'll post that here too.
Last edited by G.I.Bill; Jun 8, 2023 @ 2:50pm
< >
Showing 1-5 of 5 comments
G.I.Bill Jun 8, 2023 @ 2:43pm 
Well, after taking another look at the code I had to support mode 3, I found it was just behaving incorrectly due to a few typos. Doh! Although it seems to be working fine now, I'd strongly recommend double-checking that I'm not doing anything heinously wrong in my code, since as I mentioned this is a first step for me into both Project Zomboid modding and Lua scripting. :-)

local function handleJoypadDirUp(self, target, joypadData) -- Store the original order of the backpacks local originalOrder = {} for index, button in ipairs(target.backpacks) do originalOrder[button] = index end -- Sort the backpacks by their Y position so that scrolling works as expected table.sort(target.backpacks, function(a, b) return a:getY() < b:getY() end) -- Clear the 'backpackChoice', not sure what its actually for, but we stop it from existing on bumper inputs target.killTheChoice = true local retVal = self:pre_reorder_onJoypadDirUp(joypadData) table.sort(target.backpacks, function(a, b) return originalOrder[a] < originalOrder end)
return retVal
end

local function handleJoypadDirDown(self, target, joypadData)
-- Store the original order of the backpacks
local originalOrder = {}
for index, button in ipairs(target.backpacks) do
originalOrder[button] = index
end

-- Sort the backpacks by their Y position so that scrolling works as expected
table.sort(target.backpacks, function(a, b) return a:getY() < b:getY() end)

-- Clear the 'backpackChoice', not sure what its actually for, but we stop it from existing on bumper inputs
target.killTheChoice = true

local retVal = self:pre_reorder_onJoypadDirDown(joypadData)
table.sort(target.backpacks, function(a, b) return originalOrder[a] < originalOrder end)
return retVal
end

ISInventoryPage.pre_reorder_onJoypadDirUp = ISInventoryPage.onJoypadDirUp
ISInventoryPage.onJoypadDirUp = function(self, joypadData)
local shoulderSwitch = getCore():getOptionShoulderButtonContainerSwitch()
local inv = getPlayerInventory(self.player)
local loot = getPlayerLoot(self.player)

if shoulderSwitch == 3 then
if isJoypadPressed(joypadData.id, Joypad.LBumper) then
return handleJoypadDirUp(self, inv, joypadData)
elseif isJoypadPressed(joypadData.id, Joypad.RBumper) then
if ReorderContainers_Mod.canReorderBackpacks(loot) then
return handleJoypadDirUp(self, loot, joypadData)
end
end
end
return self:pre_reorder_onJoypadDirUp(joypadData)
end

ISInventoryPage.pre_reorder_onJoypadDirDown = ISInventoryPage.onJoypadDirDown
ISInventoryPage.onJoypadDirDown = function(self, joypadData)
local shoulderSwitch = getCore():getOptionShoulderButtonContainerSwitch()
local inv = getPlayerInventory(self.player)
local loot = getPlayerLoot(self.player)

if shoulderSwitch == 3 then
if isJoypadPressed(joypadData.id, Joypad.LBumper) then
return handleJoypadDirDown(self, inv, joypadData)
elseif isJoypadPressed(joypadData.id, Joypad.RBumper) then
if ReorderContainers_Mod.canReorderBackpacks(loot) then
return handleJoypadDirDown(self, loot, joypadData)
end
end
end
return self:pre_reorder_onJoypadDirDown(joypadData)
end
Last edited by G.I.Bill; Jun 8, 2023 @ 2:47pm
Notloc  [developer] Jun 10, 2023 @ 11:59am 
Oh thanks, I'll give this a test soon.
Controller has not been a high priority for me thus far, so this is very helpful.
76561198114572053 Jul 31, 2023 @ 10:34am 
confirm not working with controller when rearrange storage with mouse
It makes absolutely no sense that this is still a problem on controller. Even more so if someone already provided the code and it doesn't exist on vanilla or any other mod. Makes me think they don't have anyone testing controller or maybe there's a combination on controller that cycles up the containers idk man but ty anyway for trying
Notloc  [developer] Jan 31 @ 7:48pm 
You write this like you paid for a product.
< >
Showing 1-5 of 5 comments
Per page: 1530 50