Jump to content

Sanity Drain on overheating/starvation


MF99K

Recommended Posts

(I feel like I posted something like this on the DST mod forums but just in case)

 

I've been working on this character mod, and one of this character's perks is that she "hates being uncomfortable." This perk is supposed to indicate a sanity drain whenever she is starving or overheating (another perk is she doesn't mind being cold, so no modifier there). I initially tried to copy and modify Willow's chill sanity code into this character's prefab, but the code was a bit confusing to me as the tuning values were the only part of the code that mentioned anything about freezing. And, since I wanted a sanity drain for overheating and not freezing, I wasn't sure how to change it.

how would I format this code? I've already changed the temperature components that make her weak to heat and resistant to the cold, but do I need a Inst:ListenForEvent("overheating", etc) or something more similar to Willow's code, or something else?

Link to comment
Share on other sites

anyone?

Edit: The code I have currently looks like this (for hunger, there's a temperature one too that's basically the same coding with different component values:

local function onhungerchange(inst)

            if inst:HasTag("playerghost") or

                  inst.components.health:IsDead() then

            return

           if inst.components.hunger.current > 5 then

                inst.components.sanity:DoDelta(0)

           elseif inst.components.hunger.current < 5 then

                inst.components.sanity:DoDelta(-3)

          end

end

 

I keep getting an error that the game is expecting a table instead of functions ("bad argument").

 

main questions: 

  • does "elseif" need to be separated into "else if"?
  • do both inst's related to hunger need to have elseif/else if in front of them?
  • Is there something else I'm doing wrong?
Link to comment
Share on other sites

For starters, the following part does nothing:

if inst.components.hunger.current > 5 then

     inst.components.sanity:DoDelta(0)

Lua can understand "elseif", but if you remove the previous part, you change it to "if". By the way, "else" on its own would have worked in this case, allowing you to skip the second check.

Corrected:

if inst:HasTag("playerghost") or inst.components.health:IsDead() then

      return

elseif inst.components.hunger.current < 5 then

      inst.components.sanity:DoDelta(-3)

end

Where and how do you use this function? Make sure "inst" is the player and not something else!

Link to comment
Share on other sites

3 hours ago, Mobbstar said:

For starters, the following part does nothing:

if inst.components.hunger.current > 5 then

     inst.components.sanity:DoDelta(0)

Lua can understand "elseif", but if you remove the previous part, you change it to "if". By the way, "else" on its own would have worked in this case, allowing you to skip the second check.

Corrected:

if inst:HasTag("playerghost") or inst.components.health:IsDead() then

      return

elseif inst.components.hunger.current < 5 then

      inst.components.sanity:DoDelta(-3)

end

Where and how do you use this function? Make sure "inst" is the player and not something else!

thanks for answering:)

It's right underneath the common_postinit function (not part of it, just below it)

The code only refers to inst in relation to the character's stats, so it shouldn't be referencing anything besides the player character.

Assuming I use this code correctly, it should result in a sanity drain whenever the character is overheating/starving, right?

Link to comment
Share on other sites

2 minutes ago, mf99k said:

thanks for answering:)

It's right underneath the common_postinit function (not part of it, just below it)

The code only refers to inst in relation to the character's stats, so it shouldn't be referencing anything besides the player character.

Assuming I use this code correctly, it should result in a sanity drain whenever the character is overheating/starving, right?

Did you actually mention "onhungerchange" anywhere afterwards? Because if not, it's probably not hooked up properly. That said, I am not very familiar with custom characters in DST. This is the DS forum after all.

Link to comment
Share on other sites

Just now, Mobbstar said:

Did you actually mention "onhungerchange" anywhere afterwards? Because if not, it's probably not hooked up properly. That said, I am not very familiar with custom characters in DST. This is the DS forum after all.

This part of the code shouldn't be different between DS and DST. I mentioned the function down at the bottom where all the other functions are mentioned.

Link to comment
Share on other sites

@Mobbstar Alright, the code still isn't working, I'm getting the same error as before:

 

56ba343302f42_ipairserror.png.f55256c4bd

I looked at the section of player_common that mentioned the error, and it says something about the starting inventory, but the mod was working before I added the sanity drain code and that was the only part I changed.

 

do you want me to send you a .zip of my modmain and script files?

Link to comment
Share on other sites

@mf99k You're not meant to pass "onXchange" kind of functions to the constructor. Who made you think so? Wolfgang doesn't either, he does it in the postinit function:

inst:ListenForEvent("hungerdelta", onhungerchange)

Change the last line to:

return MakePlayerCharacter("warper", prefabs, assets, common_postinit, master_postinit, start_inv)

And refer to Wolfgang first if you have more questions.

Link to comment
Share on other sites

46 minutes ago, Mobbstar said:

@mf99k You're not meant to pass "onXchange" kind of functions to the constructor. Who made you think so? Wolfgang doesn't either, he does it in the postinit function:

inst:ListenForEvent("hungerdelta", onhungerchange)

Change the last line to:

return MakePlayerCharacter("warper", prefabs, assets, common_postinit, master_postinit, start_inv)

And refer to Wolfgang first if you have more questions.

I had used Wolfgang's code as reference so I obviously did something wrong. 

inst:ListenForEvent("hungerdelta", onhungerchange) only shows up in Wolfgang's onbecamehuman and onbecameghost functions. At this point I'm assuming this is a difference between DST and DS, but other than that the codes are the same. If I delete the onXchange from the last line of code and add the event listener should it work?

 

 

Edit: I put inst:ListenForEvent("sanitydelta", onhungerchange, ontemperaturechange) in the postinit. The game doesn't like how this is written. Would I do a separate line for onhungerchange and ontemperaturechange?

Edit: nevermind that gives me stack overflow

Link to comment
Share on other sites

16 minutes ago, mf99k said:

how do I use this?

If you use this code:

 

print("Hello world")

local x = 5

print("x is",x)

print("There's online documentation " .. " on base Lua functions, you know?")

 

The log.txt says:

 

Hello World

x is    5

There's online documentation  on base Lua functions, you know?

Link to comment
Share on other sites

6 hours ago, Mobbstar said:

If you use this code:

 

print("Hello world")

local x = 5

print("x is",x)

print("There's online documentation " .. " on base Lua functions, you know?")

 

The log.txt says:

 

Hello World

x is    5

There's online documentation  on base Lua functions, you know?

so on console?

Link to comment
Share on other sites

14 hours ago, mf99k said:

so on console?

You can use it anywhere in your code. When you're not sure whether or not a function gets triggered, use print() in that function to check. e.g.:

if false then

  print("You can usually use CTRL+F to look for specific phrases in a document. Use this to quickly find these words!")

  print("P.S.: this code never gets executed, so you won't find it in the log.txt")

end

The game also uses print(), but more commonly "Print()", which is a custom function that checks for the debug verbosity (i.e. less important messages are tagged as such and only get print()'d when the game is in debug mode).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...