Jump to content

Recommended Posts

This is a follow-up from a post I made a couple weeks ago. In that post, I recieved the following code:

Quote

 

local function sanityfn(inst) 
      local sanity_tresh = inst.components.sanity.current 

    return sanity_tresh <= 50 and -5/60 or 0 -- if sanity is lower or equal than 50, reduce 5 sanity per minute, else, do nothing. 
end

 

The code is fine, does exactly what I wanted. But I put it through some playtesting, and a friend pointed out that there are a couple easy ways to get over 50 Sanity and be in the clear. Even the sanity monsters themselves can be chain-slain to boost sanity immensly, which largely defeats the purpose of the threshold to begin with.

Now I don't want to just make the threshold more than 50, or adjust the rate to make it drain too fast to counter burst sanity gain. The main idea I had was setting it up to also lower sanity gain from all sources by say, 50% as an example. This would keep the numbers where I'd want them, but also keep the difficulty of the whole trait by denying the easy way out.

If any aid can be provided for this one, that'd be grand!

@TheAlphaDehur

You can hook the function inst.components.sanity.DoDelta, and alter how much delta arrives based on the current sanity value.

If you're unfamiliar with function hooking, I wrote a guide that should help with that here:

 

  • Like 2

Quick update: So I gave the guide a read, and with the DoDelta function you provided I figured I could create a seperate function using the previous code's structure:

Quote

 

local function sanityperc(inst)
    local sanity_debuf = inst.components.sanity.current

    return sanity_debuf <= 50 and 0.50 or 0 -- Same as above, but 0.50 is 50%.
end

 

I'm not sure if this would have actually worked or not, with the 0.50 value, because I got hit with this error:

"attempt to index field 'components' (a nil value)"

I presume it's because DoDelta is a server component compared to custom_rate_fn? I'll keep trying to figure out why that's happening, but I think my inexperience with LUA is showing again so help is still appreciated!

ErrorImage.png

Edited by TheAlphaDehur
Added an image of the error.
2 hours ago, TheAlphaDehur said:

Quick update: So I gave the guide a read, and with the DoDelta function you provided I figured I could create a seperate function using the previous code's structure:

I'm not sure if this would have actually worked or not, with the 0.50 value, because I got hit with this error:

"attempt to index field 'components' (a nil value)"

I presume it's because DoDelta is a server component compared to custom_rate_fn? I'll keep trying to figure out why that's happening, but I think my inexperience with LUA is showing again so help is still appreciated!

Both functions act on the server, the client gets updated on sanity value changes (once they hit a whole number threshold, about).

From scripts/components/sanity.lua:

function Sanity:DoDelta(delta, overtime)

Which is the same as:

Sanity.DoDelta = function(self, delta, overtime)

The first argument 'self' refers to the sanity component, delta being the amount changed, and overtime a boolean meaning if the effect is an over time effect (used in sounds playing mainly).

Each component stores the component's owner as the 'inst' variable on its table.

So for you to access a different component that isn't sanity, you can:

self.inst.components.health

Since your checks are for the sanity level, you can directly do:

self.current

This DoDelta stuff is to be done in addition to your custom rate gains; you keep your rate modulator as-is and add this as another thing.

  • Like 2

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