Jump to content

Sanity Arua based off of Sanity


Recommended Posts

I want to make a character have a sanity arua that weakens as they lose sanity, becoming an insanity arua when they lose a certain amount, becoming stronger as they lose more sanity. It doesn't seem to work, here's what I did.

local function sanitystuff (inst)
	if inst.components.sanity:GetPercent() == 1 then
		inst.components.sanityaura.aura = TUNING.SANITYAURA_LARGE * 2
	elseif inst.components.sanity:GetPercent() < 1 and inst.components.sanity:GetPercent() > .83 then
		inst.components.sanityaura.aura = TUNING.SANITYAURA_LARGE
	elseif inst.components.sanity:GetPercent() < .83 and inst.components.sanity:GetPercent() > .66 then
		inst.components.sanityaura.aura = TUNING.SANITYAURA_MED
	elseif inst.components.sanity:GetPercent() < .66 and inst.components.sanity:GetPercent() > .5 then
		inst.components.sanityaura.aura = TUNING.SANITYAURA_SMALL
	elseif inst.components.sanity:GetPercent() < .5 and inst.components.sanity:GetPercent() > .33 then
		inst.components.sanityaura.aura = -TUNING.SANITYAURA_SMALL
	elseif inst.components.sanity:GetPercent() < .33 and inst.components.sanity:GetPercent() > .16 then
		inst.components.sanityaura.aura = -TUNING.SANITYAURA_MED
	elseif inst.components.sanity:GetPercent() < .16 and inst.components.sanity:GetPercent() > 0 then
		inst.components.sanityaura.aura = -TUNING.SANITYAURA_MED
	elseif inst.components.sanity:GetPercent() == 0 then
		inst.components.sanityaura.aura = -TUNING.SANITYAURA_LARGE
	end
end

 

Link to comment
Share on other sites

17 minutes ago, Ultroman said:

Where do you call this function?

I think I get it now, I'm supposed to have something watch the sanity meter every .1 second and then call sanitystuff that's under sanitystuff, right? I thought this was an instance where I don't need to do something like that.

It's something like

inst:DoPeriodicTask(.2, function(inst)
	sanitystuff(inst)
	end)

right?

Link to comment
Share on other sites

Anything you want the game to do has to be triggered by something. Since you're using the sanityaura component, it actually does this for you. If you notice in its code, it not only allows you to set the "aura" variable, which is the constant rate. It also lets you give it a function to tell it how to determine what the current rate should be. The sanityaura component is "baked into" the sanity component's calculations, so any entity with a sanity component on that is within the default (and unchangeable, unless you want to mess all other sanity auras up) sanity aura range, will be affected by your sanityaura component. You code is almost complete. Just a few changes.

local function sanityaurafn (inst, observer)
	-- Create a temporary variable with the current value, so we don't call GetPercent() a million times.
	local currentSanity = inst.components.sanity:GetPercent()
	
	if currentSanity == 1 then
		return TUNING.SANITYAURA_LARGE * 2
	-- Since you're only moving downwards in numbers, and you only EVER go into ONE of these
	-- if-statements, you don't need to check for it being lower than the previous value.
	elseif currentSanity > .83 then
		return TUNING.SANITYAURA_LARGE
	elseif currentSanity > .66 then
		return TUNING.SANITYAURA_MED
	elseif currentSanity > .5 then
		return TUNING.SANITYAURA_SMALL
	elseif currentSanity > .33 then
		return -TUNING.SANITYAURA_SMALL
	elseif currentSanity > .16 then
		return -TUNING.SANITYAURA_MED
	elseif currentSanity > 0 then
		return -TUNING.SANITYAURA_MED
	-- No need for an extra check for 0. If it is above 0 it will enter the
	-- statement above, and if it is 0 or lower, it will enter this last one.
	else
		return -TUNING.SANITYAURA_LARGE
	end
end

-- Then we apply the aura function to the sanityaura component.
inst.components.sanityaura.aurafn = sanityaurafn

 

Edited by Ultroman
Link to comment
Share on other sites

What I do is: I have extracted all the game scripts into a folder. When I think "Hmm, I have no idea if this is in the game already.", then I use Notepad++ to search in all the files using the "Find in files..." feature (CTRL-SHIFT-F). I search for different words they could have used to describe or name things to do with my query. In this case, I would probably search for "sanity" or "aura" first, and if I didn't find anything, I'd try to find other words for it.

In this case, it would quickly lead you to the sanityaura component. So, how does that work? Well, looking at the code, we can see that if it doesn't have an "aurafn" function, then it uses the "aura" variable as its value. So...how does the "aurafn" work? Well, we can see that there are no functions which set the "aurafn" variable in the sanityaura component itself, so it must be set manually by some prefabs that use it. Then I'd do "Find in files..." searching for ".aurafn", and I'd find dozens of prefabs which set this "aurafn" function, e.g. spiders. Then you can see how the game sets up the function. Keep in mind, though, that this is Lua, so you often run into function declarations which omit the parameters they do not use, so ALWAYS look where the function is actually called, to see what data is passed into the function. In this case, it sends the inst that has the aura on as well as the observer (which is the entity affected by your aura, for which the sanity rate is being queried by the sanity component calculations). Again, keep in mind that not all function calls include all the parameters in all places, so always check all function calls made by the game, to see if some of them include more parameters you can make use of.

My time modding is about 30% searching in the game files, 20% searching on the forum ir looking for other mods doing the same thing I want to do, 30% thinking about a good structure for my code, and 20% actually writing the code.

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