Sarifynna Posted August 27, 2015 Share Posted August 27, 2015 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.MODTESTrequire "prefabutil"local function ondeploy(inst, pt, deployer)if deployer and deployer.SoundEmitter thendeployer.SoundEmitter:PlaySound("dontstarve/wilson/dig")endlocal map = TheWorld.Maplocal original_tile_type = map:GetTileAtPoint(pt:Get())local x, y = map:GetTileCoordsAtPoint(pt:Get())if x and y thenmap:SetTile(x,y, inst.data.tile)map:RebuildLayer( original_tile_type, x, y )map:RebuildLayer( inst.data.tile, x, y )endlocal minimap = TheWorld.minimap.MiniMapminimap:RebuildLayer(original_tile_type, x, y)minimap:RebuildLayer(inst.data.tile, x, y)inst.components.stackable:Get():Remove()endlocal 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_LARGEITEMinst:AddComponent("inspectable")inst:AddComponent("inventoryitem")inst.components.inventoryitem:ChangeImageName("turf_carpetfloor")inst.data = datainst:AddComponent("bait")inst:AddComponent("fuel")inst.components.fuel.fuelvalue = TUNING.MED_FUELMakeMediumBurnable(inst, TUNING.MED_BURNTIME)MakeSmallPropagator(inst)MakeHauntableLaunchAndIgnite(inst)inst:AddComponent("deployable")--inst.components.deployable:SetDeployMode(DEPLOYMODE.ANYWHERE)inst.components.deployable.ondeploy = ondeployif data.tile == "webbing" theninst.components.deployable:SetDeployMode(DEPLOYMODE.ANYWHERE)elseinst.components.deployable:SetDeployMode(DEPLOYMODE.TURF)endinst.components.deployable:SetUseGridPlacer(true)---------------------return instendreturn 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 More sharing options...
DarkXero Posted August 28, 2015 Share Posted August 28, 2015 @Sarifynna, there is a missing if not TheWorld.ismastersim then return inst endunder 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 More sharing options...
Sarifynna Posted August 28, 2015 Author Share Posted August 28, 2015 (edited) Thank you @DarkXero! That worked perfectly I wasn't even close to figuring that out, you're amazing! =) Is there a simple way for me to make that work with multiple turf prefabs? Edited August 28, 2015 by Sarifynna Link to comment Share on other sites More sharing options...
DarkXero Posted August 28, 2015 Share Posted August 28, 2015 (edited) @Sarifynna, yes, first you make a table on toplocal MOD_GROUND_TURFS = { [GLOBAL.GROUND.MODTEST] = "turf_test",}and then you use this lineslocal turf_prefab = MOD_GROUND_TURFS[original_tile_type]if turf_prefab ~= nil then SpawnTurf(turf_prefab, pt)endfor 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 August 28, 2015 by DarkXero Link to comment Share on other sites More sharing options...
Sarifynna Posted August 28, 2015 Author Share Posted August 28, 2015 (edited) Thank you for your help @DarkXero, I am not sure how to implement your fix. Would you mind showing me how to properly do it if you have time and show me what I am doing wrong? Thank you =)SampleTile DST.zip Edited August 28, 2015 by Sarifynna Link to comment Share on other sites More sharing options...
DarkXero Posted August 29, 2015 Share Posted August 29, 2015 @Sarifynna, here, I edited the modmain only: modmain.lua Link to comment Share on other sites More sharing options...
Sarifynna Posted August 29, 2015 Author Share Posted August 29, 2015 @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 nilLUA 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 More sharing options...
DarkXero Posted August 29, 2015 Share Posted August 29, 2015 @Sarifynna, some assets are missing.I commented stuff that caused errors.SampleTile DST.zip Link to comment Share on other sites More sharing options...
Sarifynna Posted August 30, 2015 Author Share Posted August 30, 2015 Thank you very much for your time and patience @DarkXero You're the most helpful person on the forums and we all really appreciate it! I have my mod a lot closer to functioning properly because of you. =) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now