Jump to content

First mod, need help with creating a perk


Recommended Posts

Hello, I'm making a mod for the first time and had a few questions I could use some help with. I'm only using the basic "extended sample character" file that was recommended to me, Notepad++, and my very basic understanding of coding. What I would like help with is making my character's attack increase by 0.1 or something like that every time their sanity drops 5 points. Now, being new to coding (or modding in general), I don't know how to set it up / which lua file it would go in, where to place the code, like if it had to go under another specific bit of code or not. So if you're willing to help walk through how to code something like this / where to implement it, I'd be very appreciative of your help!

Link to comment
Share on other sites

So normally I'd try to suggest a couple of mods you might wanna look at to get a feel for this sort of thing, but this kind of a perk honestly isn't incredibly complicated, so I'll try to point you in the right direction.

I'm assuming that the desired effect is to make your damage output increase as your sanity decreases, so here's some code I mocked up that should produce that effect. You'll just have to adjust a few numbers if you're not pleased with the results.

local function OnSanityDelta(inst)
	if inst.components and inst.components.sanity and inst.components.combat then
		local SanityPercent = inst.components.sanity.current / inst.components.sanity.max
		inst.components.combat.damagemultiplier = 2 - SanityPercent
	end
end

local master_postinit = function(inst)
	inst:ListenForEvent("sanitydelta", OnSanityDelta)
end

It's possible that this isn't the best way to write this, but it's serviceable. You'll want to put all of this code directly into your character.lua file inside of your prefabs folder. I think by default it'll be called esctemplate.lua. Also you may already have a function labeled "master_postinit". If you do, just put the "inst:ListenForEvent("sanitydelta", OnSanityDelta)" part of the code inside the existing function at the bottom.

In case you're interested in the actual specifics of the code, all it's doing is listening for whenever your sanity level changes, and in response it makes your character's damage multiplier equal to 2 minus (your current sanity divided by your maximum sanity). The default damage multiplier is 1. This means that if you have zero sanity, your damage output will be doubled. Conversely, if you have full sanity, your damage multiplier will be set to 1.

Let me know if you need additional clarification.

Edited by DarkMatterZero
Clarification on where to put the code
Link to comment
Share on other sites

This is exactly what I needed, thanks for your help! Though, is there a way to keep the default modifier at say 2, and then edit the multiplier so it doesn't override that? Basically I just mean that when in-game I spawned in the dummy to test different damages, and saw that whenever I'd edit the multiplier, it would actually change my base damage instead, and then only throw up to 10 on to that for the maximum amount of damage the character does. Is there a way to make the multiplier deal more than +10 without it overriding the base damage?

Link to comment
Share on other sites

Ah, okay. I think I understand what you're asking.

local function OnSanityDelta(inst)
	if inst.components and inst.components.sanity and inst.components.combat then
		local BaseDamageMult = 1
		local InsanityPercent = 1 - (inst.components.sanity.current / inst.components.sanity.max)
		local BonusDamageMult = 4
		inst.components.combat.damagemultiplier = BaseDamageMult + (InsanityPercent * BonusDamageMult)
	end
end

Use this code instead. I made some proper variables so it's more customizable and easier to understand. If you want to change the amount of damage you're dealing by default (before the insanity bonus is applied), you can adjust the "BaseDamageMult" variable. If you want to change the amount of insanity bonus damage you're dealing, you'll want to adjust the "BonusDamageMult" variable. The InsanityPercent variable shouldn't need any editing.

For clarification, I'll give an example. Let's say you have 50 sanity remaining out of 200 total.
Your InsanityPercent would be 1 - (50 / 200) = 0.75

Your damage multiplier would be 1 + (0.75 * 4) = 4
With the current numbers plugged in, you could have a maximum damage multiplier of 5 if you had 0 sanity.

I hope this is what you were asking for and that it helps. Cheers!

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