Jump to content

Recommended Posts

I recently got into modding DST, and I'm currently working on a character whose sanity is supposed to drain constantly, even during the day, and worse at night. Though being in proximity to other players will stop it from draining, and being around things such as plants, animals and a specific character will make it go up.
Can anyone help me with how I'm meant to code this? I've already sat for hours trying to get something to work but I just can't figure it out.

The best way of achieving this would probably be with `custom_rate_fn`. You can set it up in your character to react to certain conditions and it would add onto normal sanity drain.

inst.components.sanity.custom_rate_fn = function(inst, dt)
    -- Your code here
  
    return 0 -- Should return a number value
end

 

You can check what day cycle the world is in with `TheWorld.state.isday` / `TheWorld.state.isnight` / `TheWorld.state.isdusk`.
You can check proximity to players, or any entities really, with `TheSim:FindEntities()` or simply `FindEntity()` if you only need to search for 1.

-- TheSim:FindEntities() returns a table of entities that pass the tag criteria

local ents = TheSim:FindEntities(x, y, z, range, musttags, canttags, mustoneoftags)
if #ents > 0 then
    -- There is at least 1 entity that meets the tag requirements
end
-- FindEntity is a global function that returns an entity if it finds one that matches the tag criteria, returns `nil` otherwise

local ent = GLOBAL.FindEntity(inst, radius, fn, musttags, canttags, mustoneoftags)
if ent then
    -- There is an entity that matches the tag requirements
end

 

  • Like 1
On 4/11/2023 at 7:48 PM, -t- said:
inst.components.sanity.custom_rate_fn = function(inst, dt)
    -- Your code here
  
    return 0 -- Should return a number value
end

Which code you mean is meant to go here?
Also the main thing I'm confused about is how it works for things to make it go specifically up or down. It's not important (unless it is), but it would be nice to know
Another thing is also placing in the script, though perhaps I should just get off my lazy ass and try to learn some LUA basics instead of just hopping onto it like I'm doing.
Thank you for your input either way

Edited by NatTheJar

Well, the sanity component recalculates players sanity every tick. There is a Sanity:OnUpdate() function inside the component that runs Sanity:Recalc(), checking for many different conditions and variables to determine whether sanity should lower or not and by how much.

Inside Sanity:Recalc() there is this statement:

if self.custom_rate_fn ~= nil then
    --NOTE: dt param was added for wormwood's custom rate function
    --      dt shouldn't have been applied to the return value yet
    self.rate = self.rate + self.custom_rate_fn(self.inst, dt)
end

So it simply adds on the returned value of custom_sanity_fn.

 

You should set up your custom_rate_rn inside of your characters master_postinit function.

  • Like 1

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
×
  • Create New...