Jump to content

Recommended Posts

Hello there, I wanna add a feather into DST which is similar with Shipwrecked, which is, when an entity with foodtype of meat is landed on water, a hound will spawn nearby.

I am starting with the SpawnPrefab("hound") lines. and they seem not be working when added to the "Floater" class. Please give me a hand.:grin:
 

AddComponentPostInit("Floater",function(self, inst)
    local old_OnLandedServer = self.OnLandedServer
    
    self.OnLandedServer = function()
                    local spawnprefab=GLOBAL.SpawnPrefab
                    local x, y, z = self.inst.Transform:GetWorldPosition()
                    local sharx1 = spawnprefab("hound")
                    sharx1.Physics:Teleport(x, y, z)
                    --sharx1.Transform:SetPosition(x, y, z)
        old_OnLandedServer()

    end
end)

However, Nothing happens when I created a DST server.

10 hours ago, AmberH said:

 


AddComponentPostInit("Floater",function(self, inst)
    local old_OnLandedServer = self.OnLandedServer
    
    self.OnLandedServer = function()
                    local spawnprefab=GLOBAL.SpawnPrefab
                    local x, y, z = self.inst.Transform:GetWorldPosition()
                    local sharx1 = spawnprefab("hound")
                    sharx1.Physics:Teleport(x, y, z)
                    --sharx1.Transform:SetPosition(x, y, z)
        old_OnLandedServer()

    end
end)

However, Nothing happens when I created a DST server.

I think case sensitivity is the biggest issue here, but here's how I'd do it:

local SpawnPrefab = GLOBAL.SpawnPrefab

AddComponentPostInit(
    "floater",
    function(inst)
        -- Potential client-side stuff here
        if not TheWorld.ismastersim
        then
            return
        end
        -- Other server-side stuff here
        local OnLandedServer_old = self.OnLandedServer
        self.OnLandedServer = function(self, ...)
            local sharx1 = SpawnPrefab("hound")
            local x, y, z = self.inst.Transform:GetWorldPosition()
            sharx1.Transform:SetPosition(x, y, z)
            return OnLandedServer_old(self, ...)
        end
    end
)

Doing a safeguard check for it being on the server with the TheWorld.ismastersim check, varadic argument and returning the old one to help future proof it.  You can read my tutorial on future proofing function hooking at:

 

  • Like 1
18 hours ago, CarlZalph said:

 

Doing a safeguard check for it being on the server with the TheWorld.ismastersim check, varadic argument and returning the old one to help future proof it.  You can read my tutorial on future proofing function hooking at:

 

Thank you CarlZalph, your tutorial did show basic rules of parameters delivery of a function.

I tried to learn from other MODs today, and finally finished my coding as below^ ^

AddComponentPostInit("floater", function(self)

        local OnLandedServer_old = self.OnLandedServer
        self.OnLandedServer = function(self, ...)
            if (self.inst.Transform ~=nil) then
                local x, y, z = self.inst.Transform:GetWorldPosition()
				local isOnOcean = GLOBAL.TheWorld.Map:IsOceanTileAtPoint(x, 0, z)
		        if isOnOcean==true then
                    if ((self.inst.prefab~=nil) and (self.inst.prefab ~= "shark_fin")) and (not self.inst:HasTag("monstermeat")) and self.inst.components.edible and (self.inst.components.edible.foodtype == FOODTYPE.MEAT or self.inst.components.edible.foodtype == "MEAT") and (not self.inst:HasTag("spawnnosharx"))  then	        
                        if math.random() <= 0.005 * self.inst.components.edible.hungervalue then
                            local xx, zz, angle
						    local i = 0
						    repeat
                                angle = math.random() * 2 * 3.1415926
                                xx=x + math.cos(angle)*12
                                zz=z + math.sin(angle)*12
                                isOnOcean = GLOBAL.TheWorld.Map:IsOceanTileAtPoint(xx, 0, zz)
                                i = i + 1                    
                            until (isOnOcean == true) or (i > 10)
                            if isOnOcean == true then
                                local sharx3 = SpawnPrefab("sharx")
                                sharx3.Transform:SetPosition(xx, y, zz)	
                            end
					    end			    
				    end
				end
            
		    end
            return OnLandedServer_old(self, ...)
        end
    end
)

This seems to be working under simple tests.

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