Jump to content

Character Trait Help For a New Modder


Recommended Posts

I'm a first time modder and I'm creating two separate characters. These are the traits I'd like them to have
-Looses sanity during the day
-Sanity unaffected at dusk 
-Increases sanity in the dark/at night
-Increases sanity during thunder storms/rain (I still want the sanity to be effected based on wetness though)

 

Also one:

-Is a monster/befriends spiders

 

The other:

-Comes with a personal hat and umbrella

 

And help would be great!

Link to comment
Share on other sites

Sanity, in your character.lua:

local function sanityfn(inst)	if inst.sg:HasStateTag("nomorph") or		inst:HasTag("playerghost") or		inst.components.health:IsDead() then		return	end		if TheWorld.state.israining then		inst.components.sanity.dapperness = TUNING.DAPPERNESS_SMALL	elseif TheWorld.state.isday then		inst.components.sanity.dapperness = -TUNING.DAPPERNESS_TINY	elseif TheWorld.state.isdusk then		inst.components.sanity.dapperness = 0	elseif TheWorld.state.isnight then		inst.components.sanity.dapperness = TUNING.DAPPERNESS_TINY	endend

In your character.lua's master_postinit:

inst.components.sanity.custom_rate_fn = sanityfn

Monster, befriends spiders, in your character.lua's master_postinit:

inst:AddTag("monster")inst:AddTag("spiderwhisperer")

Comes with a personal hat and umbrella, in your character.lua:

local start_inv = {	"tophat"	"umbrella"}
Link to comment
Share on other sites

 

Sanity, in your character.lua:

local function sanityfn(inst)	if inst.sg:HasStateTag("nomorph") or		inst:HasTag("playerghost") or		inst.components.health:IsDead() then		return	end		if TheWorld.state.israining then		inst.components.sanity.dapperness = TUNING.DAPPERNESS_SMALL	elseif TheWorld.state.isday then		inst.components.sanity.dapperness = -TUNING.DAPPERNESS_TINY	elseif TheWorld.state.isdusk then		inst.components.sanity.dapperness = 0	elseif TheWorld.state.isnight then		inst.components.sanity.dapperness = TUNING.DAPPERNESS_TINY	endend

In your character.lua's master_postinit:

inst.components.sanity.custom_rate_fn = sanityfn

Monster, befriends spiders, in your character.lua's master_postinit:

inst:AddTag("monster")inst:AddTag("spiderwhisperer")

Comes with a personal hat and umbrella, in your character.lua:

local start_inv = {	"tophat"	"umbrella"}

 

Starting items need a comma at the  end.

 

example

 

local start_inv = {

"tophat",

"umbrella",

Link to comment
Share on other sites

local function sanityfn(inst)    if inst.sg:HasStateTag("nomorph") or        inst:HasTag("playerghost") or        inst.components.health:IsDead() then        return    end         if TheWorld.state.israining then        inst.components.sanity.dapperness = TUNING.DAPPERNESS_SMALL    elseif TheWorld.state.isday then        inst.components.sanity.dapperness = -TUNING.DAPPERNESS_TINY    elseif TheWorld.state.isdusk then        inst.components.sanity.dapperness = 0    elseif TheWorld.state.isnight then        inst.components.sanity.dapperness = TUNING.DAPPERNESS_TINY    endend 

 

This isn't the best way to do this, the custom_rate_fn in the sanity component is awaiting the function to return a value.  Willows sanity function returns a number depending on the number and size of fire near her.

 

This function is replacing the value of the characters dapperness, if another mod or feature was changing this it will create a conflict.

 

Here's a better way

local function sanityfn(inst)    if inst.sg:HasStateTag("nomorph") or        inst:HasTag("playerghost") or        inst.components.health:IsDead() then        return    end         if TheWorld.state.israining then        return TUNING.DAPPERNESS_SMALL * TUNING.SANITY_DAPPERNESS    elseif TheWorld.state.isday then        return -TUNING.DAPPERNESS_TINY * TUNING.SANITY_DAPPERNESS    elseif TheWorld.state.isdusk then        return 0    elseif TheWorld.state.isnight then        return TUNING.DAPPERNESS_TINY * TUNING.SANITY_DAPPERNESS    endend

Does the same thing that was intended, except now it works completely separate from the characters built in dapperness stat.

 

 

 

 

So I got the monster trait to work no problem. But my sanity still stays the same during the day and drains at night. I copied and pasted the text into my character lua. Any advice?

 

 

Did you remember to put the change in your master_postinit?

 

It looks like this in your character.lua at the bottom of it

local master_postinit = function(inst)inst.components.sanity.custom_rate_fn = sanityfn

The sanity function smugleaf and I gave only work if it gets set as your characters custom_rate_fn, so if you overlooked that part then it won't do anything.

 

His method works too, but I suggest using mine, it does the same thing but has better compatibility.

Edited by Zackreaver
Link to comment
Share on other sites

@ArkamInmate00451,
 

local MakePlayerCharacter = require "prefabs/player_common"local assets = {        Asset( "ANIM", "anim/player_basic.zip" ),        Asset( "ANIM", "anim/player_idles_shiver.zip" ),        Asset( "ANIM", "anim/player_actions.zip" ),        Asset( "ANIM", "anim/player_actions_axe.zip" ),        Asset( "ANIM", "anim/player_actions_pickaxe.zip" ),        Asset( "ANIM", "anim/player_actions_shovel.zip" ),        Asset( "ANIM", "anim/player_actions_blowdart.zip" ),        Asset( "ANIM", "anim/player_actions_eat.zip" ),        Asset( "ANIM", "anim/player_actions_item.zip" ),        Asset( "ANIM", "anim/player_actions_uniqueitem.zip" ),        Asset( "ANIM", "anim/player_actions_bugnet.zip" ),        Asset( "ANIM", "anim/player_actions_fishing.zip" ),        Asset( "ANIM", "anim/player_actions_boomerang.zip" ),        Asset( "ANIM", "anim/player_bush_hat.zip" ),        Asset( "ANIM", "anim/player_attacks.zip" ),        Asset( "ANIM", "anim/player_idles.zip" ),        Asset( "ANIM", "anim/player_rebirth.zip" ),        Asset( "ANIM", "anim/player_jump.zip" ),        Asset( "ANIM", "anim/player_amulet_resurrect.zip" ),        Asset( "ANIM", "anim/player_teleport.zip" ),        Asset( "ANIM", "anim/wilson_fx.zip" ),        Asset( "ANIM", "anim/player_one_man_band.zip" ),        Asset( "ANIM", "anim/shadow_hands.zip" ),        Asset( "SOUND", "sound/sfx.fsb" ),        Asset( "SOUND", "sound/wilson.fsb" ),        Asset( "ANIM", "anim/beard.zip" ),        Asset( "ANIM", "anim/patrick.zip" ),        Asset( "ANIM", "anim/ghost_patrick_build.zip" ),}local prefabs = {}-- Custom starting itemslocal start_inv = 	{		"tophat", 		"umbrella",	}-- When the character is revived from humanlocal function onbecamehuman(inst)	-- Set speed when loading or reviving from ghost (optional)	inst.components.locomotor.walkspeed = 4	inst.components.locomotor.runspeed = 6end-- When loading or spawning the characterlocal function onload(inst)    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)    if not inst:HasTag("playerghost") then        onbecamehuman(inst)    endend--Sanity Test	local function sanityfn(inst)    if inst.sg:HasStateTag("nomorph") or        inst:HasTag("playerghost") or        inst.components.health:IsDead() then        return    end          if TheWorld.state.israining then        return TUNING.DAPPERNESS_SMALL * TUNING.SANITY_DAPPERNESS    elseif TheWorld.state.isday then        return -TUNING.DAPPERNESS_TINY * TUNING.SANITY_DAPPERNESS    elseif TheWorld.state.isdusk then        return 0    elseif TheWorld.state.isnight then        return TUNING.DAPPERNESS_TINY * TUNING.SANITY_DAPPERNESS    endend    -- This initializes for both the server and client. Tags can be added here.local common_postinit = function(inst) 	-- Minimap icon	inst.MiniMapEntity:SetIcon( "patrick.tex" )end    -- This initializes for the server only. Components are added here.local master_postinit = function(inst)	-- choose which sounds this character will play	inst.soundsname = "woodie"		-- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used    --inst.talker_path_override = "dontstarve_DLC001/characters/"		-- Stats		inst.components.health:SetMaxHealth(150)	inst.components.hunger:SetMax(150)	inst.components.sanity:SetMax(200)		-- Damage multiplier (optional)    inst.components.combat.damagemultiplier = 1		-- Hunger rate (optional)	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE	--This is what calls the custom_rate function above	inst.components.sanity.custom_rate_fn = sanityfn		inst.OnLoad = onload    inst.OnNewSpawn = onload		--Monster Trait--	inst:AddTag("monster")	inst:AddTag("spiderwhisperer")	endreturn MakePlayerCharacter("patrick", prefabs, assets, common_postinit, master_postinit, start_inv) 

No crash, tried it myself.

It is also good practice to do indentation so it is easier to read. :3
Things to consider too, your sanity drain is so low even during daylight while 100% soaked.
Might want to tweak it some.

Edited by Valkyrie19k
Link to comment
Share on other sites

Ok update: everything works fine until the character becomes a ghost....then the game crashes. Any ideas?

Please post your crash logs. If you're on a windows computer it will be located (by default) at C:\Users\PC\Documents\Klei\DoNotStarveTogether\log.txt

 

This will tell you the exact line of code that is causing an issue usually.

Link to comment
Share on other sites

@Noggle,

 

 

[string "scripts/components/sanity.lua"]:304: attempt to perform arithmetic on a nil value

LUA ERROR stack traceback:
scripts/components/sanity.lua:304 in (method) Recalc (Lua) <262-319>
   self =
      _oldissane = true
      dapperness_mult = 1
      dapperness = 0
      rate = -0.21998019159109
      rate_modifier = 1
      _oldpercent = 0.97702524758045
      night_drain_mult = 1
      inst = 109368 - jet (valid:true)
      fxtime = 0
      neg_aura_mult = 1
      sanity_penalties = table: 0EA78160
      _ = table: 0EA78070
      custom_rate_fn = function - ../mods/jetro/scripts/prefabs/jet.lua:66
   dt = 0.033333335071802
   total_dapperness = 0
   dapper_delta = 0
   moisture_delta = 0
   light_delta = 0
   aura_delta = -0.21998019159109
   x = -348.41632080078
   y = 0
   z = 50.545486450195
   ents = table: 0E2AD358
   ghost_delta = -0
scripts/components/sanity.lua:236 in (method) OnUpdate (Lua) <231-245>
   self =
      _oldissane = true
      dapperness_mult = 1
      dapperness = 0
      rate = -0.21998019159109
      rate_modifier = 1
      _oldpercent = 0.97702524758045
      night_drain_mult = 1
      inst = 109368 - jet (valid:true)
      fxtime = 0
      neg_aura_mult = 1
      sanity_penalties = table: 0EA78160
      _ = table: 0EA78070
      custom_rate_fn = function - ../mods/jetro/scripts/prefabs/jet.lua:66
   dt = 0.033333335071802
scripts/update.lua:183 in () ? (Lua) <146-219>
   dt = 0.033333335071802
   tick = 771
   k = 109368
   v = 109368 - jet (valid:true)
   cmp = table: 0EA78250
 
[00:01:27]: [string "scripts/components/sanity.lua"]:304: attempt to perform arithmetic on a nil value
LUA ERROR stack traceback:
    scripts/components/sanity.lua:304 in (method) Recalc (Lua) <262-319>
    scripts/components/sanity.lua:236 in (method) OnUpdate (Lua) <231-245>
    scripts/update.lua:183 in () ? (Lua) <146-219>

 

Link to comment
Share on other sites

Managed to fix the ghost crashing error! Aaaand now the sanity drains constantly....during day, dusk, and night.....

 

I'm sure everyone is annoyed with me at this point. I honestly have no idea what I'm doing and am thankful for everyone's help so far!

 

Here's my code, attached. I'd still like to increase the drain at day, and stop the drain at dusk and night.

patrick.lua

Edited by ArkamInmate00451
Link to comment
Share on other sites

I'm sure everyone is annoyed with me at this point.

Your wrong, were not annoyed at all.  We're glad to help 

 

Edit: I don't see anything wrong with the code on the outlook, I'm going to do a field test and see what might be the problem.

 

Edit2: Ah I see the problem, our code doesn't STOP the drain at night, it add's sanity recover during night.

 

However the sanity recover we gave your character isn't higher than the amount he loses.

 

Our code was working, it just wasn't strong enough, so we'll just raise the numbers.

 

TUNING.DAPPERNESS_TINY = 0.0222222222

 

Thats the number that tuning.dapperness_tiny gives

 

you could just make it say

return 0.02

If you raise that number, it will increase the amount of sanity that you gain.

 

The exact amount of sanity that you lose at night normally is

 

-0.083

when your in light at nighttime

-0.83

when your in pitch black at nighttime.

 

So if you have the nighttime value set as

return 0.83

You'll never lose sanity at night even when your at pitch black.  And you'll gain it if your near a light source

 

But if you instead want to not lose any sanity from night time at all, but you don't want to gain any either, you can do this

local lightval = CanEntitySeeInDark(inst) and .9 or inst.LightWatcher:GetLightValue()        return -1 *               ((lightval > TUNING.SANITY_HIGH_LIGHT and TUNING.SANITY_NIGHT_LIGHT) or                (lightval < TUNING.SANITY_LOW_LIGHT and TUNING.SANITY_NIGHT_DARK) or                TUNING.SANITY_NIGHT_MID) * inst.components.sanity.night_drain_mult

After adjusted it should look something like this

local function sanityfn(inst)    if inst.sg:HasStateTag("nomorph") or        inst:HasTag("playerghost") or        inst.components.health:IsDead() then        return    end    if TheWorld.state.israining then        return TUNING.DAPPERNESS_SMALL * TUNING.SANITY_DAPPERNESS    elseif TheWorld.state.isday then        return -TUNING.DAPPERNESS_TINY * TUNING.SANITY_DAPPERNESS    else        local lightval = CanEntitySeeInDark(inst) and .9 or inst.LightWatcher:GetLightValue()        return -1 *           ((lightval > TUNING.SANITY_HIGH_LIGHT and TUNING.SANITY_NIGHT_LIGHT) or           (lightval < TUNING.SANITY_LOW_LIGHT and TUNING.SANITY_NIGHT_DARK) or           TUNING.SANITY_NIGHT_MID) * inst.components.sanity.night_drain_mult    endend
Edited by Zackreaver
Link to comment
Share on other sites

@Valkyrie19k,  Moosegoose's face feels like how I feel most of the time, very derpy.

 

Also, rezecib rock's the angry dragonfly portrait, and Darkxero was made a picture of glommer pooping code (perfect representation), so I felt like trying to fit in.

 

Also, I actually am a Moose, after being a bearger, all accidental.

http://forums.kleientertainment.com/topic/52761-help-i-accidently-became-a-bearger/?hl=%2Baccidently+%2Bbearger

 

I should be more careful.

 

 

 

Hey, that works great! Thanks! I tested it in game, nothing crashes even after death. You're fantastic.

 

Glad to hear it, feel free to ask for help anytime.

Edited by Zackreaver
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...