Jump to content

Calling Temperature component from custom component


Recommended Posts

I am trying to port my Thrist mod into DST and I am facing troubles doing it. The thing is - thirst rate shoud depend on temperature of player and also player should start to sweat, if he is overheating.

But if I try to call temperature component from thirst component - I get a crash saying "U're trying to access nil temperature blah blah blah".

Here is the code from modmain:

Quote

modmain.lua

AddReplicableComponent("thirst")

..............................

local function GiveThirst(inst)
    if inst.components.thirst == nil then
        inst:AddComponent("thirst")
        inst:ListenForEvent("death", Deaththirst)
        inst:ListenForEvent("respawnfromghost", Respawnthirst)
        
        inst.maxthirst = GLOBAL.net_ushortint( inst.GUID, "thirstmax", "currentdirty")
        inst.currentthirst = GLOBAL.net_ushortint( inst.GUID, "thirstcurrent", "currentdirty")
        inst.penaltythirst = GLOBAL.net_float( inst.GUID, "thirstpenalty", "currentdirty")
        
        if not TheWorld.ismastersim then
            inst:ListenForEvent("currentdirty", function(inst) OnthirstDirty(inst) end)
        end

        inst:DoPeriodicTask(1, function(inst) 
            inst.components.thirst:DoDelta(0)--Badge Updates
        end)        
    end
    
end
-- RUNS THE ABOVE FUNCTION FOR ALL CHARACTERS, INCLUDING MODDED ONES --
AddPlayerPostInit(GiveThirst)

 

Here is the code from thrist.lua :

Quote

local thirst = Class(function(self, inst)
    self.inst = inst
    self.max = 200
    self.min = 0
    self.current = self.max
    
    self.thirstrate = 0.2*TUNING.THIRST_DIFFILCULTY_THIRST_SPEED
    self.burnrate = 1*TUNING.THIRST_DIFFILCULTY_THIRST_SPEED

    self.burning = true
    
    self.timetosay = 45
    
    self.period = 1
    
    self.task = self.inst:DoPeriodicTask(self.period, function() self:DoDec(self.period) end)

end,
nil,
{})


function thirst:GetDelta()
    if self.inst.components.thirst then
      local temp = self.inst.components.temperature:GetCurrent()   ---------- Here I get a crash, it says "temperature is nil"
      local tf = 0
      if temp > 55 then
          tf = temp/150
      end
      local delta = self.inst.components.thirst.thirstrate+tf
      return delta
    else
      return 1
    end
end

function thirst:DoDec(dt, ignore_damage)
     local old = self.current

     if self.burning then
        self:DoDelta(self.burnrate*(-self:GetDelta()*dt), true)
        if self.inst.components.temperature.current/100 > 0.55 then   ---------- Here I get a crash, it says "temperature is nil"
            if self.inst.components.moisture then    ---------- Here I get a crash, it says "moisture is nil"
                self.inst.components.moisture:DoDelta(0.3)
            end
                
        end
     end
end

 

 

What am I doing wrong? How do I fix this?

Thanks in advance.

Edited by Desblat
Link to comment
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
 Share

×
  • Create New...