AmberH Posted December 4, 2021 Share Posted December 4, 2021 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. 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. Link to comment https://forums.kleientertainment.com/forums/topic/135836-how-to-override-a-funcion-of-class-floater/ Share on other sites More sharing options...
CarlZalph Posted December 4, 2021 Share Posted December 4, 2021 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: 1 Link to comment https://forums.kleientertainment.com/forums/topic/135836-how-to-override-a-funcion-of-class-floater/#findComment-1519014 Share on other sites More sharing options...
AmberH Posted December 5, 2021 Author Share Posted December 5, 2021 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. Link to comment https://forums.kleientertainment.com/forums/topic/135836-how-to-override-a-funcion-of-class-floater/#findComment-1519143 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