Jump to content

Unequip damaged items.


Recommended Posts

I know there's the 1% unequip mod, but I wanted to make a custom character that removed an item not covered by the mod (by default).  All I want is a chilled amulet to be removed when at 5%.  Currently my code doesn't crash the game, but it also doesn't seem to do, anything at all..

It's a periodic task: inst.task = inst:DoPeriodicTask(1, AutoUnequip)  --Test code.

local function AutoUnequip(inst)  --Test code.
    local item = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BACK or EQUIPSLOTS.BODY)  --Note: Back and body?
    --local item = item.entity:GetParent()  --Get current item's percent.

    if not item.percentused:value() > 5 and item.name == "Chilled Amulet" then
        Unequip(item)
    end
end

Link to comment
Share on other sites

@penguin0616
Of course!  How could I have forgotten!

Also double thanks, because I only looked up how to check item names last night and that’s what I found. (Whoops)  I knew there was a Better way to check the ‘name’ but had no idea what to search for.  I’ll check these and provide an update tonight.

Edited by FurryEskimo
Link to comment
Share on other sites

@penguin0616
Welp, somehow even this breaks the game.  I'm still trying to figure out what's wrong.  It's specifically the code checking for the item's percent value or prefab value that seems to be breaking this time.  Currently this is in my custom character's file, used to make them check their item every second, and unequip it it's going to break.  What's so weird is that I've looked at the unequip at 1% mod (code attached) and it's almost so simple, but still complex, and I honestly don't even know how their code runs, like where is it? When is it being called??  I tried to make my own sort of, noob-version..

local function AutoUnequip(inst)
    local item = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BACK or EQUIPSLOTS.BODY)  --Note: Back and body?

    if not item.percentused:value() > 0.05 and item.prefab("blueamulet") then  --Note: Something here is broken.
--        Unequip(item)
    end
end

modmain.lua

Link to comment
Share on other sites

@penguin0616
I thought so, yeah.  I was just testing another method in-case the original code was wrong. (item.prefab == "blueamulet")
I think the issue may lie in 'item'.  I don't think it's actually calling on the item, but the local variable which might be defined wrong..  In the 1% mod the function refers to the instance/item directly, but I'm not doing that.

The reason my code looks the way it does is because I'm basing my code on code from the Nanachi mod.

 local item = sleeper.components.inventory:GetEquippedItem(EQUIPSLOTS.BACK or EQUIPSLOTS.BODY)  --This is where the item's stats (about a custom backpack) are being defined, and how I assumed, I could store my necklace's stats.

    if item then print(item.prefab) end  --Print's item's prefab data.
    if sleeper:HasTag("nanachi") and item and item:HasTag("mitty") then
        print("mitty in sleep!")
        local lefttime = item.components.timer:GetTimeLeft("sleep")
        if lefttime and lefttime > 0 then
            item.components.timer:StopTimer("sleep")
            inst.components.timer:StartTimer("mittysleep",lefttime/3)
        end
        if item:HasTag("sleep") then
            inst.AnimState:AddOverrideBuild("nanachitent_mitty")
        else
            inst.AnimState:AddOverrideBuild("nanachitent_mittywake")
        end
    end

From what I can tell, the blue amulet's data is stored inside 'item', but I haven't figured out how to use the data yet.  I'd call on the item's data directly, but I haven't found any guides on how to do that yet.

Edited by FurryEskimo
Link to comment
Share on other sites

@penguin0616
Server.  Is that bad?  I put the code into the character's file, assuming it would make anyone playing the character un-equip the item.  Normally I wouldn't care to add a feature like this, but surprisingly, blue amulets only last 6 minutes, and I sort of assumed they lasted longer than that.  (I want to help reduce the amount of micro-managing.  I keep burning through amulets because I look away for a minute and bam, no more amulet..)

Edited by FurryEskimo
Link to comment
Share on other sites

@penguin0616

No, that can’t be it.  I have the unequip code commented out.  The code that’s checking the ‘item’’s stats is what causes the issue.  When commented out it works fine, but when allowed to run it crashes.  It should (currently) be checking the item’s stats, and then doing nothing.
I haven:t been able to fix the issue yet, so I’m working on the character’s art, and may simply tell people to use the 1% mod if I can’t figure this out.

Link to comment
Share on other sites

@penguin0616

When this line of code is called, the game crashes.

    if not item.percentused:value() > 0.05 and item.prefab("blueamulet") then  --Note: Something here is broken.
--        Unequip(item)
    end

I haven’t had time to test it much.  It’s possible the variable ‘item’ isn’t storing the data I think it is.  

Link to comment
Share on other sites

@penguin0616
After a second of the server begins running, everything freezes and I'm told I've been disconnected from the server.  No error report is presented.

I checked the client log, and this is repeated about two dozen times: [00:02:50]: Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. 

And at the end it says:

[00:02:50]: Orphaned unnamed resource. This resource must have used Add( resource ) to insert itself into the manager. 
[00:02:51]: CurlRequestManager::ClientThread::Main() complete
[00:02:51]: HttpClient2 discarded 0 callbacks.
[00:02:51]: Shutting down

I'm assuming it's referring to 'item'.
local item = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BACK or EQUIPSLOTS.BODY)  --Note: Back and body?

Edited by FurryEskimo
Link to comment
Share on other sites

@penguin0616
I think I found it.

[00:01:16]: [string "../mods/FurryEskimo/scripts/prefabs/furryes..."]:232: attempt to index local 'item' (a nil value)
LUA ERROR stack traceback:
../mods/FurryEskimo/scripts/prefabs/furryeskimo.lua:232 in (field) fn (Lua) <210-245>
   inst = 116733 - furryeskimo (valid:true)
   item = nil

server_log.txt

Link to comment
Share on other sites

@penguin0616
That stopped it from crashing!

Now I just have to make the character unequip the item.  Of course it's not working, but at least it's not crashing.

    if not item == nil then
        if not item.percentused:value() > 0.5 and item.prefab == "blueamulet" then  --Note: Something here is broken.
        --    Unequip(item)
        --    inst.Unequip(item)
        --    inst.components.inventory:Unequip(EQUIPSLOTS.BODY)
            inst.components.inventory:Unequip(EQUIPSLOTS.BACK)
        end
    end

Link to comment
Share on other sites

I believe I found the component.  I seem to be right, but I'm missing the 'slip' value.  I think that's if I drop it or not.

function Inventory:Unequip(equipslot, slip)
    local item = self.equipslots[equipslot]
    --print("Inventory:Unequip", item)
    if item ~= nil then
        if item.components.equippable ~= nil then
            item.components.equippable:Unequip(self.inst)
            local overflow = self:GetOverflowContainer()
            if overflow ~= nil and overflow.inst == item then
                self.inst:PushEvent("setoverflow", {})
            end
        end
        if equipslot == EQUIPSLOTS.BODY then
            self.heavylifting = false
        end
    end
    self.equipslots[equipslot] = nil
    self.inst:PushEvent("unequip", {item=item, eslot=equipslot, slip=slip})
    return item
end

>>>I've tried a few different things so far, but none seem to do anything.  I'm going to test if this code is even running.

    --        PushEvent("unequip")
        --    Unequip(item)
        --    Unequip(EQUIPSLOTS.BODY)
        --    inst.Unequip(item)
        --    inst.Unequip(EQUIPSLOTS.BODY)
    --        inst.components.inventory:Unequip(EQUIPSLOTS.BODY)
        --    inst.components.inventory:Unequip(EQUIPSLOTS.BACK)
            local itemchest = inst.components.inventory:UnEquip(EQUIPSLOTS.chest)  --Test code.
            inst.components.inventory:GiveItem(itemchest)

Edited by FurryEskimo
Link to comment
Share on other sites

@penguin0616
Well I've had time to test my code, and read other forum posts, and something is definitely wrong..
All it does is print "About to test equipped item" and "No item".  It appears that 'item' is nil, whether I have an item on my body or not..
Every forum post I check has a different answer to this problem, but almost all seem to fail.  Basically, I don't know how to check the statistics of what I'm wearing, and it seems like few people do.

    --local item = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BACK or EQUIPSLOTS.BODY)  --Note: Back and body?
    local item = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
    --local item = player.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BODY)  --Both cause crash. 'Global' not defined.
    --local item = player.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)

    print("About to test equipped item")
    if not item == nil then
        if not item.percentused:value() > 0.5 and item.prefab == "blueamulet" then  --Note: Something here is broken.
            print("Condition true")
        else
            print("Condition false")
        end
    else
        print("No item")
    end

Edited by FurryEskimo
Link to comment
Share on other sites

function AutoUnequip(inst, data) 
	local owner = inst.components.inventoryitem:GetGrandOwner()
	if owner and owner.components.inventory:IsItemEquipped(inst) then -- check if someone is holding it and they have it equipped
		if data.percent <= 0.05 then -- check if its 5% or below
			--owner.components.inventory:Unequip(EQUIPSLOTS.BODY) -- expressly unequips
			owner.components.inventory:GiveItem(inst) -- implicitly unequips
		end
	end
end 

AddPrefabPostInit("blueamulet", function(inst) 
	if not TheWorld.ismastersim then return end
	inst:ListenForEvent("percentusedchange", function(inst, data) 
		AutoUnequip(inst, data) 
	end) 
end) 

@FurryEskimo

  • Like 2
Link to comment
Share on other sites

@penguin0616
Thanks!  I'm trying to implement it now, but it's causing the server to launch, then crash so hard the whole game closes.
I've tried implementing the code a few different ways, but presumably the unequip code either in the character's file or the modmain file, and the blue amulet code is added to the modmain, yes?

According to the error report, there's a bug when you ask if the world is the mastersim, but I've compared it to other code and they appear to be exactly the same.
AddPrefabPostInit("blueamulet", function(inst)  --Test code.
    if not TheWorld.ismastersim then return end
    inst:ListenForEvent("percentusedchange", function(inst, data)
        AutoUnequip(inst, data)
    end)
end)

image.thumb.png.32bedd83c2a30a7b5d34449d2bb0eeed.png

Link to comment
Share on other sites

@penguin0616
I made no changes.  I added the code where I guessed it needed to be (unequip to the character file as a new function, and blue amulet code to the modmain).  I guessed that there might be an issue since I already have a "AddPrefabPostInit("blueamulet", function(inst)", so I combined the two (by taking the "if not TheWorld.ismastersim then return end" and so, parts and putting it in the first), but that didn't help either..

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

×
×
  • Create New...