Jump to content

Recommended Posts

I have customized a workstation, and it basically works as expected. However, the icon on the label is too small, and I don't know what to do with it.

Additionally, I want a particular prefab to be craftable only once per day. How could I make it?
Maybe I should listen to something and change the recipe tag, but I didn't find the right event.
Any help would be greatly appreciated!

icon.png

Edited by yanecc

My current code is as following, which will cause panic when a new day comes.

AddRecipe2("free_drink", {}, TECH.APPLESTORE,
    { product = "goatmilk", nounlock = true, builder_tag = "firstorder", no_deconstruction = true })

AddPlayerPostInit(function(inst)
    inst:ListenForEvent("makerecipe", function(inst, data)
        if data.recipe.name == "free_drink" then
            inst:RemoveTag("firstorder")
        end
    end)
end)

local function AddFirstOrderTagToPlayer(inst, player)
    local day = GLOBAL.TheWorld.state.cycles
    local key = player.userid .. "_" .. tostring(day)

    if not inst.TradingRecord[key] then
        inst.TradingRecord[key] = true
        player:AddTag("firstorder")
    end
end

local function AddFirstOrderTagToAllPlayers(inst)
    for _, player in ipairs(GLOBAL.AllPlayers) do
        AddFirstOrderTagToPlayer(inst, player)
    end
end

local function OnDayComplete(inst)
    inst:DoTaskInTime(0, AddFirstOrderTagToAllPlayers(inst))
end

local function OnPlayerJoined(inst, player)
    AddFirstOrderTagToPlayer(inst, player)
end

AddPrefabPostInit("world", function(inst)
    local _OnSave = inst.OnSave
    local function onSave(inst, data, ...)
        data.TradingRecord = inst.TradingRecord
        if _OnSave ~= nil then _OnSave(inst, data, ...) end
    end
    inst.OnSave = onSave

    local _OnLoad = inst.OnLoad
    local function onLoad(inst, data, ...)
        if data ~= nil then
            inst.TradingRecord = data.TradingRecord or {}
        end
        if _OnLoad ~= nil then
            return _OnLoad(inst, data, ...)
        end
    end
    inst.OnLoad = onLoad

    inst:WatchWorldState("cycles", OnDayComplete)
    inst:ListenForEvent("ms_playerjoined", OnPlayerJoined)
    AddFirstOrderTagToAllPlayers(inst)
end)

 

QQ截图20240727025721.png

Edited by yanecc
48 minutes ago, _zwb said:

你把制作栏的贴图做大点不就行了

我对mod贴图方面不太了解,原图片是64x64的尺寸,我尝试过将图标放大到256x256以后重新打包,替换原图片重新上传以后,无法正确显示放大后的图标,而是被替换成了默认的制作图标。我无法确定是对图标的尺寸有要求,又或者我遗漏了某些步骤。

QQ20240731004754.png.7f2eaeb64c9bfd2684a98e0926d767ea.png

3 hours ago, Harryyw said:

单从你这个报错页面来看,在AddFirstOrderTagToPlayer里,如果世界没有TradingRecord的话要先设置一下呀,不然后边直接访问TradingRecord[key]不就报错了。

你是对的,我之前只是在inst.OnLoad中初始化变量,但这是不够的,看起来world的inst.OnLoad函数并没有在进入游戏时加载,所以仍需要在prefab主函数中初始化变量。感谢你的提醒!

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