Jump to content

Function to detect what is killed


Recommended Posts

Hello everyone, i am looking for a way in wich i can detect what mob is killed by the player, looking specifically for shadow creatures, so that they can boost a little the sanity they give.

This is what i have so far obtained directly from wigfrids lua, but i don't get what exactly does the function "IsValidVictim", can someone make a basic explanation of this, does it return the full entity of the victim or just it's tags, please: 

local function IsValidVictim(victim)
    return victim ~= nil
        and not ((victim:HasTag("prey") and not victim:HasTag("hostile")) or
                victim:HasTag("veggie") or
                victim:HasTag("structure") or
                victim:HasTag("wall") or
                victim:HasTag("companion"))
        and victim.components.health ~= nil
        and victim.components.combat ~= nil
end

local function onkilled(inst, data)
    local sntybst = 0
    local victim = data.victim
    if IsValidVictim(victim) then
        if (victim:HasTag("shadowcreature") then
            inst.components.sanity:DoDelta(victim.sanityreward or TUNING.SANITY_SMALL)
        end
    end
end

Problem is i can't test it right now, so can anyone help me out telling me if it is correct??, thanks in advance your opinions are wellcome.

Link to comment
Share on other sites

3 hours ago, RinShigure said:

wigfrids lua

When an entity with combat component kills another entity with combat component, the event "killed" gets pushed on the killer.

inst:ListenForEvent("killed", onkilled)

Wigfrid listens to that one. If we go to the combat component, the event is pushed with data containing the victim.

attacker:PushEvent("killed", { victim = self.inst })

onkilled will receive a table that has one key "victim" with a value that is the killed entity.

    local victim = data.victim
    if IsValidVictim(victim) then

onkilled will extract the victim value and call a function with it.

local function IsValidVictim(victim)
    return victim ~= nil
        and not ((victim:HasTag("prey") and not victim:HasTag("hostile")) or
                victim:HasTag("veggie") or
                victim:HasTag("structure") or
                victim:HasTag("wall") or
                victim:HasTag("companion"))
        and victim.components.health ~= nil
        and victim.components.combat ~= nil
end

This function returns true if the victim is an entity (and therefore not nil), if the victim is not prey (small creature), a vegetable, a wall, or companion pets. The victim also needs to have a health and a combat component.

If this returns true, then Wigfrid will go through and call:

        if not victim.components.health.nofadeout and (victim:HasTag("epic") or math.random() < .1) then
            local time = victim.components.health.destroytime or 2
            local x, y, z = victim.Transform:GetWorldPosition()
            local scale = (victim:HasTag("smallcreature") and smallScale)
                        or (victim:HasTag("largecreature") and largeScale)
                        or medScale
            inst:DoTaskInTime(time, spawnspirit, x, y, z, scale)
        end
                                                                                                   

So if the victim doesn't have nofadeout (default is false), and the victim is a giant (entity with epic tag), or the victim isn't a giant but there's a 10% chance to happen anyways, then a ghostly appearance will spawn over the victim.

You know, that ectoplasm white thing that sometimes appears when you kill stuff.

latest?cb=20140430091402

Makes sense to have it sometimes for stuff you kill a lot (spiders) and always for stuff you sometimes kill (giants).

 

And yes, your code would work.

You can even reduce it to:

local function onkilled(inst, data)
	local victim = data.victim
	if victim and victim:HasTag("shadowcreature") then
		inst.components.sanity:DoDelta(victim.sanityreward or TUNING.SANITY_SMALL)
	end
end

Because we know that stuff with "shadowcreature" tag most likely passes the IsValidVictim test.

Just check for a nil just in case. And also remember to add the ListenForEvent in the master_postinit of your character.

Link to comment
Share on other sites

@DarkXero Really thank you for your help, i did understand the function that shows the soul of the victim but i really appreciate your explanation, and i like the way you did it, i'll try it out as soon as i can.

Just got a little doubt with the "~" character i havent seen this character in any languages i have used, what is it for??

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