From b42569eb9c9e6656fd7973f1192cdb757d6c9d52 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 11 Aug 2026 13:43:23 -0400 Subject: [PATCH] Prevent unavailable color switching crash --- room.lua | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/room.lua b/room.lua index 69c1314..a37bba5 100644 --- a/room.lua +++ b/room.lua @@ -341,21 +341,21 @@ function Room:movePlayer(key) end function Room:switchPlayerColor(color) - local canSwitch + -- Number keys can request a colour the player has not collected yet. Do + -- nothing in that case: there is no corresponding player object to switch + -- to (or use as the sound source). + local targetPlayer = self.player[color] + if not targetPlayer then return false end - if self:onlyColorIsActive(color) then - canSwitch = false - else - for _, c in pairs(self:getPossibleColors()) do - if c == color then - self:setActiveColor(c, true) - else - self:setActiveColor(c, false) - end + local canSwitch = not self:onlyColorIsActive(color) + if canSwitch then + for _, c in ipairs(self:getPossibleColors()) do + self:setActiveColor(c, c == color) end end - self.player[color]:playSwitchSound(canSwitch) + targetPlayer:playSwitchSound(canSwitch) + return canSwitch end function Room:setActiveColor(color, bool)