Sign in to follow this  
FreeSpirit

Hunger Value Modding on a Character

Recommended Posts

FreeSpirit    3

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!

Share this post


Link to post
Share on other sites
rezecib    3182

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

Share this post


Link to post
Share on other sites
FreeSpirit    3

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.

Share this post


Link to post
Share on other sites
RedMattis    39

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())

Share this post


Link to post
Share on other sites
FreeSpirit    3

 

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())

Share this post


Link to post
Share on other sites
FreeSpirit    3

Sorry for the doublepost (can't find an "edit post" button anywhere), but I wanted to ask if there's a place to find all the events (dusktime, nighttime, etc.). I want to make say a function for certain moon phases like a full moon.

Share this post


Link to post
Share on other sites
rezecib    3182

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

Share this post


Link to post
Share on other sites
FreeSpirit    3

 

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

Share this post


Link to post
Share on other sites
rezecib    3182
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)

Share this post


Link to post
Share on other sites
FreeSpirit    3

 Oh, oops. Add this:

local mult = 1if TheWorld.state.isday then mult = 1.5elseif TheWorld.state.isnight then mult = 0.5 endupdateHungerRate(inst, mult)

 

That works! Thanks very much!

Edited by FreeSpirit

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