Jump to content

Recommended Posts

Hello, I need some help with this I got no idea what to do :).

So, thanks to help of Aquaterion my custom structure now has an action to swap one of its parts, now what I want to do is make game save the part it's swapped to then when player logs out of world they don't have to change it back every single time to what they want :).

Here's all the code of my stucture so far.

canvas.lua

Spoiler

local assets =
{
	Asset("ANIM", "anim/canvas.zip"),
	Asset("ANIM", "anim/canvas_art_1.zip"),
}

local prefabs = 
{
}

SetSharedLootTable('canvas',
{
    {'boards',		1.00},
    {'papyrus',		1.00},
})

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

local function onhit(inst, worker)
    if not inst:HasTag("burnt") then
        inst.AnimState:PlayAnimation("hit")
        inst.AnimState:PushAnimation("idle", false)
    end
end

local function onhammered(inst, worker)
	if inst.components.burnable ~= nil and inst.components.burnable:IsBurning() then
		inst.components.burnable:Extinguish()
	end
    inst.components.lootdropper:DropLoot()
	local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("wood")
    inst:Remove()
end

--local function OnSave(inst, data)  
--end

--local function OnLoad(inst, data)
--end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()
	MakeObstaclePhysics(inst, 1.1)

	--inst.MiniMapEntity:SetPriority(5)
    --inst.MiniMapEntity:SetIcon(name..".png")

    inst.AnimState:SetBank("canvas")
    inst.AnimState:SetBuild("canvas")
    inst.AnimState:PlayAnimation("idle", true)
	
	inst:AddTag("collider")
	inst:AddTag("structure")
	inst:AddTag("canvas")
	
	inst:ListenForEvent("onbuilt", onbuilt)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
	
	--inst:AddComponent("named")
	
	--[[
	local _random = math.random(1,2)
	if _random == 1 then
		--inst.components.named:SetName("Painting 1")
		inst.painting0 = nil
	elseif _random == 2 then
		inst.AnimState:OverrideSymbol("art", "canvas_art_1", "art")
		--inst.components.named:SetName("Painting 2")
		inst.painting0 = true
	end
	--]]
	
    MakeLargeBurnable(inst)
    MakeMediumPropagator(inst)
	inst.components.burnable:SetBurnTime(15)
	
    inst:AddComponent("inspectable")
    inst:AddComponent("lootdropper")
	
	inst:AddComponent("paintable")

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

	inst:AddComponent("hauntable")
	inst.components.hauntable:SetHauntValue(TUNING.HAUNT_TINY)
	
	--inst.OnSave = OnSave
	--inst.OnLoad = OnLoad

    return inst
end

return Prefab("canvas", fn, assets, prefabs), 
MakePlacer("canvas_placer", "canvas", "canvas", "idle", false, nil, nil, nil, 0, nil, nil)

 

modmain.lua

Spoiler

AddPrefabPostInit("featherpencil", function(inst)
	if not GLOBAL.TheWorld.ismastersim then
		return inst
	end
	inst:AddComponent("paintingtool")
end)


GLOBAL.STRINGS.ACTIONS.DRAW_ART = "Draw"

local DRAW_ART = AddAction("DRAW_ART", "Draw Art", function(act)
	if act.doer.components.inventory then	
		if act.target.components.burnable ~= nil then
			if act.target.components.burnable:IsBurning() or act.target.components.burnable:IsSmoldering() then
				return false
			end
		end

        if act.invobject then

			if act.target.painting0 == nil then
				act.target.painting0 = true
				act.target.AnimState:OverrideSymbol("art", "canvas_art_1", "art")
			else
				act.target.painting0 = nil
				act.target.AnimState:ClearOverrideSymbol("art")
			end

			act.target.SoundEmitter:PlaySound("dontstarve/common/together/draw")
			act.invobject.components.paintingtool:Paint(act.target)
			return true
        end
    end
end)
DRAW_ART.priority = 0

AddComponentAction("USEITEM", "paintingtool", function(inst, doer, target, actions, right)
	if target:HasTag("canvas") then
		table.insert(actions, DRAW_ART)
	end
end)

AddStategraphActionHandler("wilson", GLOBAL.ActionHandler(DRAW_ART, "dolongaction"))
AddStategraphActionHandler("wilson_client", GLOBAL.ActionHandler(DRAW_ART, "dolongaction"))

 

paintable.lua component (butchered copypaste of drawable.lua)

Spoiler

local function oncanpaint(self, canpaint)
    if canpaint then
        self.inst:AddTag("paintable")
    else
        self.inst:RemoveTag("paintable")
    end
end

local Paintable = Class(function(self, inst)
    self.inst = inst
	
	self.canpaint = true
	self.onpaintfn = nil
	self.paint = nil
	--self.onchange = nil

end,
nil,
{
	canpaint = oncanpaint,
})

function Paintable:OnRemoveFromEntity()
    self.inst:RemoveTag("paintable")
end

function Paintable:SetCanPaint(canpaint)
    self.canpaint = canpaint
end

function Paintable:CanPaint()
    return self.canpaint
end

function Paintable:SetOnPaintFn(fn)
    self.onpaintfn = fn
end

function Paintable:OnPaint(paint)

	
    if self.paint ~= paint then
        self.paint = paint
        if self.onpaintfn ~= nil then
            self.onpaintfn(self.inst, paint)
        end
    end
	
end

--[[
function Paintable:SetOnChangeFn(fn)
    self.onchange = fn
end
--]]

--[[
function Paintable:OnSave()
    
end

function Paintable:LoadPostPass(savedata)
    
end
--]]

return Paintable


	

 

paintingtool.lua component (butchered copypaste of drawingtool.lua)

Spoiler

local PaintingTool = Class(function(self, inst)
    self.inst = inst

    self.onpaintfn = nil
end)

function PaintingTool:SetOnPaintFn(fn)
    self.onpaintfn = fn
end

function PaintingTool:Paint(target, paint)
    if target ~= nil and target.components.paintable ~= nil then
        target.components.paintable:OnPaint(paint)
        if self.onpaintfn ~= nil then
            self.onpaintfn(self.inst, target, paint)
        end
    end
end

return PaintingTool

 

 

Basically what I want is when the action is modmain.lua is called I want value "act.target.painting0 = value" to be saved than in a OnLoad function I can put code to make the thing have the correct part, the only problem is I don't know how to do any of this so I'm kinda stuck, so it'd really be great if someone can tell me how I can save those values with OnSave & OnLoad :D!!! Thanks so much for your time, have a great day/night :D!!!!!

 

Edited by SuperDavid

if you just want to save painting0 then you should be able to

function Paintable:OnSave()
    return 
	{
		painting0 = self.inst.painting0 or nil
	}
end

function Paintable:Load(data)
    self.inst.painting0 = data.painting0 or nil
end

but i'm not sure why you have this variable on your object, and if you're going to have it on your object, then why not save it on the object directly?

Edited by Aquaterion
13 minutes ago, Aquaterion said:

if you just want to save painting0 then you should be able to

I am supposed to put this in paintable.lua right? I did and unfortunately nothing happened.

 

13 minutes ago, Aquaterion said:

but i'm not sure why you have this variable on your object, and if you're going to have it on your object, then why not save it on the object directly?

I don't know how to save it on my object, I know nothing about OnSave OnLoad.

The reason I have this variable is because I want use it for switching my art of my canvas because it's only way I know how, for example.

Spoiler

if act.target.painting0 == nil then
	act.target.painting0 = true
	act.target.AnimState:OverrideSymbol("art", "canvas_art_1", "art")
elseif act.target.painting0 == true then
	act.target.painting1 = true
	act.target.AnimState:OverrideSymbol("art", "canvas_art_2", "art")
elseif act.target.painting1 == true then
	act.target.painting2 = true
	act.target.AnimState:OverrideSymbol("art", "canvas_art_3", "art")
elseif act.target.painting2 == true then
	act.target.painting3 = true
	act.target.AnimState:OverrideSymbol("art", "canvas_art_4", "art")
	ect...
end

 

And so I want to save the value then if there's like 20 arts I don't want every time player have to preform action so many times if the art takes lots of action to get too

Edited by SuperDavid
24 minutes ago, Aquaterion said:

inst.OnSave = function() data.art = inst.art end

inst.OnLoad = function(inst, data) if data ~= nil and data.art then inst.art = data.art end end

Wow, so simple yet im dumb couldnt figure it out thanks a lot dude :wilson_smile:!

@Aquaterion can the same thing be used for all prefabs? like characters too not just structures?

Edited by SuperDavid

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