Jump to content

Lose sanity for killing (DST)


Recommended Posts

I'm getting started modding custom characters, and long story short- I'm not used to scripting yet or know how to work scripts properly. The character I'm creating is a Vegan, and I also wanted her to lose sanity from killing any small creatures (prey & birds). I wanted to create a character that was 'one with nature'. So far so good, animals do not run from her and she will absolutely not eat meat, but the issue was making her lose sanity for killing.

Code I used:

Quote

 

local function onkill(inst, data)
    if data.cause == inst
        and data.inst:HasTag("prey") then
    inst.components.sanity:DoDelta(-10)
    end
end

    inst:ListenForEvent("entity_death", function(wrld, data) --I put this in the master_postinit
    onkill(inst, data) end, TheWorld)


 

The game does not crash, but she doesn't lose sanity either.

Link to comment
Share on other sites

7 hours ago, Chexanh said:

I'm getting started modding custom characters, and long story short- I'm not used to scripting yet or know how to work scripts properly. The character I'm creating is a Vegan, and I also wanted her to lose sanity from killing any small creatures (prey & birds). I wanted to create a character that was 'one with nature'. So far so good, animals do not run from her and she will absolutely not eat meat, but the issue was making her lose sanity for killing.

Code I used:

The game does not crash, but she doesn't lose sanity either.

well... on the 9th line world was spelt 'wrld' maybe that's your problem...

Link to comment
Share on other sites

I believe data.cause contains prefab name of the attacker and data.afflicter the actual attacker entity.

Try

local function onkill(inst, data)
    print(string.format("[vegan|onkill] inst=%s, data=%s %s", tostring(inst), tostring(data), type(data) == "table" and tabletodictstring(data) or ""))
    if data.afflicter == inst
        and data.inst:HasTag("prey") then
        inst.components.sanity:DoDelta(-10)
    end
end

 

Link to comment
Share on other sites

7 hours ago, NeddoFreddo said:

well... on the 9th line world was spelt 'wrld' maybe that's your problem...

I previously checked other topics and asked someone, and that was given to me. To make sure that wasn't my problem though I changed 'wrld' to 'world' and went into game. The game neither crashed, but the function didn't work either. ^^

 

5 hours ago, Muche said:

I believe data.cause contains prefab name of the attacker and data.afflicter the actual attacker entity.

Try


local function onkill(inst, data)
    print(string.format("[vegan|onkill] inst=%s, data=%s %s", tostring(inst), tostring(data), type(data) == "table" and tabletodictstring(data) or ""))
    if data.afflicter == inst
        and data.inst:HasTag("prey") then
        inst.components.sanity:DoDelta(-10)
    end
end

 

Thank you! ^^ This worked for me and my character now loses sanity from killing.

Link to comment
Share on other sites

There is also killed event that could be utilized (it's pushed after entity_death and death events, so there might be some edge cases where it behaves differently from the above code using entity_death event):

local function onkill(inst, data)
    print(string.format("[vegan|onkill] inst=%s, data=%s %s", tostring(inst), tostring(data), type(data) == "table" and tabletodictstring(data) or ""))
    if data and data.victim and data.victim:HasTag("prey") then
        inst.components.sanity:DoDelta(-10)
    end
end

    inst:ListenForEvent("killed", onkill) 

 

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