Jump to content

[Help] Turf Mod


Recommended Posts

Hi! I am trying to make a custom turfs mod using a mod I found from don't starve called 'Sample Tile'. I have this mod working for the most part but I have run into an issue. When I dig the turf back up with a pitchfork it does not give the item back to my inventory. Can anyone help me with this? Here is the code inside the turf_test.lua prefab:

 

local TARGET_TILE = GROUND.MODTEST


require "prefabutil"

local function ondeploy(inst, pt, deployer)
if deployer and deployer.SoundEmitter then
deployer.SoundEmitter:PlaySound("dontstarve/wilson/dig")
end

local map = TheWorld.Map
local original_tile_type = map:GetTileAtPoint(pt:Get())
local x, y = map:GetTileCoordsAtPoint(pt:Get())
if x and y then
map:SetTile(x,y, inst.data.tile)
map:RebuildLayer( original_tile_type, x, y )
map:RebuildLayer( inst.data.tile, x, y )
end

local minimap = TheWorld.minimap.MiniMap
minimap:RebuildLayer(original_tile_type, x, y)
minimap:RebuildLayer(inst.data.tile, x, y)

inst.components.stackable:Get():Remove()
end


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

local prefabs =
{
"gridplacer",
}


local data = {name = "turf_test", anim = "carpet", tile = TARGET_TILE}

local function fn(Sim)

local inst = CreateEntity()

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

MakeInventoryPhysics(inst)

inst:AddTag("groundtile")

inst.AnimState:SetBank("turf")
inst.AnimState:SetBuild("turf")
inst.AnimState:PlayAnimation(data.anim)

inst:AddTag("molebait")
MakeDragonflyBait(inst, 3)

inst.entity:SetPristine()


inst:AddComponent("stackable")
inst.components.stackable.maxsize = TUNING.STACK_SIZE_LARGEITEM

inst:AddComponent("inspectable")
inst:AddComponent("inventoryitem")
inst.components.inventoryitem:ChangeImageName("turf_carpetfloor")
inst.data = data

inst:AddComponent("bait")

inst:AddComponent("fuel")
inst.components.fuel.fuelvalue = TUNING.MED_FUEL
MakeMediumBurnable(inst, TUNING.MED_BURNTIME)
MakeSmallPropagator(inst)
MakeHauntableLaunchAndIgnite(inst)

inst:AddComponent("deployable")
--inst.components.deployable:SetDeployMode(DEPLOYMODE.ANYWHERE)
inst.components.deployable.ondeploy = ondeploy
if data.tile == "webbing" then
inst.components.deployable:SetDeployMode(DEPLOYMODE.ANYWHERE)
else
inst.components.deployable:SetDeployMode(DEPLOYMODE.TURF)
end
inst.components.deployable:SetUseGridPlacer(true)

---------------------
return inst
end

return Prefab( "common/objects/turf_test", fn, assets, prefabs)

 

I would appreciate any tips as well in cleaning up the code and how to do this/understand this better. Be gentle, I am new to this and pretty much at this point learning by copy/paste trial and error. =) Thanks!

Link to comment
Share on other sites

@Sarifynna, there is a missing

        if not TheWorld.ismastersim then            return inst        end

under inst.entity:SetPristine().

This would most likely case a crash for people joining dedicated servers with the mod.

 

However the turf not appearing is unrelated to this.

 

The turf appearing back on the ground is thanks to the terraformer component of the pitchfork.

You have a table linking ground types and prefabs there.

But we can't reach it.

 

So we have to override everything.

-- We append a function to run with initialized terraform componentsAddComponentPostInit("terraformer", function(self)	-- Using the same SpawnTurf function	local function SpawnTurf(turf, pt)		if turf ~= nil then			local loot = GLOBAL.SpawnPrefab(turf)			loot.Transform:SetPosition(pt:Get())			if loot.Physics ~= nil then				local angle = math.random() * 2 * GLOBAL.PI				loot.Physics:SetVel(2 * math.cos(angle), 10, 2 * math.sin(angle))			end		end	end	-- We save old Terraform	local _Terraform = self.Terraform	-- We make a new one using the old one	self.Terraform = function(self, pt)		-- We pretty much need the initial checks because we need the original_tile_type info		local world = GLOBAL.TheWorld		local map = world.Map		if not map:CanTerraformAtPoint(pt:Get()) then			return false		end		local original_tile_type = map:GetTileAtPoint(pt:Get())		-- We got what we want, run like normal		local ret = _Terraform(self, pt)		if ret == true then			-- If something got digged (means ret was true, returned), if the original was our turf, we give back a turf			if original_tile_type == GLOBAL.GROUND.MODTEST then -- turf tile				SpawnTurf("turf_test", pt) -- turf prefab			end			return true		end		return false	endend)

in modmain.

Link to comment
Share on other sites

@Sarifynna, yes, first you make a table on top

local MOD_GROUND_TURFS = {	[GLOBAL.GROUND.MODTEST] = "turf_test",}

and then you use this lines

local turf_prefab = MOD_GROUND_TURFS[original_tile_type]if turf_prefab ~= nil then	SpawnTurf(turf_prefab, pt)end

for to check if the table has a value for the tile, being used as key.

 

If there is a value, then it was a mod turf, and the value is the prefab to spawn, so we spawn it.

 

Now all you need to do is to add more pairs in the mod table, in the same fashion as the turf_test.

Edited by DarkXero
Link to comment
Share on other sites

@DarkXero I'm getting this as an error:

 

[00:00:20]: Mod: SampleTile DST (Sample Tile DST) Loading modmain.lua
[00:00:20]: Mod: SampleTile DST (Sample Tile DST) Error loading mod!
[string "../mods/SampleTile DST/modmain.lua"]:69: table index is nil
LUA ERROR stack traceback:
../mods/SampleTile DST/modmain.lua(69,1) in main chunk
=[C] in function 'xpcall'
scripts/util.lua(560,1) in function 'RunInEnvironment'
scripts/mods.lua(416,1) in function 'InitializeModMain'
scripts/mods.lua(397,1) in function 'LoadMods'
scripts/main.lua(248,1) in function 'ModSafeStartup'
scripts/main.lua(299,1)
=[C] in function 'SetPersistentString'
scripts/mainfunctions.lua(25,1) in function 'SavePersistentString'
scripts/modindex.lua(79,1)
=[C] in function 'GetPersistentString'
scripts/modindex.lua(66,1) in function 'BeginStartupSequence'
scripts/main.lua(298,1) in function 'callback'
scripts/modindex.lua(519,1)
=[C] in function 'GetPersistentString'
scripts/modindex.lua(499,1) in function 'Load'
scripts/main.lua(297,1) in main chunk

 

SampleTile DST.zip

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