Jump to content

i need help with my mod pls :(


Recommended Posts

I saw my light But couldn't see a friend's light 
And friends don't see my lights either.

 

Is there any code that I made a mistake? I need help

vvvvv( mycode )vvvvv

local assets=
{
    Asset("ANIM", "anim/brass_lantern.zip"),
    Asset("ANIM", "anim/swap_brass_lantern.zip"),
        Asset("SOUND", "sound/wilson.fsb"),
}

local function turnon(inst)
    if not inst.components.fueled:IsEmpty() then
        if not inst.components.machine.ison then
            if inst.components.fueled then
                inst.components.fueled:StartConsuming()        
            end
            inst.Light:Enable(true)
            inst.AnimState:PlayAnimation("idle_on")

            if inst.components.equippable:IsEquipped() then
                inst.components.inventoryitem.owner.AnimState:OverrideSymbol("swap_object","swap_brass_lantern", "swap_lantern_on")
                inst.components.inventoryitem.owner.AnimState:Show("LANTERN_OVERLAY") 
                
            end
            inst.components.machine.ison = true

            inst.SoundEmitter:PlaySound("dontstarve/wilson/lantern_on")
            inst.SoundEmitter:PlaySound("dontstarve/wilson/lantern_LP", "loop")

            inst.components.inventoryitem:ChangeImageName("brass_lantern")
        end
    end
end

local function turnoff(inst)
    if inst.components.fueled then
        inst.components.fueled:StopConsuming()        
    end

    inst.Light:Enable(false)
    inst.AnimState:PlayAnimation("idle_off")

    if inst.components.equippable:IsEquipped() then
        inst.components.inventoryitem.owner.AnimState:OverrideSymbol("swap_object", "swap_brass_lantern", "swap_lantern_off")
        inst.components.inventoryitem.owner.AnimState:Hide("LANTERN_OVERLAY") 
    end

    inst.components.machine.ison = false

    inst.SoundEmitter:KillSound("loop")
    inst.SoundEmitter:PlaySound("dontstarve/wilson/lantern_off")

    inst.components.inventoryitem:ChangeImageName("brass_lantern")
end

local function OnLoad(inst, data)
    if inst.components.machine and inst.components.machine.ison then
        inst.AnimState:PlayAnimation("idle_on")
        turnon(inst)
    else
        inst.AnimState:PlayAnimation("idle_off")
        turnoff(inst)
    end
end

local function ondropped(inst)
    turnoff(inst)
    turnon(inst)
end

local function onpickup(inst)
    turnon(inst)
end

local function onputininventory(inst)
    turnoff(inst)
end

local function onequip(inst, owner) 
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal")
    owner.AnimState:OverrideSymbol("lantern_overlay", "swap_brass_lantern", "lantern_overlay")
    
    if inst.components.fueled:IsEmpty() then
        owner.AnimState:OverrideSymbol("swap_object", "swap_brass_lantern", "swap_lantern_off")
        owner.AnimState:Hide("LANTERN_OVERLAY") 
    else
        owner.AnimState:OverrideSymbol("swap_object", "swap_brass_lantern", "swap_lantern_on")
        owner.AnimState:Show("LANTERN_OVERLAY") 
    end
    turnon(inst)
end

local function onunequip(inst, owner) 
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal")
    owner.AnimState:ClearOverrideSymbol("lantern_overlay")
    owner.AnimState:Hide("LANTERN_OVERLAY")     
end

local function nofuel(inst)
    local owner = inst.components.inventoryitem and inst.components.inventoryitem.owner
    if owner then
        owner:PushEvent("torchranout", {torch = inst})
    end

    turnoff(inst)
end

local function takefuel(inst)
    if inst.components.equippable and inst.components.equippable:IsEquipped() then
        turnon(inst)
    end
end

local function fuelupdate(inst)
    local fuelpercent = inst.components.fueled:GetPercent()
    inst.Light:SetIntensity(Lerp(0.4, 0.6, fuelpercent))
    inst.Light:SetRadius(Lerp(3, 5, fuelpercent))
    inst.Light:SetFalloff(.9)
end

local function fn(Sim)
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()        
    MakeInventoryPhysics(inst)
    
    inst.entity:AddNetwork()
    
    inst.AnimState:SetBank("lantern")
    inst.AnimState:SetBuild("brass_lantern")
    inst.AnimState:PlayAnimation("idle_off")

    -- anim:SetBank("lantern")
    -- anim:SetBuild("lantern")
    -- anim:PlayAnimation("idle_off")

    inst:AddTag("light")

    inst:AddComponent("inspectable")
    
       inst.entity:AddLight()
    --inst.Light:SetFalloff(0.4)
    --inst.Light:SetIntensity(3)
    --inst.Light:SetRadius(10)
    inst.Light:SetColour(220/255, 200/255, 155/255)

    
             if not TheWorld.ismastersim then
       return inst
    end
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/brass_lantern.xml"
    inst.components.inventoryitem:SetOnDroppedFn(ondropped)
    --inst.components.inventoryitem:SetOnPickupFn(makesmalllight)
    --inst.components.inventoryitem:SetOnActiveItemFn(makesmalllight)
    inst.components.inventoryitem:SetOnPutInInventoryFn(onputininventory)    

    
        inst:AddComponent("equippable")
    inst:AddComponent("fueled")

    inst:AddComponent("machine")
    inst.components.machine.turnonfn = turnon
    inst.components.machine.turnofffn = turnoff
    inst.components.machine.cooldowntime = 0
    inst.components.machine.caninteractfn = function() return not inst.components.fueled:IsEmpty() end


    inst.components.fueled.fueltype = "BURNABLE"
    inst.components.fueled:InitializeFuelLevel(TUNING.LANTERN_LIGHTTIME*0.50)
    inst.components.fueled:SetDepletedFn(nofuel)
    inst.components.fueled:SetUpdateFn(fuelupdate)
    inst.components.fueled.ontakefuelfn = takefuel
    inst.components.fueled.accepting = true

    fuelupdate(inst)

    
    inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip ) 

    inst.OnLoad = OnLoad

    return inst
end


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

 

Captureaa.PNG

Link to comment
Share on other sites

if I remember correctly, lights are entities tied to the object and must be called to spawn in through a separate prefab.

we treat them as a follower, otherwise the network will not register the light and it will be local only.


 

   

-- an example of equipting and spawning a local entity 
	local function onequip(inst, owner)
-- print ("spawn light prfab on equip")        
        inst.fire = SpawnPrefab("examplelight")
        local follower = inst.fire.entity:AddFollower()
        inst.fire.Follower:FollowSymbol(owner.GUID, "swap_body", 0, 0, 0)
    end


-- example of deleting the entity when you take the item off
local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")
    inst:RemoveEventCallback("blocked", OnBlocked, owner)
 --    print ("deleting ent")		
	inst.fire:Remove()
    inst.fire = nil
    end


--Example of item on ground putting off light
local function Droppedexample(inst)		
	if inst.fire == nil then
		inst.fire = SpawnPrefab("examplelight")
		local follower = inst.fire.entity:AddFollower()
		follower:FollowSymbol(inst.GUID, "swap_object", 0, -110, 1)   
		end
end

--example of final local function
local function fn()
  local inst = CreateEntity()
  	inst:ListenForEvent("ondropped", example) 
end


-- ####################################seprate lua file that will be called when entity is called
local function fn()

 print ("ent light has spawned")		
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddLight()
    inst.entity:AddNetwork()

    inst:AddTag("FX")

	inst.Light:Enable(true)
    inst.Light:SetRadius(2)
    inst.Light:SetFalloff(0.25)
    inst.Light:SetIntensity(0.25)
    inst.Light:SetColour(255 / 255, 255 / 255, 255/ 255) 

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.persists = true

    return inst
end

return Prefab("examplelight", fn)

 

  • Like 1
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...