Don't Starve Together

Don't Starve Together

Stack Tweaker
Furry Eskimo 28 Apr, 2023 @ 6:17pm
"attempt to perform arithmetic on field '?' (a nil value)"
Hello there. I've received a report that this mod causes the server to crash when used in conjunction with Wirra, specifically her hound plushies. I don't suppose you know why?

Proven to cause a server crash:
-Small Items: 180
-Medium Items: 120
-Large Items: 60
-Tiny Items: 60
-Plush Stack Size: 10

I'd wager you code is doing something weird with my code, specifically, with houndplush, line 179 or so. Care to take a peak and offer some feedback so we can ensure our mods are compatible?


Log file:
[00:00:31]: [string "scripts/components/stackable_replica.lua"]:34: attempt to perform arithmetic on field '?' (a nil value)
LUA ERROR stack traceback:
scripts/components/stackable_replica.lua:34 in (method) SetMaxSize (Lua) <33-35>
scripts/components/stackable.lua:11 in (field) ? (Lua) <10-12>
scripts/class.lua:43 in () ? (Lua) <36-45>
../mods/workshop-2238207705/scripts/prefabs/houndplush.lua:181 in (field) fn (Lua) <149-187>
scripts/mainfunctions.lua:336 in () ? (Lua) <325-371>
=[C]:-1 in (method) SpawnPrefab (C) <-1--1>
scripts/mainfunctions.lua:389 in (global) SpawnPrefab (Lua) <382-391>
scripts/mainfunctions.lua:420 in (global) SpawnSaveRecord (Lua) <418-464>
scripts/components/container.lua:704 in (method) OnLoad (Lua) <701-710>
scripts/entityscript.lua:1759 in (method) SetPersistData (Lua) <1746-1767>
scripts/mainfunctions.lua:457 in (global) SpawnSaveRecord (Lua) <418-464>
scripts/gamelogic.lua:662 in (upvalue) PopulateWorld (Lua) <394-698>
< >
Showing 1-12 of 12 comments
dewy  [developer] 29 Apr, 2023 @ 9:21pm 
i have been able to replicate the crash with the settings specified, i'm going to look into this further
Furry Eskimo 30 Apr, 2023 @ 2:57am 
Wonderful, thank you!
If there’s anything I can do to help avoid this issue, please let me know.
dewy  [developer] 30 Apr, 2023 @ 3:33am 
So my mod is rather simple, using only 4 lines in my modmain.lua to modify the different preset tuning variables for item stacks, there is a config option to enable a "ui fix". But that is fully optional and does not load if turned off.

I've deduced it's not my mod directly causing the issue per se, but a problem with how the game handles stack sizes.

And so I've spent the last five hours attempting to figure out a solution to this issue. Unfortunately I haven't found much on it.

The code for setting the hound plush's stack size is this:
inst.components.stackable.maxsize = forever_hound_stackable_config
While this works fine normally, it seems updating any of the stack size tuning variables breaks this for whatever reason??
Last edited by dewy; 30 Apr, 2023 @ 4:09am
dewy  [developer] 30 Apr, 2023 @ 3:36am 
This identical issue has occurred previously with these two mods

Delphox Character - https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=950178272
Stacks Size - https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=831657598

I opened up the code of the Delphox character, and sure enough they're setting a stack size using a specific number rather than one of the default tuning variables.

It works fine on its own, but the moment any of the tuning variables for the default stack sizes are updated with a new value, it breaks and causes the same exact crash we are encountering.
dewy  [developer] 30 Apr, 2023 @ 3:45am 
Unfortunately they did not find or implement a solution to the issue I could look at. I have tested a lot of various tweaks to your mod to attempt finding a fix for this but have so far been totally unlucky.

I however I did find a KLEI forum post that seems to have a potential solution that might solve this particular crash. I am yet to try what is show here, but it seems very promising.

They've used upvalue hacking to get the STACK_SIZES table from stackable_replica.lua and are inserting their own values into it.

https://forums.kleientertainment.com/forums/topic/69981-how-to-change-the-maximum-stack-size-of-one-prefab-to-a-custom-size-via-a-mod

I feel I may be missing a simpler solution to this, but I have not done any modding or programming in a long while and can't think of anything else for now.
Furry Eskimo 30 Apr, 2023 @ 12:04pm 
Ah, that old issue.. I recall it causing me some trouble too for a while, but I never spent much time on it. Perhaps there's a way to detect these values which are abnormal before you edit them, and if they don't match the expected values, simply don't edit them? That's been my go-to solution.
dewy  [developer] 30 Apr, 2023 @ 3:17pm 
I took the code from the KLEI forum post I linked, and implemented it into a slightly modified version of your mod

It seems to work fine when testing with just your mod and mine using any mix of settings.
dewy  [developer] 30 Apr, 2023 @ 3:28pm 
The implementation of the fix is very minor and can be copy-pasted with ease, take a look and hopefully it'll help out

EDIT: Modifying houndplush.lua was not needed and actually causes a crash. The addition to modmain.lua does work however.
First, modify houndplush.lua Line 181, instead of setting the maxsize to the forever hound stack variable, we'll set it to a default value for now to prevent issues, we'll override this in the modmain.lua
if forever_hound_stackable_config ~= 1 then inst:AddComponent("stackable") --These can be stacked in your inventory. inst.components.stackable.maxsize = TUNING.STACK_SIZE_MEDITEM -- this value is overridden in modmain, and is only set here to prevent empty value error inst.components.stackable.forcedropsingle = true --Can only be dropped on at a time. end

Next, simply paste this block at the end of the modmain.lua, this is where we're setting the actual config value.
--compat for mods that modify stacksize of default tuning variables, ex: TUNING.STACK_SIZE_MEDITEM --code borrowed from DarkXero on a klei forum post https://forums.kleientertainment.com/forums/topic/69981-how-to-change-the-maximum-stack-size-of-one-prefab-to-a-custom-size-via-a-mod --please look at the forum post linked for proper code documentation and explanation on how this works. TLDR: upvalue hacking to insert custom values into table if forever_hound_stackable_config ~= 1 then local function GetTableFromFunction(tablename, func) local debug = GLOBAL.debug local i = 1 while true do local n, v = debug.getupvalue(func, i) if not n then return nil end if n == tablename then return v end i = i + 1 end end local stackable_replica = GLOBAL.require("components/stackable_replica") stackable_replica._ctor = function(self, inst) self.inst = inst self._stacksize = GLOBAL.net_byte(inst.GUID, "stackable._stacksize", "stacksizedirty") self._maxsize = GLOBAL.net_smallbyte(inst.GUID, "stackable._maxsize") end local STACK_SIZES = GetTableFromFunction("STACK_SIZES", stackable_replica.MaxSize) local STACK_SIZE_CODES = GetTableFromFunction("STACK_SIZE_CODES", stackable_replica.SetMaxSize) local my_new_maxsizes = {forever_hound_stackable_config} for k, v in pairs(my_new_maxsizes) do table.insert(STACK_SIZES, v) end for k, v in pairs(STACK_SIZE_CODES) do STACK_SIZE_CODES[k] = nil end for k, v in pairs(STACK_SIZES) do STACK_SIZE_CODES[v] = k end local function setCustomStack(inst) if inst.components.stackable then inst.components.stackable.maxsize = forever_hound_stackable_config end end AddPrefabPostInit("houndplush", setCustomStack) end
Last edited by dewy; 2 May, 2023 @ 8:06pm
Furry Eskimo 30 Apr, 2023 @ 6:02pm 
Wow, that’s, a bit much. What is this actually doing? It’s taking a few menial lines of code and replacing them with, quite a bit, including a few loop functions. Is it really so complex to set a stack size?
dewy  [developer] 30 Apr, 2023 @ 6:06pm 
Visit the linked KLEI forum post, they've commented on the code block and explain how it works there, I'd like to find a better solution but am busy with other things for now.
Furry Eskimo 1 May, 2023 @ 3:53am 
Great, thanks. I’ll review the code tonight and beta-test it with a few others.
Furry Eskimo 12 Sep, 2024 @ 3:45pm 
Preparing the update now, sorry it took so ridiculously long.

I think the mods are compatible now!
Last edited by Furry Eskimo; 12 Sep, 2024 @ 5:36pm
< >
Showing 1-12 of 12 comments
Per page: 1530 50