Jump to content

Recommended Posts

[00:12:01]: [string "scripts/widgets/containerwidget.lua"]:29: attempt to index local 'widget' (a nil value)
LUA ERROR stack traceback:
    scripts/widgets/containerwidget.lua:29 in (method) old_container_open (Lua) <24-132>
    ../mods/workshop-365119238/modmain_segments/containerwidgetfunction.lua:21 in (method) Open (Lua) <20-60>
    scripts/screens/playerhud.lua:236 in (upvalue) OpenContainerWidget (Lua) <233-238>
    scripts/screens/playerhud.lua:246 in (method) OpenContainer (Lua) <240-248>
    scripts/components/container_replica.lua:277 in (method) Open (Lua) <263-284>
    scripts/components/container_replica.lua:89 in (field) fn (Lua) <87-91>
    scripts/scheduler.lua:177 in (method) OnTick (Lua) <155-207>
    scripts/scheduler.lua:371 in (global) RunScheduler (Lua) <369-377>
    scripts/update.lua:170 in () ? (Lua) <149-228>

For some reason I get this error only when I add caves into the world. It works fine without.

Here's the code:

local MakePlayerCharacter = require "prefabs/player_common"


local assets = {

        Asset( "ANIM", "anim/finnthehuman.zip" ),
        Asset( "ANIM", "anim/ghost_finnthehuman_build.zip" ),
}
local prefabs = {}

-- Custom starting items
local start_inv = {
"demonbloodsword"
}

-- When the character is revived from human
local function onbecamehuman(inst)
	-- Set speed when loading or reviving from ghost (optional)
	inst.components.locomotor.walkspeed = 4
	inst.components.locomotor.runspeed = 6
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)

    if not inst:HasTag("playerghost") then
        onbecamehuman(inst)
    end

    if inst.OnNewSpawn then
    	inst.components.inventory:Equip(SpawnPrefab("greenbackpack"))
    end
end


-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "finnthehuman.tex" )
    inst:AddTag("demonbloodsword_builder")

end

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
	-- choose which sounds this character will play
	inst.soundsname = "willow"

    inst:RemoveTag("scarytoprey")

    inst:ListenForEvent("equip", function()
		inst.AnimState:ClearOverrideSymbol("swap_hat")
		inst.AnimState:Show("hair")
	end)

	-- Stats	
	inst.components.health:SetMaxHealth(150)
	inst.components.hunger:SetMax(100)
	inst.components.sanity:SetMax(150)

	-- Hunger rate (optional)
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload
end


return MakePlayerCharacter("finnthehuman", prefabs, assets, common_postinit, master_postinit, start_inv)

I know it's failing at this line:

inst.components.inventory:Equip(SpawnPrefab("greenbackpack"))

I've tried other things like, spawning the backpack in the world - but when I pick it up, it disconnects the game.

Please advise on this, I'm so stumped..

24 minutes ago, Lumina said:

What is the greenbackpack ?
Because it seems to cause the error.

It's a prefab created with the mod.

Reminder that the mod works fine without caves - it's also strange that if I don't spawn the prefab inside the mod scripts itself and use the console log to spawn the greenbackpack in game, it works completely fine as it should be. (this is with caves)
Here's the code for the greenbackpack

local assets =
{
    Asset("ANIM", "anim/greenbackpack.zip"),
    Asset("ANIM", "anim/swap_greenbackpack.zip"),

    Asset("ATLAS", "images/inventoryimages/greenbackpack.xml"),
    Asset("IMAGE", "images/inventoryimages/greenbackpack.tex")
}

local function OnBlocked(owner) 
    owner.SoundEmitter:PlaySound("dontstarve/wilson/hit_armour") 
end

local function onequip(inst, owner) 

    owner.AnimState:OverrideSymbol("swap_body", "swap_greenbackpack", "swap_body")

    inst:ListenForEvent("blocked", OnBlocked, owner)

    if inst.components.container ~= nil then
        inst.components.container:Open(owner)
    end
end

local function onunequip(inst, owner) 


    owner.AnimState:ClearOverrideSymbol("swap_body")

    inst:RemoveEventCallback("blocked", OnBlocked, owner)

    if inst.components.container ~= nil then
    inst.components.container:Close(owner)
    end

    
end

local function onopen(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_open", "open")
end

local function onclose(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_close", "open")
end

local function fn(Sim)
	local inst = CreateEntity()
    
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()
    
    inst.AnimState:SetBank("backpack1")
    inst.AnimState:SetBuild("swap_greenbackpack")
    inst.AnimState:PlayAnimation("anim")

    MakeInventoryPhysics(inst)

    inst:AddTag("backpack")
    --inst:AddTag("irreplaceable")

	local minimap = inst.entity:AddMiniMapEntity()
	minimap:SetIcon("greenbackpack.png")

    inst.foleysound = "dontstarve/movement/foley/backpack"

    if not TheWorld.ismastersim then
        inst:DoTaskInTime(0.314, function() 
            inst.replica.container:WidgetSetup("piggyback")
            end)
        return inst
    end

    inst.entity:SetPristine()

    inst:AddComponent("characterspecific")

    inst:AddComponent("inspectable")

    inst:AddComponent("armor")
    inst.components.armor:InitCondition(TUNING.ARMORWOOD, TUNING.ARMORWOOD_ABSORPTION)

    inst:AddComponent("insulator")
    inst.components.insulator:SetInsulation(TUNING.INSULATION_TINY)
 
    inst:AddComponent("container")
    inst.components.container:WidgetSetup("piggyback")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.cangoincontainer = false
    inst.components.inventoryitem.imagename = "greenbackpack"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/greenbackpack.xml"

    inst:AddComponent("inspectable")
    
    inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose
    
    inst:AddComponent("equippable")
	inst.components.equippable.equipslot = EQUIPSLOTS.BODY

        if EQUIPSLOTS["PACK"] then
        inst.components.equippable.equipslot = EQUIPSLOTS.PACK
    else
        inst.components.equippable.equipslot = EQUIPSLOTS.BODY
    end
	
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
    inst.components.equippable.walkspeedmult = 1.0

    MakeHauntableLaunchAndDropFirstItem(inst)
    
    return inst
end

return Prefab( "common/inventory/greenbackpack", fn, assets) 

 

Edited by Tirimiru
2 hours ago, Tirimiru said:

it's also strange that if I don't spawn the prefab inside the mod scripts itself and use the console log to spawn the greenbackpack in game, it works completely fine as it should be. (this is with caves)

You said previously :

9 hours ago, Tirimiru said:

I've tried other things like, spawning the backpack in the world - but when I pick it up, it disconnects the game.

So does it crash every time you equip backpack in world with caves ?
 

The fact that it happens in world with caves mean that the problem is probably client side. In your world without caves you are host, in your world with caves, dedicated server and someone else world, you are client.


    if not TheWorld.ismastersim then
        inst:DoTaskInTime(0.314, function() 
            inst.replica.container:WidgetSetup("piggyback")
            end)
        return inst
    end

What is this line for ?

15 hours ago, Lumina said:

You said previously :

So does it crash every time you equip backpack in world with caves ?
 

The fact that it happens in world with caves mean that the problem is probably client side. In your world without caves you are host, in your world with caves, dedicated server and someone else world, you are client.



    if not TheWorld.ismastersim then
        inst:DoTaskInTime(0.314, function() 
            inst.replica.container:WidgetSetup("piggyback")
            end)
        return inst
    end

What is this line for ?

Apparently that line is for players that aren't the host of the server to have the widget set up for the custom backpack.

Okay so, without adding caves to the world, the mod works completely fine and works using the line

inst.components.inventory:Equip(SpawnPrefab("greenbackpack"))

The greenbackpack gets equipped onto the mod character on new spawn.

When I add caves to the world, it crashes with that line and gives me the widget error. So I remove the line completely - works fine. It also works fine when I use the console log in-game to spawn the greenbackpack and pick it up and equip it.

But if I write the script to spawn the greenbackpack, e.g.

SpawnPrefab("greenbackpack")

it does spawn in-game, but when i pick it up, it disconnects the game.

The only way it doesn't get issues is if I don't spawn the greenbackpack within the script itself, and that's a problem.

Sorry if that's confusing for you, hope you understand better now

Edited by Tirimiru

Sorry for reviving a dead thread, but I just wanted to says I figured out a solution that worked for me, and wanted to put it here in case anyone else needs help and finds this.

Why does this problem happen? It happens because even though the server knows all the properties of your container, the client doesn't know a thing, since containers are handled differently when you aren't the server.

You need to this inside your prefab's main function, and it needs to happen when the client is still reading it, and only for the clients. To do this, you need to add it in the part of your prefab's main function that only executes on clients, and not on the server. This is inside an if statement that looks like this:

if not TheWorld.ismastersim then
	return inst
end

To add it in, simply paste this line inside the if statement, before return inst, just be sure to change CONTAINER_NAME to whatever the name of your container is.

inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup("CONTAINER_NAME") end

If you don't have anything else special already inside the client only part of the main function, it should look something like this

if not TheWorld.ismastersim then
	inst.OnEntityReplicated = function(inst) inst.replica.container:WidgetSetup("CONTAINER_NAME") end
	return inst
end

 

I hope this helps anyone who finds this, and I'm sorry of it doesn't work for you, and once again, sorry for reviving an almost 4 year old thread.

  • Like 5
  • Thanks 1
  • Health 1

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