MF99K Posted June 10, 2016 Share Posted June 10, 2016 local function OnDeath(inst) if inst.deathcause = "darkness" or inst.deathcause = "charlie" then SpawnPrefab("rose") end end (don't worry, rose is set as a prefab) my functions usually have some trouble and I haven't had a chance to test the mod I'm making this for yet but this code theoretically would spawn a rose whenever a player died of darkness. does this code look correct? Link to comment Share on other sites More sharing options...
Aquaterion Posted June 10, 2016 Share Posted June 10, 2016 (edited) when comparing you need to use "==" not "=" so. Also I don't believe the rose has its own prefab, but its a normal flower. You also need to tell it where it should be placed. local function OnDeath(inst) if inst.deathcause == "darkness" or inst.deathcause == "charlie" then local rose = SpawnPrefab("flower") rose.Transform:SetPosition(inst.Transform:GetWorldPosition()) rose.animname = "rose" rose.AnimState:PlayAnimation(rose.animname) --not sure if this line is needed, but mostlikely end end Edited June 10, 2016 by Aquaterion Link to comment Share on other sites More sharing options...
MF99K Posted June 11, 2016 Author Share Posted June 11, 2016 18 hours ago, Aquaterion said: when comparing you need to use "==" not "=" so. Also I don't believe the rose has its own prefab, but its a normal flower. You also need to tell it where it should be placed. local function OnDeath(inst) if inst.deathcause == "darkness" or inst.deathcause == "charlie" then local rose = SpawnPrefab("flower") rose.Transform:SetPosition(inst.Transform:GetWorldPosition()) rose.animname = "rose" rose.AnimState:PlayAnimation(rose.animname) --not sure if this line is needed, but mostlikely end end as I said, I have the rose set as a prefab so it will work without the other lines you added besides transform. for world position, how can I set x and y coordinates to be slightly away from the point of death? Link to comment Share on other sites More sharing options...
Aquaterion Posted June 11, 2016 Share Posted June 11, 2016 (edited) 5 hours ago, mf99k said: as I said, I have the rose set as a prefab so it will work without the other lines you added besides transform. for world position, how can I set x and y coordinates to be slightly away from the point of death? either you can go the simple way and just add a random number to it local function OnDeath(inst) if inst.deathcause == "darkness" or inst.deathcause == "charlie" then local rose = SpawnPrefab("rose") local x,y,z = inst.Transform:GetWorldPosition() rose.Transform:SetPosition(x + math.random(-2,2),y,z + math.random(-2,2)) end end or you can add a bit of code to check if the land is actually valid local function OnDeath(inst) if inst.deathcause == "darkness" or inst.deathcause == "charlie" then local rose = SpawnPrefab("rose") local x,y,z = inst.Transform:GetWorldPosition() local tries = 0 while true do--a while loop to keep trying to generating a new number if last 1 wasnt valid local newx = x + math.random(-5,5) + math.random() -- x + (-5 to 5) + (0.00 to 1.00) local newz = z + math.random(-5,5) + math.random() local spawntile = TheWorld.Map:GetTileAtPoint(newx,y,newz) if spawntile ~= GROUND.IMPASSABLE and spawntile ~= GROUND.UNDERGROUND then rose.Transform:SetPosition(newx,y,newz) return elseif tries > 15 then -- there is a possibility that a person dies on water, with no land around, so after 15 tries, give up rose:Remove() return end tries = tries + 1 end end end Edited June 11, 2016 by Aquaterion Link to comment Share on other sites More sharing options...
MF99K Posted June 15, 2016 Author Share Posted June 15, 2016 On 6/11/2016 at 2:38 AM, Aquaterion said: either you can go the simple way and just add a random number to it local function OnDeath(inst) if inst.deathcause == "darkness" or inst.deathcause == "charlie" then local rose = SpawnPrefab("rose") local x,y,z = inst.Transform:GetWorldPosition() rose.Transform:SetPosition(x + math.random(-2,2),y,z + math.random(-2,2)) end end or you can add a bit of code to check if the land is actually valid local function OnDeath(inst) if inst.deathcause == "darkness" or inst.deathcause == "charlie" then local rose = SpawnPrefab("rose") local x,y,z = inst.Transform:GetWorldPosition() local tries = 0 while true do--a while loop to keep trying to generating a new number if last 1 wasnt valid local newx = x + math.random(-5,5) + math.random() -- x + (-5 to 5) + (0.00 to 1.00) local newz = z + math.random(-5,5) + math.random() local spawntile = TheWorld.Map:GetTileAtPoint(newx,y,newz) if spawntile ~= GROUND.IMPASSABLE and spawntile ~= GROUND.UNDERGROUND then rose.Transform:SetPosition(newx,y,newz) return elseif tries > 15 then -- there is a possibility that a person dies on water, with no land around, so after 15 tries, give up rose:Remove() return end tries = tries + 1 end end end it doesn't seem to be doing anything Link to comment Share on other sites More sharing options...
Aquaterion Posted June 15, 2016 Share Posted June 15, 2016 if you could specify some more information than just "didnt work" would be nice Link to comment Share on other sites More sharing options...
MF99K Posted June 15, 2016 Author Share Posted June 15, 2016 6 hours ago, Aquaterion said: if you could specify some more information than just "didnt work" would be nice i used the second code and the rose didn't spawn Link to comment Share on other sites More sharing options...
Aquaterion Posted June 15, 2016 Share Posted June 15, 2016 it seems that when dying from darkness, the deathcause is "NIL" for some reason.. Link to comment Share on other sites More sharing options...
DarkXero Posted June 16, 2016 Share Posted June 16, 2016 Then do something like local function OnGrueAttack(inst) if inst.components.health and inst.components.health:IsDead() then OnDeath(inst) end end inst:ListenForEvent("attackedbygrue", OnGrueAttack) Link to comment Share on other sites More sharing options...
MF99K Posted June 16, 2016 Author Share Posted June 16, 2016 5 hours ago, DarkXero said: Then do something like local function OnGrueAttack(inst) if inst.components.health and inst.components.health:IsDead() then OnDeath(inst) end end inst:ListenForEvent("attackedbygrue", OnGrueAttack) still no flowers. i checked to see if it was a prefab issue, but the prefab spawns fine if I use console Link to comment Share on other sites More sharing options...
DarkXero Posted June 16, 2016 Share Posted June 16, 2016 26 minutes ago, mf99k said: still no flowers. i checked to see if it was a prefab issue, but the prefab spawns fine if I use console inst:ListenForEvent("attackedbygrue", function(inst) if inst.components.health and inst.components.health:IsDead() then local rose = SpawnPrefab("rose") local x, y, z = inst.Transform:GetWorldPosition() local tries = 0 while true do -- a while loop to keep trying to generating a new number if last 1 wasnt valid local newx = x + math.random(-5, 5) + math.random() -- x + (-5 to 5) + (0.00 to 1.00) local newz = z + math.random(-5, 5) + math.random() local spawntile = TheWorld.Map:GetTileAtPoint(newx, y, newz) if spawntile ~= GROUND.IMPASSABLE and spawntile ~= GROUND.UNDERGROUND then rose.Transform:SetPosition(newx, y, newz) return elseif tries > 15 then -- there is a possibility that a person dies on water, with no land around, so after 15 tries, give up rose:Remove() return end tries = tries + 1 end end end) 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