Jump to content

Recommended Posts

I am a novice mod maker and have learned how to create a handheld item. But now I want to make a building, and I don't know what its necessary components are. Its basic function is to have four compartments, place an item, and then click the button to convert it to another item in the backpack item column.

Edited by tpmove
The problem has been resolved

Now I have implemented it as a box, which generates a set of ice when I close it, but when I open the dedicated server, it will report an error

This is the error log

[00:01:41]: [string "scripts/widgets/containerwidget.lua"]:31: attempt to index local 'widget' (a nil value)
LUA ERROR stack traceback:
    scripts/widgets/containerwidget.lua:31 in (method) Open (Lua) <25-158>
    scripts/screens/playerhud.lua:367 in (upvalue) OpenContainerWidget (Lua) <352-373>
    scripts/screens/playerhud.lua:381 in (method) OpenContainer (Lua) <375-383>
    scripts/components/container_replica.lua:438 in (method) Open (Lua) <424-451>
    scripts/components/container_replica.lua:145 in (field) fn (Lua) <137-151>
    scripts/scheduler.lua:186 in (method) OnTick (Lua) <164-216>
    scripts/scheduler.lua:409 in (global) RunScheduler (Lua) <407-415>
    scripts/update.lua:240 in () ? (Lua) <224-298>

 

This is my code,it looks very messy

require "prefabutil"
 
local assets = {
    Asset("ANIM", "anim/testcookpot.zip"),
    Asset("ANIM", "anim/swap_testcookpot.zip"),
    Asset("ANIM", "anim/testcookpot_placer.zip"),
    Asset("ANIM", "anim/swap_testcookpot_placer.zip"),
    Asset("ANIM", "anim/ui_chest_3x3.zip"),
    Asset("ATLAS", "images/inventoryimages/testbuild.xml"),
}
 
local prefabs =
{
    "collapse_small",
}
 
local function onopen(inst)
    inst.AnimState:PlayAnimation("idle")
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_open")
end
 
local function onclose(inst)
   inst.AnimState:PlayAnimation("idle")
   inst.SoundEmitter:PlaySound("dontstarve/common/icebox_close")
   for i = 1, 40, 1 do
        inst.components.container:GiveItem(SpawnPrefab("ice"))   --GiveItem()有四个参数第一个是物品名字,第二个是生成位置
   end
end
 
local function onhammered(inst, worker)
    inst.components.lootdropper:DropLoot()
    inst.components.container:DropEverything()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("metal")
    inst:Remove()
end
 
local function onhit(inst, worker)
    inst.AnimState:PlayAnimation("idle")
    inst.components.container:DropEverything()
    inst.AnimState:PushAnimation("idle", false)
    inst.components.container:Close()
end
 
local function onbuilt(inst)
    inst.AnimState:PlayAnimation("idle")
    -- inst.AnimState:PushAnimation("closed", false)
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_craft")
end
 
local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform() -- 添加变换组件,一般只需要添加组件就行了
    inst.entity:AddAnimState() -- 添加动画组件
    inst.entity:AddSoundEmitter()   --添加声音组件
    inst.entity:AddNetwork() -- 添加网络组件
 
    inst:AddTag("structure")
    inst:AddTag("chest")
 
    MakeObstaclePhysics(inst, .5) -- 设置物品拥有一般物品栏物体的物理特性,这是一个系统封装好的函数,内部已经含有对物理引擎的设置
 
    inst.AnimState:SetBank("testcookpot") -- 设置动画属性 Bank lotus_umbrella
    inst.AnimState:SetBuild("testcookpot") -- 设置动画属性 Build lotus_umbrella
    inst.AnimState:PlayAnimation("idle") -- 设置默认播放动画为idle
 
   
    -- 以下这几句是设置网络状态的,并且作为一个分界线,从这个if then 块往上是主客机通用代码,往下则是只限于主机使用的代码。
    if not TheWorld.ismastersim then
        return inst
    end
 
    inst.entity:SetPristine()
    inst:AddComponent("container")      --容器组件
    inst.components.container:WidgetSetup("icebox")
   
    inst.components.container.onopenfn = onopen     --打开时调用函数
    inst.components.container.onclosefn = onclose   --关闭时调用函数
    inst.components.container.skipclosesnd = true
    inst.components.container.skipopensnd = true
   
   
 
   
    inst:AddComponent("lootdropper")
 
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(2)
    inst.components.workable:SetOnFinishCallback(onhammered)
    inst.components.workable:SetOnWorkCallback(onhit)
 
    inst:ListenForEvent("onbuilt", onbuilt)
 
    return inst
end
 
return Prefab("testcookpot", fn, assets, prefabs),
       MakePlacer("common/testcookpot_placer", "testcookpot_placer", "testcookpot_placer", "idle", true)
52 minutes ago, tpmove said:

Now I have implemented it as a box, which generates a set of ice when I close it, but when I open the dedicated server, it will report an error

This is the error log

[00:01:41]: [string "scripts/widgets/containerwidget.lua"]:31: attempt to index local 'widget' (a nil value)
LUA ERROR stack traceback:
    scripts/widgets/containerwidget.lua:31 in (method) Open (Lua) <25-158>
    scripts/screens/playerhud.lua:367 in (upvalue) OpenContainerWidget (Lua) <352-373>
    scripts/screens/playerhud.lua:381 in (method) OpenContainer (Lua) <375-383>
    scripts/components/container_replica.lua:438 in (method) Open (Lua) <424-451>
    scripts/components/container_replica.lua:145 in (field) fn (Lua) <137-151>
    scripts/scheduler.lua:186 in (method) OnTick (Lua) <164-216>
    scripts/scheduler.lua:409 in (global) RunScheduler (Lua) <407-415>
    scripts/update.lua:240 in () ? (Lua) <224-298>

 

This is my code,it looks very messy

require "prefabutil"
 
local assets = {
    Asset("ANIM", "anim/testcookpot.zip"),
    Asset("ANIM", "anim/swap_testcookpot.zip"),
    Asset("ANIM", "anim/testcookpot_placer.zip"),
    Asset("ANIM", "anim/swap_testcookpot_placer.zip"),
    Asset("ANIM", "anim/ui_chest_3x3.zip"),
    Asset("ATLAS", "images/inventoryimages/testbuild.xml"),
}
 
local prefabs =
{
    "collapse_small",
}
 
local function onopen(inst)
    inst.AnimState:PlayAnimation("idle")
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_open")
end
 
local function onclose(inst)
   inst.AnimState:PlayAnimation("idle")
   inst.SoundEmitter:PlaySound("dontstarve/common/icebox_close")
   for i = 1, 40, 1 do
        inst.components.container:GiveItem(SpawnPrefab("ice"))   --GiveItem()有四个参数第一个是物品名字,第二个是生成位置
   end
end
 
local function onhammered(inst, worker)
    inst.components.lootdropper:DropLoot()
    inst.components.container:DropEverything()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("metal")
    inst:Remove()
end
 
local function onhit(inst, worker)
    inst.AnimState:PlayAnimation("idle")
    inst.components.container:DropEverything()
    inst.AnimState:PushAnimation("idle", false)
    inst.components.container:Close()
end
 
local function onbuilt(inst)
    inst.AnimState:PlayAnimation("idle")
    -- inst.AnimState:PushAnimation("closed", false)
    inst.SoundEmitter:PlaySound("dontstarve/common/icebox_craft")
end
 
local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform() -- 添加变换组件,一般只需要添加组件就行了
    inst.entity:AddAnimState() -- 添加动画组件
    inst.entity:AddSoundEmitter()   --添加声音组件
    inst.entity:AddNetwork() -- 添加网络组件
 
    inst:AddTag("structure")
    inst:AddTag("chest")
 
    MakeObstaclePhysics(inst, .5) -- 设置物品拥有一般物品栏物体的物理特性,这是一个系统封装好的函数,内部已经含有对物理引擎的设置
 
    inst.AnimState:SetBank("testcookpot") -- 设置动画属性 Bank lotus_umbrella
    inst.AnimState:SetBuild("testcookpot") -- 设置动画属性 Build lotus_umbrella
    inst.AnimState:PlayAnimation("idle") -- 设置默认播放动画为idle
 
   
    -- 以下这几句是设置网络状态的,并且作为一个分界线,从这个if then 块往上是主客机通用代码,往下则是只限于主机使用的代码。
    if not TheWorld.ismastersim then
        return inst
    end
 
    inst.entity:SetPristine()
    inst:AddComponent("container")      --容器组件
    inst.components.container:WidgetSetup("icebox")
   
    inst.components.container.onopenfn = onopen     --打开时调用函数
    inst.components.container.onclosefn = onclose   --关闭时调用函数
    inst.components.container.skipclosesnd = true
    inst.components.container.skipopensnd = true
   
   
 
   
    inst:AddComponent("lootdropper")
 
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(2)
    inst.components.workable:SetOnFinishCallback(onhammered)
    inst.components.workable:SetOnWorkCallback(onhit)
 
    inst:ListenForEvent("onbuilt", onbuilt)
 
    return inst
end
 
return Prefab("testcookpot", fn, assets, prefabs),
       MakePlacer("common/testcookpot_placer", "testcookpot_placer", "testcookpot_placer", "idle", true)

Try this

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

 

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