Jump to content

Recommended Posts

I've been fiddling again with modding.
 The thing I am trying to do, is to make that the character, stead of losing health when freezing or overheating, they lose sanity. I've looked around the temperature.lua and spotted the 'startfreezing' and 'startoverheating', I think I could do something with a listenforevent or maybe make a component but I am not entirelly sure where to start or whether my approach is the best.

help would be greatly appreciated.

UPDATE: I did it, found a way to do that, it just took me a bit of stubborness.
 Well since it is posted, I will just drop the code here.

 

local function ontemperaturechange(inst)
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then
    return
elseif inst.components.temperature.current > 2 and inst.components.temperature.current < 68 then
	inst.components.sanity.dapperness = nil
    elseif inst.components.temperature.current < 1 then
        inst.components.sanity.dapperness = (-TUNING.DAPPERNESS_LARGE * 40)
        
    elseif inst.components.temperature.current > 69 then
        inst.components.sanity.dapperness = (-TUNING.DAPPERNESS_LARGE * 40)
    end
end

and in master_postinit

inst:ListenForEvent("sanitydelta", ontemperaturechange)



    inst.components.temperature.hurtrate = 0

 

Edited by halfrose
Resolved it myself.
3 hours ago, halfrose said:

 


local function ontemperaturechange(inst)
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then
    return
    elseif inst.components.temperature.current < 1 then
        inst.components.sanity.dapperness = (-TUNING.DAPPERNESS_LARGE * 40)
        
    elseif inst.components.temperature.current > 69 then
        inst.components.sanity.dapperness = (-TUNING.DAPPERNESS_LARGE * 40)
    end
end

and in master_postinit


inst:ListenForEvent("sanitydelta", ontemperaturechange)



    inst.components.temperature.hurtrate = 0

 

I tried to use that code for my character (Finally I found it, thanks.), reducing the sanity penalty. And the sanity drain, didn't removed. 

Edited by Neutral_Steve
removed the text.
1 minute ago, Neutral_Steve said:

I tried to use that code for my character (Finally I found it, thanks.), reducing the sanity penalty. And the sanity drain, didn't removed. 

are you sure you placed it on the right area, the local function ontemperature change is outside the master_postinit, the rest goes inside it.

Also make sure you are not adding a number that is too low, literally, I had to add such an absurd number first to make sure it was working.

6 minutes ago, halfrose said:

are you sure you placed it on the right area, the local function ontemperature change is outside the master_postinit, the rest goes inside it.

Also make sure you are not adding a number that is too low, literally, I had to add such an absurd number first to make sure it was working.

Yep. 

2016-02-16_00003.thumb.jpg.7aaabd5c016c5

15 minutes ago, Neutral_Steve said:

I tried to use that code for my character (Finally I found it, thanks.), reducing the sanity penalty. And the sanity drain, didn't removed. 

OOPS, my bad, I realized I forgot to paste this into the coding as well

elseif inst.components.temperature.current > 2 and inst.components.temperature.current < 68 then
    inst.components.sanity.dapperness = nil

it goes into the ontemperaturechange I updated the main post.

3 hours ago, halfrose said:

UPDATE: I did it, found a way to do that, it just took me a bit of stubborness.
 Well since it is posted, I will just drop the code here.

You're code is working in a very hackish kinda way.

Try the following code.

local _TemperatureUpdate = inst.components.temperature.OnUpdate
function inst.components.temperature:OnUpdate( dt, applyhealthdealta )

	-- Call the old function, making sure not to apply health damage.
	_TemperatureUpdate( self, dt, false )

	-- Do sanity damage here.
	if self.current < 0 then
		self.inst.components.sanity:DoDelta(5)
	elseif self.current > self.overheattemp then
		self.inst.components.sanity:DoDelta(5)
	end
end

This code gets placed in your master_postinit and will do it in a much nicer fashion.

Edited by Kzisor
Fixed code, forgot a 'then' statement.
3 minutes ago, halfrose said:

OOPS, my bad, I realized I forgot to paste this into the coding as well


elseif inst.components.temperature.current > 2 and inst.components.temperature.current < 68 then
    inst.components.sanity.dapperness = nil

it goes into the ontemperaturechange I updated the main post.

Ah. I will check it, Also, thanks for the code, I tried to check that when you posted, but I didn't finded an way. 

2 minutes ago, Kzisor said:

You're code is working in a very hackish kinda way.

Try the following code.


local _TemperatureUpdate = inst.components.temperature.OnUpdate
function inst.components.temperature:OnUpdate( dt, applyhealthdealta )

	-- Call the old function, making sure not to apply health damage.
	_TemperatureUpdate( self, dt, false )

	-- Do sanity damage here.
	if self.current < 0 then
		self.inst.components.sanity:DoDelta(5)
	elseif self.current > self.overheattemp then
		self.inst.components.sanity:DoDelta(5)
	end
end

This code gets placed in your master_postinit and will do it in a much nicer fashion.

ohh, thanks! I tried to go around the self and dodelta but I couldn't get to work in first place. but hey, learning XD.
 

On 2/17/2016 at 4:25 PM, Kzisor said:

You're code is working in a very hackish kinda way.

Try the following code.


local _TemperatureUpdate = inst.components.temperature.OnUpdate
function inst.components.temperature:OnUpdate( dt, applyhealthdealta )

	-- Call the old function, making sure not to apply health damage.
	_TemperatureUpdate( self, dt, false )

	-- Do sanity damage here.
	if self.current < 0 then
		self.inst.components.sanity:DoDelta(5)
	elseif self.current > self.overheattemp then
		self.inst.components.sanity:DoDelta(5)
	end
end

This code gets placed in your master_postinit and will do it in a much nicer fashion.

DoDelta(5) ... won't that add sanity instead?

So I copied this code but using DoDelta(-5) and then overheat myself... I ended up losing -5 sanity rapidly and then I have 0 sanity.

I was expecting to lose sanity much much slower than that.

I suppose I could doperiodictask instead?

2 hours ago, SenL said:

DoDelta(5) ... won't that add sanity instead?

So I copied this code but using DoDelta(-5) and then overheat myself... I ended up losing -5 sanity rapidly and then I have 0 sanity.

I was expecting to lose sanity much much slower than that.

I suppose I could doperiodictask instead?

A lot of the code I post is usually psuedo-code which requires equations to be changed in order to get your desired affect.

Thanks.

I ended up doing this, is it ok? (all below is in char prefab)

local function OnTemperatureTick(inst)
  if inst.components.temperature:IsOverheating() then
    inst.components.sanity:DoDelta(-3)
  end
end

local function onbecamehuman(inst)

  ...

  if inst.OnTempTick == nil then

    inst.OnTempTick = inst:DoPeriodicTask(3, function(inst) OnTemperatureTick(inst) end)

  end

end

local function onbecameghost(inst)

  ...

  if inst.OnTempTick ~= nil then

    inst.OnTempTick:Cancel()

    inst.OnTempTick = nil

  end

end

 

The code

if inst.OnTempTick == nil then

    inst.OnTempTick = inst:DoPeriodicTask(3, function(inst) OnTemperatureTick(inst) end)

  end

is also in onload() and common_postinit.

It seems to work fine. Anything wrong?

 

40 minutes ago, SenL said:

It seems to work fine. Anything wrong?

The more tasks which are running with other mods the more performance will degrade. The reason why I always suggest people using the method I use is because performance doesn't degrade when you overwrite function like that. It's already calling the function, whether or not it continues executing depends on a small if-statement. Whereas using tasks requires the code to run a completely separate function which might not run when you want it to run.

 

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