Project Zomboid

Project Zomboid

Better Flashlights [FIXED]
Improving Code Structure by Localizing Global Variables
I recommend making the Item variable local rather than global, and organizing the code for better readability.

local getActivatedMods = getActivatedMods local size = size local get = get local function isMod(mod_Name) local mods = getActivatedMods(); for i = 0, mods:size() - 1, 1 do if mods:get(i) == mod_Name then return true; end end return false; end local function Adjust(Name, Property, Value) local item = ScriptManager.instance:getItem(Name) if item then item:DoParam(Property .. " = " .. Value) else print("Error: No item found with name " .. Name) end end if isMod("Authentic Z - Backpacks+") or isMod("Authentic Z - Current") then Adjust("AuthenticZClothing.Torch2", "AttachmentType", "Torch") Adjust("AuthenticZClothing.HandTorch2", "AttachmentType", "HandTorchSmall") end if isMod("Better Belts") then Adjust("Base.HandTorch", "AttachmentType", "Torchb") Adjust("Base.Belt3", "DisplayCategory", "Accessory") Adjust("Base.Belt4", "DisplayCategory", "Accessory") Adjust("Base.Belt3", "WorldStaticModel", "Belt_Ground") Adjust("Base.Belt4", "WorldStaticModel", "Belt_Ground") else Adjust("Base.HandTorch", "AttachmentType", "HandTorchSmall") end
< >
Showing 1-2 of 2 comments
MythicalGoon 24 Nov, 2023 @ 4:21am 
I agree this code looks much more organized and should provide a lighter load
Sennyツ  [developer] 29 Feb, 2024 @ 4:28pm 
Originally posted by 매콤다람쥐:
I recommend making the Item variable local rather than global, and organizing the code for better readability.

local getActivatedMods = getActivatedMods local size = size local get = get local function isMod(mod_Name) local mods = getActivatedMods(); for i = 0, mods:size() - 1, 1 do if mods:get(i) == mod_Name then return true; end end return false; end local function Adjust(Name, Property, Value) local item = ScriptManager.instance:getItem(Name) if item then item:DoParam(Property .. " = " .. Value) else print("Error: No item found with name " .. Name) end end if isMod("Authentic Z - Backpacks+") or isMod("Authentic Z - Current") then Adjust("AuthenticZClothing.Torch2", "AttachmentType", "Torch") Adjust("AuthenticZClothing.HandTorch2", "AttachmentType", "HandTorchSmall") end if isMod("Better Belts") then Adjust("Base.HandTorch", "AttachmentType", "Torchb") Adjust("Base.Belt3", "DisplayCategory", "Accessory") Adjust("Base.Belt4", "DisplayCategory", "Accessory") Adjust("Base.Belt3", "WorldStaticModel", "Belt_Ground") Adjust("Base.Belt4", "WorldStaticModel", "Belt_Ground") else Adjust("Base.HandTorch", "AttachmentType", "HandTorchSmall") end

I have now implemented this into the code and hopefully what you both suggest is true. Let me know if any problems occur! :cwat:
< >
Showing 1-2 of 2 comments
Per page: 1530 50