Jump to content

What's the basic coding of stat gain/drain based on another stat level?


Recommended Posts

Health, hunger, and sanity each push a delta event when they are changed that you can listen for. So for example, if you want movement speed to scale with hunger percentage you can do something like:

local function UpdateSpeed(inst, data)
	local speedmult = data and data.newpercent or 1
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "hungry", speedmult)
end

inst:ListenForEvent("hungerdelta", UpdateSpeed)

The above code will make it so that the player's movement speed is always multiplied by their hunger percentage, so at 50% hunger they'd move half as fast, for example. Obviously that's a bit cruel and you can adjust the scaling however you wish, but the principle is the same: use event listeners to respond to changing stat levels.

Link to comment
Share on other sites

@Ziro2k Thanks for the lines!

Two questions:

1. This coding goes into the local master_postinit = function(inst) territory, right?

2. If I want to invert the speed/hunger ratio (alias make him more faster when he's hungry), I should just set the 

local speedmult = data and data.newpercent or 1

to

local speedmult = data and data.newpercent or -1

right?

Edited by BillTheCipher
Link to comment
Share on other sites

6 hours ago, BillTheCipher said:

1. This coding goes into the local master_postinit = function(inst) territory, right?

Yes, you'll want to put the event listener part in master_postinit. The UpdateSpeed local function can go anywhere in the character prefab above master_postinit.

6 hours ago, BillTheCipher said:

2. If I want to invert the speed/hunger ratio (alias make him more faster when he's hungry), I should just set the 


local speedmult = data and data.newpercent or 1

to


local speedmult = data and data.newpercent or -1

right?

Not exactly. The "1" in that particular line of code sort of just acts as a default, by taking advantage of how LUA works. This is incase the function gets called but no data is actually passed, very unlikely to ever happen but I guess it's just a habit of mine at this point to prevent edge-case crashes. It can also be read this way: "If data exists, then speedmult is equal to data.newpercent. If data does not exist, then speedmult is equal to 1." What you're looking for would be:

local speedmult = data and (1 - data.newpercent) or 1

This would make the character have full speed at 0% hunger and 0x speed at full hunger. If you'd like it to be only a bonus instead of a penalty then you'll want:

local speedmult = data and 1 + (1 - data.newpercent) or 1

This would make the character start at normal speed and gain speed as he loses hunger, up to 2x speed at 0% hunger. If you want a balance of penalty and bonus you could do this:

local speedmult = data and 1 + (0.5 - data.newpercent) or 1

This would start the character out at 0.5x speed at full hunger, and then increase this speed as he loses hunger to a maximum of 1.5x speed at 0% hunger.

I could go on and on with this, there are an infinite number of ways you could manipulate the equation to scale the way you want it to. But keep in mind, I was just providing an example of how to use an event listener to make changes to your character based on changing stat values. If you actually want to implement this there are a lot of additional factors that need to be considered. You'll probably want to scale it properly with an easing function (take a look at the graphs at https://easings.net/ to see the different scaling options) or at the very least easing.linear if you just want linear scaling. This would actually make things a bit easier, since you wouldn't have to come up with a specific equation to accomplish what you're looking for like I was doing above. The easing functions can be found in scripts\easing.lua in the game's files. You'll also want to clamp the value between a min and max that you specify using math.clamp, just to be safe (you don't want to end up with 0% speed or a negative value, for example). Finally, if you don't want these speed changes to affect the player while they are a ghost, you'll need to account for that with additional event listeners.

Edited by Ziro2k
  • Sanity 3
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...