Sign in to follow this  
KXWl1

Need help with a character

Recommended Posts

KXWl1    7

I want to make a chracter who loses sanity per min when his temperature is low and gains sanity per min when his temperature is high, but I don't know what to do.

Share this post


Link to post
Share on other sites
CarlZalph    3,390

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.

Share this post


Link to post
Share on other sites
KXWl1    7
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?

Share this post


Link to post
Share on other sites
Ultroman    619

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

Share this post


Link to post
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

Sign in to follow this