Jump to content

Prefab invisible when placed by player


Recommended Posts

  • Hello, I have a problem with my mod. Actually, I am working with an example from steam workshop and for some reason when I use c_spawn everything is working fine but if I craft and place prefab on the ground it is invisible but when is relog it is working fine. This prefab contains some code from the original firepit script.

 

ModMain.lua:

PrefabFiles = {
	"myprefab",
}
local assets=
{
    Asset("ATLAS", "images/inventoryimages/myprefab.xml"),
    Asset("IMAGE", "images/inventoryimages/myprefab.tex"),
}
AddMinimapAtlas("images/inventoryimages/myprefab.xml")

local myrecipe = AddRecipe("myprefab", -- name
{Ingredient("boards", 1)}, -- ingredients Add more like so , {Ingredient("boards", 1), Ingredient("rope", 2), Ingredient("twigs", 1), etc}
GLOBAL.RECIPETABS.FARM, -- tab ( FARM, WAR, DRESS etc)
GLOBAL.TECH.NONE, -- level (GLOBAL.TECH.NONE, GLOBAL.TECH.SCIENCE_ONE, etc)
"myprefab_placer", -- placer
nil, -- min_spacing
nil, -- nounlock
nil, -- numtogive
nil, -- builder_tag
"images/inventoryimages/myprefab.xml", -- atlas
"myprefab.tex") -- image

GLOBAL.STRINGS.NAMES.MYPREFAB = "My Prefab" --It's name in-game
GLOBAL.STRINGS.RECIPE_DESC.MYPREFAB = "It's a custom prefab!" --recipe description

myprefab.lua

require "prefabutil"
require "recipe"
require "modutil"
local assets=
{
    Asset("ANIM", "anim/myprefab.zip"),
    Asset("ATLAS", "images/inventoryimages/myprefab.xml"),
    Asset("IMAGE", "images/inventoryimages/myprefab.tex"),
}
local deltaTime = .1
--------------------------------
local function onhammered(inst, worker)
    inst.components.lootdropper:DropLoot()
    inst:Remove()
end

local function onhit(inst, worker)
    inst.AnimState:PlayAnimation("hit")
    inst.AnimState:PushAnimation("idle")
end

local function onextinguish(inst)
    if inst.components.fueled ~= nil then
        inst.components.fueled:InitializeFuelLevel(0)
    end
end

local function ontakefuel(inst)
    inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel")
end

local function updatefuelrate(inst)
    inst.components.fueled.rate = TheWorld.state.israining and 1 + TUNING.FIREPIT_RAIN_RATE * TheWorld.state.precipitationrate or 1
end

local function onupdatefueled(inst)
    if inst.components.burnable ~= nil and inst.components.fueled ~= nil then
        updatefuelrate(inst)
        inst.components.burnable:SetFXLevel(inst.components.fueled:GetCurrentSection(), inst.components.fueled:GetSectionPercent())
    end

end

local function onfuelchange(newsection, oldsection, inst, doer)
    if newsection <= 0 then
        inst.components.burnable:Extinguish()
    else
        if not inst.components.burnable:IsBurning() then
            updatefuelrate(inst)
            inst.components.burnable:Ignite(nil, nil, doer)
        end
        inst.components.burnable:SetFXLevel(newsection, inst.components.fueled:GetSectionPercent())
    end
end

local SECTION_STATUS =
{
    [0] = "OUT",
    [1] = "EMBERS",
    [2] = "LOW",
    [3] = "NORMAL",
    [4] = "HIGH",
}
local function getstatus(inst)
    return SECTION_STATUS[inst.components.fueled:GetCurrentSection()]
end

local function onbuilt(inst)
    inst.AnimState:PlayAnimation("place")
    inst.AnimState:PushAnimation("idle", false)
    inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel")
end

local function OnInit(inst)
    if inst.components.burnable ~= nil then
        inst.components.burnable:FixFX()
    end
end
----------------------------------

local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()
    local minimap = inst.entity:AddMiniMapEntity()
    minimap:SetIcon( "myprefab.tex" )

    inst.AnimState:SetBank("myprefab")
    inst.AnimState:SetBuild("myprefab")
    inst.AnimState:PlayAnimation("idle",false)

    inst:AddTag("structure")

    MakeObstaclePhysics(inst, 0.66)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    -----------------------
    inst:AddComponent("burnable")
    --inst.components.burnable:SetFXLevel(2)
    inst.components.burnable:AddBurnFX("campfirefire", Vector3(0, 0, 0), "firefx", true)
    inst:ListenForEvent("onextinguish", onextinguish)

    -------------------------
    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(4)
    inst.components.workable:SetOnFinishCallback(onhammered)
    inst.components.workable:SetOnWorkCallback(onhit)

    -------------------------
    inst:AddComponent("fueled")
    inst.components.fueled.maxfuel = 40
    inst.components.fueled.accepting = true

    inst.components.fueled:SetSections(4)
    inst.components.fueled.bonusmult = 2
    inst.components.fueled:SetTakeFuelFn(ontakefuel)
    inst.components.fueled:SetUpdateFn(onupdatefueled)
    inst.components.fueled:SetSectionCallback(onfuelchange)
    inst.components.fueled:InitializeFuelLevel(0)

    -----------------------------

    inst:AddComponent("inspectable")

    inst:ListenForEvent("onbuilt", onbuilt)

    inst:DoTaskInTime(0, OnInit)
    print("init end")
    return inst
end
return Prefab( "common/myprefab", fn, assets, prefabs),
        MakePlacer( "common/myprefab_placer", "myprefab", "myprefab", "idle" )

 

Link to comment
Share on other sites

A guess would be that your prefab doesn't have a "place" animation. The only place it's used is in onbuilt, which is the function called when building the thing. If you just spawn it, it'll adhere to the animation you've set for it in fn(0) (which in this case is "idle"). A quick fix would be to replace:

local function onbuilt(inst)
    inst.AnimState:PlayAnimation("place")
    inst.AnimState:PushAnimation("idle", false)
    inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel")
end

with this

local function onbuilt(inst)
    inst.AnimState:PlayAnimation("idle", false)
    inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel")
end

...but then you're missing a nice placement animation.

Edited by Ultroman
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...