Jump to content

just wanted to make sure this function makes sense


Recommended Posts

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

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 by Aquaterion
Link to comment
Share on other sites

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

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 by Aquaterion
Link to comment
Share on other sites

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

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

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

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