Jump to content

Hunger Value Modding on a Character


Recommended Posts

I'm trying to port over a character (for just me and a friend) originally made for Don't Starve that was also compatible with Reign of Giants over to DST, but I managed to get everything working except for one particular thing.

 

In the modmain.lua, I noticed this particular section of code was meant to give the character double the hunger value for meats:

 

GetPlayer = GLOBAL.GetPlayerAddComponentPostInit("edible", function(Edible, inst)local oldGetHunger = Edible.GetHungerfunction Edible:GetHunger(eater)    local multiplier = 1    local character = GetPlayer()    if character:HasTag("lovesMeat") then        if self.foodtype == "VEGGIE" or self.foodtype == "SEEDS" then            multiplier = 1        elseif self.foodtype == "MEAT" then            multiplier = 2        end    end    return (multiplier * oldGetHunger(self, eater))endend)

 

I want to get that over to the script/prefabs lua file. But I don't know how to exactly. The character runs perfectly fine and works with that left in the modmain file but it conflicts with the Display Food Values mod and ends up crashing the game.

 

Any help would be appreciated!

Link to comment
Share on other sites

Change it to this:

AddComponentPostInit("edible", function(Edible, inst)local oldGetHunger = Edible.GetHungerfunction Edible:GetHunger(eater)    local multiplier = 1    if eater and eater:HasTag("lovesMeat") then        if self.foodtype == "VEGGIE" or self.foodtype == "SEEDS" then            multiplier = 1        elseif self.foodtype == "MEAT" then            multiplier = 2        end    end    return (multiplier * oldGetHunger(self, eater))endend)

The old way it was written didn't take into account that there are multiple players in a game of DST, so it can't just use "GetPlayer()". Not sure why it would be conflicting with DFV, but DFV doesn't work at all for clients anyway, so...

Link to comment
Share on other sites

Thanks a lot! It no longer crashes the game when running with DFV.

 

I do have another question regarding another line of code but I don't want to make another topic/thread. So I'll post it here.

 

I have a function to give a player a slower hunger drain during the night, but a faster hunter drain during the day (both are separate), but I do not know where to place this line of code in the prefabs file to have it activate/deactivate:

 

[codesyntax]inst:ListenForEvent( "nighttime", function() [code GOES HERE] end, GetWorld() )[/codesyntax]

 

 

I found that snipit for someone trying to add Night Vision to their character and another member posted that to help with it activating during the night and then deactivating it during the day. But I would like it to be used for the function I want to give it above as I mentioned.

Link to comment
Share on other sites

Try something like this:

local function updateHungerRate(inst)   if TheWorld.state.isday then      inst.components.hunger:SetRate(1.5*TUNING.WILSON_HUNGER_RATE)   end   elseif TheWorld.state.isdusk then      inst.components.hunger:SetRate(1*TUNING.WILSON_HUNGER_RATE)   end   elseif TheWorld.state.isnight then      inst.components.hunger:SetRate(0.5*TUNING.WILSON_HUNGER_RATE)   endendinst:ListenForEvent( "dusktime", function() updateHungerRate(inst) end , GetWorld())inst:ListenForEvent( "daytime", function() updateHungerRate(inst) end , GetWorld())inst:ListenForEvent( "nighttime", function() updateHungerRate(inst) end , GetWorld())
Link to comment
Share on other sites

 

Try something like this:

local function updateHungerRate(inst)   if TheWorld.state.isday then      inst.components.hunger:SetRate(1.5*TUNING.WILSON_HUNGER_RATE)   end   elseif TheWorld.state.isdusk then      inst.components.hunger:SetRate(1*TUNING.WILSON_HUNGER_RATE)   end   elseif TheWorld.state.isnight then      inst.components.hunger:SetRate(0.5*TUNING.WILSON_HUNGER_RATE)   endendinst:ListenForEvent( "dusktime", function() updateHungerRate(inst) end , GetWorld())inst:ListenForEvent( "daytime", function() updateHungerRate(inst) end , GetWorld())inst:ListenForEvent( "nighttime", function() updateHungerRate(inst) end , GetWorld())

 

It didn't work when I first pasted that in, but easily fixed it by removing the excess "end" lines:

local function updateHungerRate(inst)	   if TheWorld.state.isday then	  inst.components.hunger:SetRate(1.5*TUNING.WILSON_HUNGER_RATE)   elseif TheWorld.state.isdusk then	  inst.components.hunger:SetRate(1*TUNING.WILSON_HUNGER_RATE)   elseif TheWorld.state.isnight then	  inst.components.hunger:SetRate(0.5*TUNING.WILSON_HUNGER_RATE)   endendinst:ListenForEvent( "dusktime", function() updateHungerRate(inst) end , GetWorld())inst:ListenForEvent( "daytime", function() updateHungerRate(inst) end , GetWorld())inst:ListenForEvent( "nighttime", function() updateHungerRate(inst) end , GetWorld())
Link to comment
Share on other sites

Should be this, I think:

local function updateHungerRate(inst, mult)       inst.components.hunger:SetRate(mult*TUNING.WILSON_HUNGER_RATE)end inst:WatchWorldState( "startday", function() updateHungerRate(inst, 1.5) end )inst:WatchWorldState( "startdusk", function() updateHungerRate(inst, 1) end )inst:WatchWorldState( "startnight", function() updateHungerRate(inst, 0.5) end )
Edited by rezecib
Link to comment
Share on other sites

 

Should be this, I think:

local function updateHungerRate(inst, mult)       inst.components.hunger:SetRate(mult*TUNING.WILSON_HUNGER_RATE)end inst:WatchWorldState( "startday", function() updateHungerRate(inst, 1.5) end )inst:WatchWorldState( "startdusk", function() updateHungerRate(inst, 1) end )inst:WatchWorldState( "startnight", function() updateHungerRate(inst, 0.5) end )

 

 

It works, but when you enter the world (joining or hosting), it does not come into effect until the next phase of time. So if I made a brand new world which starts you during the day, it does not go into effect until dusk.

 

I tried changing the "startday" to "isday" but it didn't still didn't go into effect immediately.

Edited by FreeSpirit
Link to comment
Share on other sites

It works, but when you enter the world (joining or hosting), it does not come into effect until the next phase of time. So if I made a brand new world which starts you during the day, it does not go into effect until dusk.
 Oh, oops. Add this:
local mult = 1if TheWorld.state.isday then mult = 1.5elseif TheWorld.state.isnight then mult = 0.5 endupdateHungerRate(inst, mult)
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...