Jump to content

[Help] How to correctly use IsValid() to judge whether an item is valid?

Recommended Posts

I'm making a mod, Smart Chest, workshop-2097358269.

Among them, one function is to collect periodically dropped items. My implementation method is to modify the onspawn function of the periodicspawner component. 0.5 seconds after the corresponding item spawns, search the chests around this item and try to put it in.

In most scenarios it works fine. But based on feedback from other players, it often causes crashes when dealing with Grom slime spawns.

By looking at the log file, the process of the crash is probably like this.
First, a glommerfuel (valid: false) (indicated by A) is generated, then a series of processes are heard, and the function of collecting this item in the chest is triggered. Finally, the existing glommerfuel in the chest (indicated by B) uses the Put function of the stackable component , add this generated glommerfuel (A) into it.
Because the "stacksize" attribute of B is modified, the onstacksize function in the stackable component will be triggered. This function will call B.replica.inventoruyitem:SetPickupPos(_src_pos). The attribute "classified" will be used in the SetPickupPos function. But this time this attribute is nil, which caused a crash.

I feel that the main problem is why B does not have the attribute classified. But I don't know why.

I try to avoid entering the subsequent collection process by judging whether A is Valid. The code I use is as follows: The dropped here corresponds to the glammerfuel of this A. In the next line of the for loop, I use IsValid() to add a judgment, but the program still runs to the inside of the if judgment, not the break in the else.

-- modmain.lua 205 - 223 lines
local function collectItem2Chest(dropped)
  if dropped ~= nil then
    -- 搜索周围的箱子
    --print("[collectItem2Chest] prefab = " .. dropped.prefab)
    --print("[collect] search Tag lxautocollectitems")
    local x, y, z = dropped.Transform:GetWorldPosition()
    local ents = _G.TheSim:FindEntities(x, y, z, collectdist, {"lxautocollectitems"}, {"burnt"})
    local name = dropped.drawnameoverride or dropped:GetBasicDisplayName()
    for k, v in pairs(ents) do
      if dropped ~= nil and dropped:IsValid() then
        if v and v.components and v.components.lxautocollectitems then
          dropped = v.components.lxautocollectitems:onCollectItems(dropped, name)

The corresponding log is:

../mods/workshop-2097358269/modmain.lua:216 in (upvalue) collectItem2Chest (Lua) <205-223>
   dropped = 180753 - glommerfuel (valid:false)
   x = -48
   y = 0
   z = -165.5
   ents = table: 00000000A08ED590
   name = 格罗姆的黏液
   k = 1
   v = 101400 - treasurechest (valid:true)

I put the log file in the attachment, as well as the files in my mod. The path of lxautocollectionitems.lua relative to modmain.lua is ./scripts/components/lxautocollectionitems.lua.

I hope someone can help me solve or explain this problem. I don't quite understand valid, replica, and classified in DST.

client_log(1).txt modmain.lua lxautocollectitems.lua

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...