Jump to content

Recommended Posts

Hi! So as the title says; i'm trying to make a container repair items inside it; like wickerbottom's bookshelf. I did some digging and found Wickerbottom's code for restoreing books. However It only restores items with a certain tag; I want my container to restore any item with durability inside it. Since the container only allows certain items inside it anyways. I set up a few functions in my prefab file but it's currently crashing and I think it's not exactly what I want. I'll put the function in here; hopefully Someone knows how to get this working!

image.png.7ec43204417b8fc216991efd65e31624.png

local function RestoreWrampItem(inst)
 
    for k,v in pairs(inst.components.container.slots) do
        if v.components.finiteuses then
            local percent = v.components.finiteuses:GetPercent()
            if percent < 1 then
                v.components.finiteuses:SetPercent(math.min(1, percent + (TUNING.BOOKSTATION_RESTORE_AMOUNT)))
            end
        end
    end
end
 
local function ItemGet(inst)
    if inst.RestoreTask == nil then
            inst.RestoreTask = inst:DoPeriodicTask(TUNING.BOOKSTATION_RESTORE_TIME, RestoreWrampItem)
        end
    end
end
 
local function ItemLose(inst)
    if not inst.components.container:HasItemWithTag("shadow_item", 1) then
        if inst.RestoreTask ~= nil then
            inst.RestoreTask:Cancel()
            inst.RestoreTask = nil
        end
    end
end

And further down my prefab file I listen for event:

inst:ListenForEvent("itemget", ItemGet)
    inst:ListenForEvent("itemlose", ItemLose)

If you need me to upload my mod I can do that! I'm pretty new to LUA so any help would be awesome.

I'm also fairly new to LUA, so I don't feel that qualified for this, but uploading your mod or a screenshot of what your screen looks like with the crash log could help me solve this! (Or whatever gobbledegook is spat out in the Documents\Klei\DoNotStarveTogether directory if DST is straight up crashing with no crash log popping up)

Here's the crash

00:00:50]: Mod: Wramp suitcase CODE REPAIR TEST (Wramp Suitcase REPAIR TEST)    Registering prefabs    
[00:00:50]: Mod: Wramp suitcase CODE REPAIR TEST (Wramp Suitcase REPAIR TEST)      Registering prefab file: prefabs/wramp_suitcase    
[00:00:50]: error calling LoadPrefabFile in mod Wramp suitcase CODE REPAIR TEST (Wramp Suitcase REPAIR TEST): 
[string "scripts/mainfunctions.lua"]:160: Error loading file prefabs/wramp_suitcase
[string "../mods/Wramp suitcase CODE REPAIR TEST/scripts/prefabs/wramp_s..."]:39: '<eof>' expected near 'end'
LUA ERROR stack traceback:
        =[C] in function 'assert'
        scripts/mainfunctions.lua(160,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(190,1)
        scripts/mods.lua(689,1) in function '_RegisterPrefabs'
        local MS_MODNAME = 'workshop-2812783478';local a = {}
 

9 minutes ago, Doodle Monster said:

Here's the crash

00:00:50]: Mod: Wramp suitcase CODE REPAIR TEST (Wramp Suitcase REPAIR TEST)    Registering prefabs    
[00:00:50]: Mod: Wramp suitcase CODE REPAIR TEST (Wramp Suitcase REPAIR TEST)      Registering prefab file: prefabs/wramp_suitcase    
[00:00:50]: error calling LoadPrefabFile in mod Wramp suitcase CODE REPAIR TEST (Wramp Suitcase REPAIR TEST): 
[string "scripts/mainfunctions.lua"]:160: Error loading file prefabs/wramp_suitcase
[string "../mods/Wramp suitcase CODE REPAIR TEST/scripts/prefabs/wramp_s..."]:39: '<eof>' expected near 'end'
 

Alright, I believe this crash should be an easy fix. It seems you just have an additional end on the "ItemGet" function that's causing the game to go crazy. So...

local function ItemGet(inst)
    if inst.RestoreTask == nil then
            inst.RestoreTask = inst:DoPeriodicTask(TUNING.BOOKSTATION_RESTORE_TIME, RestoreWrampItem)
        end
    end
end

Should get trimmed down to just...

local function ItemGet(inst)
    if inst.RestoreTask == nil then
        inst.RestoreTask = inst:DoPeriodicTask(TUNING.BOOKSTATION_RESTORE_TIME, RestoreWrampItem)
    end
end

I just did a little additon to the function; hopefully it will work but i'm not sure. It's supposed to detect if the item uses armor durability instead and repair that.

local function RestoreWrampItem(inst)
 
    for k,v in pairs(inst.components.container.slots) do
        if v.components.finiteuses then
            local percent = v.components.finiteuses:GetPercent()
            if percent < 1 then
                v.components.finiteuses:SetPercent(math.min(1, percent + (TUNING.BOOKSTATION_RESTORE_AMOUNT)))
            or
        if v.components.armor then
            local percent = v.components.armor:GetPercent()
            if percent < 1 then
                v.components.armor:SetPercent(math.min(1, percent + (TUNING.BOOKSTATION_RESTORE_AMOUNT)))
            end
        end
    end
end

 

Edited by Doodle Monster

Okay, I added a area for repairing armor; problem is I'm getting a new crash. Here's the function I changed and the crash.

local function RestoreWrampItem(inst)
 
    for k,v in pairs(inst.components.container.slots) do
        if v.components.finiteuses then
            local percent = v.components.finiteuses:GetPercent()
            if percent < 1 then
                v.components.finiteuses:SetPercent(math.min(1, percent + (TUNING.BOOKSTATION_RESTORE_AMOUNT)))
            else
        if v.components.armor then
            local percent = v.components.armor:GetPercent()
            if percent < 1 then
                v.components.armor:SetPercent(math.min(1, percent + (TUNING.BOOKSTATION_RESTORE_AMOUNT)))
            end
        end
    end
end
Edited by Doodle Monster

[00:01:10]: [string "scripts/mainfunctions.lua"]:160: Error loading file prefabs/wramp_suitcase
[string "../mods/Wramp suitcase CODE REPAIR WORKING PARTIALLY/scripts/pr..."]:124: 'end' expected (to close 'for' at line 24) near '<eof>'
LUA ERROR stack traceback:
        =[C] in function 'assert'
        scripts/mainfunctions.lua(160,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(190,1)
        scripts/mods.lua(689,1) in function '_RegisterPrefabs'
        local MS_MODNAME = 'workshop-2812783478';local a = {}

Edited by Doodle Monster

Okay, I think I fixed the syntax error on my own; However it is still crashing when trying to read the armor repair line.

local function RestoreWrampItem(inst)
 
    for k,v in pairs(inst.components.container.slots) do
        if v.components.finiteuses then
            local percent = v.components.finiteuses:GetPercent()
                    if percent < 1 then
                v.components.finiteuses:SetPercent(math.min(1, percent + (TUNING.BOOKSTATION_RESTORE_AMOUNT)))
            else
                v.components.armor then
                local percent = v.components.armor:GetPercent()
                    if percent < 1 then
                v.components.armor:SetPercent(math.min(1, percent + (TUNING.BOOKSTATION_RESTORE_AMOUNT)))
                end
            end
        end
    end
end
Edited by Doodle Monster

[00:01:52]: [string "scripts/mainfunctions.lua"]:160: Error loading file prefabs/wramp_suitcase
[string "../mods/Wramp suitcase CODE repair armor test/scripts/prefabs/w..."]:30: '=' expected near 'then'
LUA ERROR stack traceback:
        =[C] in function 'assert'
        scripts/mainfunctions.lua(160,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(190,1)
        scripts/mods.lua(689,1) in function '_RegisterPrefabs'
        local MS_MODNAME = 'workshop-2812783478';local a = {}

Edited by Doodle Monster

I'm not sure if v.components.armor works for repairing armor anyways, I did take a quick look at the dreadstone armor but I don't really know what's happening in the code

Edited by Doodle Monster

I think this line is what i'm looking for. I'll take another crack at it later, Hopefully someone can help me get this working :P

inst.components.armor:Repair(inst.components.armor.maxcondition * rate * setbonus)
Edited by Doodle Monster

It seems geting it to repair armor is a lot trickyer; I added the line from the dreadstone armor into the function. I *really* Don't know what i'm doing. Here's the function and the crash:

local function RestoreWrampItem(inst)
 
    for k,v in pairs(inst.components.container.slots) do
        if v.components.finiteuses then
            local percent = v.components.finiteuses:GetPercent()
                    if percent < 1 then
                v.components.finiteuses:SetPercent(math.min(1, percent + (TUNING.BOOKSTATION_RESTORE_AMOUNT)))
            else
                v.components.armor then
                local percent = v.components.armor:GetPercent()
                    if percent < 1 then
                    v.components.armor:Repair(inst.components.armor.maxcondition * 1 * 1.5)
                end
            end
        end
    end
end

[00:01:48]: Disabling Wramp suitcase CODE repair armor test dreadstone (Wramp Suitcase REPAIR TEST) because it had an error.    
[00:01:48]: [string "scripts/mainfunctions.lua"]:160: Error loading file prefabs/wramp_suitcase
[string "../mods/Wramp suitcase CODE repair armor test dreadstone/script..."]:30: '=' expected near 'then'
LUA ERROR stack traceback:
        =[C] in function 'assert'
        scripts/mainfunctions.lua(160,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(190,1)
        scripts/mods.lua(689,1) in function '_RegisterPrefabs'
        local MS_MODNAME = 'workshop-2812783478';local a = {}
 

Line 30 starts at this part: 

v.components.armor then
                local percent = v.components.armor:GetPercent()
                    if percent < 1 then
                    v.components.armor:Repair(inst.components.armor.maxcondition * 1 * 1.5)

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...