Jump to content

Need help with tools!


Recommended Posts

Hello. I need help again this time with disassembly tools. As planned, they were supposed to work like a pocketwatch dismantler only for items. There was a post on the forum about how they tried to do this, but there was not enough information on it. I have problems with the action because it crashes the game in the log they write bad argument #1 to 'insert' (table expected, got nil)  and what this means is not clear to me and how to fix it.
Here's the code:

Modmain

local DISASSEMBLYTRINKET = Action({ mount_valid=true })
DISASSEMBLYTRINKET.str = "Disassembly"
DISASSEMBLYTRINKET.id = "DISASSEMBLYTRINKET"
DISASSEMBLYTRINKET.fn = function(act)
    local can_dismantle, reason = act.invobject.components.Disassembly_Work:CanDismantle(act.target, act.doer)
    if can_dismantle then
    act.invobject.components.Disassembly_Work:Dismantle(act.target, act.doer)
    end
    return can_dismantle, reason
end

AddAction(DISASSEMBLYTRINKET)

AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.DISASSEMBLYTRINKET, "dolongaction"))
AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.DISASSEMBLYTRINKET, "dolongaction"))

AddComponentAction("INVENTORY", "DisassemblyWork", function(inst, doer, target, actions)
      table.insert(actions, GLOBAL.ACTIONS.DISASSEMBLYTRINKET)
end)

AddPrefabPostInit("disassemblytools", function(inst)
    inst:AddComponent("DisassemblyWork")
end)

Component/DisassemblyWork

local Disassembly_Work = Class(function(self, inst)
    self.inst = inst
end)

function Disassembly_Work:CanDismantle(target, doer)
    if not doer:HasTag("technician") then
        return false
    end

    return true
end

function Disassembly_Work:Dismantle(target, doer)
    local owner = target.components.inventoryitem:GetGrandOwner()
    local receiver = owner ~= nil and not owner:HasTag("pocketdimension_container") and (owner.components.inventory or owner.components.container) or nil
    local pt = receiver ~= nil and self.inst:GetPosition() or doer:GetPosition()

    local loot = target.components.lootdropper:GetFullRecipeLoot(AllRecipes[target.prefab])
    target:Remove() -- We remove the target before giving the loot to make more space in the inventory

    for _, prefab in ipairs(loot) do
        if prefab ~= "nightmarefuel" then
            if receiver ~= nil then
                receiver:GiveItem(SpawnPrefab(prefab), nil, pt)
            else
                target.components.lootdropper:SpawnLootPrefab(prefab, pt)
            end
        end
    end

    SpawnPrefab("brokentool").Transform:SetPosition(doer.Transform:GetWorldPosition())
end

return Disassembly_Work
 

And client log :

If you have a solution you can write

client_log.txt

Link to comment
Share on other sites

Posted (edited)

After several days of trying, I managed to do :

Modmain :

AddAction("DISMANTLE_TRINKET", "disassembly", function(act)
    if act.invobject then
    act.invobject.components.disassembly_trinket:dismantle(act.target, act.doer)
    return true
    end
    
    
    return false
end)

AddComponentAction("USEITEM", "disassembly_trinket", function(inst, doer, target, actions, right)
    if doer:HasTag("technician") then
      if target:HasTag("disassemblytrinket") then
      table.insert(actions, GLOBAL.ACTIONS.DISMANTLE_TRINKET)
        end
    end
end)

AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.DISMANTLE_TRINKET, "dolongaction"))
AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.DISMANTLE_TRINKET, "dolongaction"))

local disassemblytrinketitem = {
    "trinket_1",
}

AddPrefabPostInitAny(function(inst)
    if GLOBAL.TheWorld.ismastersim then
    for k, trinketitem in pairs(disassemblytrinketitem) do
    if inst.prefab == trinketitem then
    inst:AddTag("disassemblytrinket")
            end
        end
    end
end)

Component/disassembly_trinket

local disassembly_trinket = Class(function(self, inst)
    self.inst = inst
end)

function disassembly_trinket:dismantle(target, doer)
    if target.prefab == "trinket_1" then
    self.inst.components.finiteuses:Use(1)
    doer.components.inventory:GiveItem(SpawnPrefab("metals"))
    SpawnPrefab("brokentool").Transform:SetPosition(doer.Transform:GetWorldPosition())
    if target.components.stackable ~= nil then
    target.components.stackable:Get(1):Remove()
    else
    target:Remove()
        end
    end
end

return disassembly_trinket
prefabs/disassemblytools

inst:AddComponent("disassembly_trinket")

Maybe this will be useful to someone, but for now, good luck to everyone.

Edited by josra102
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
 Share

×
  • Create New...