Jump to content

Recommended Posts

Create a variable that's stored to the entity, listen for event "temperaturedelta" to modify this variable to your liking, and finally use the sanity component's "custom_rate_fn" custom function to apply the variable as a sanity rate by returning your variable in a wrapper function.

14 hours ago, CarlZalph said:

Create a variable that's stored to the entity, listen for event "temperaturedelta" to modify this variable to your liking, and finally use the sanity component's "custom_rate_fn" custom function to apply the variable as a sanity rate by returning your variable in a wrapper function.

Thanks, could you give me some examples?

Here's an example:

-- Put this part somewhere outside master_postinit
local myCustomSanityChangePerSecond = 1

local mySanityRateFunction = function(inst, deltaTime)
	return myCustomSanityChangePerSecond
end

-- Put this part in master_postinit
inst.components.sanity.custom_rate_fn = mySanityRateFunction

Then, somewhere else in the code, you can modify the myCustomSanityChangePerSecond variable. In the example below, I have done as @CarlZalph ("Wololooo") suggested and hooked up a custom function to a listener listening for "temperaturedelta", which is the event called when SetTemperature() is called on the temperature component. You want your myCustomSanityChangePerSecond to be calculated using the temperature, so listening for changes to it is a good way to go.

-- Put this part somewhere outside master_postinit
local myTemperatureChangeFunction = function(inst, data)
	-- Here we can access the data from the event. In the case of "temperaturedelta",
	-- we get data.last and data.new, which are the previous temperature value
	-- and the new temperature value, respectively.
	-- Do whatever calculations you want with them, to find the current sanity rate you want,
	-- and set myCustomSanityChangePerSecond to something.
	myCustomSanityChangePerSecond = 0
end

-- Put this part in master_postinit
inst:ListenForEvent("temperaturedelta", myTemperatureChangeFunction)

 

Edited by Ultroman

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