Sign in to follow this  
Cthonicus

Sanity Aura Issue

Recommended Posts

Cthonicus    13

Hey everyone!

 

I came here awhile ago with to find some help on writing a line of code to create a sanity aura for my character, and while it does work, I've discovered over time an issue.

 

-- MY CUSTOM CODE!
-- This value is the highest amount of sanity that a single entity can give per second.
local maxSanityRateBonusPerEntity = TUNING.SANITYAURA_SMALL
-- This function is hooked up to the sanity component, to add a custom rate-based
-- change to sanity for this particular character.
local function sanityfn(inst)
	local delta = 0
	local x, y, z = inst.Transform:GetWorldPosition()
	local max_rad = 10
	local ents = TheSim:FindEntities(x, y, z, max_rad, nil, nil, {
	"bee", "flower", "butterfly", "rabbit", "koalefant_summer",
	"koalefant_winter", "chester", "squid", "glommer", "hutch", "mole"
})
	
	for i, v in ipairs(ents) do
		-- Determine whether the entity is dead, which they can only be if they have a health-component.
		-- Otherwise, we default to them not being seen as dead.
		local isDead = v.components.health ~= nil and v.components.health:IsDead() or false
		local distsq = inst:GetDistanceSqToInst(v) - 9
		-- Add the maximum sanity rate divided by distance, so you get more sanity the closer
		-- you are to the entities, but cap it so distance cannot be lower than 1,
		-- because dividing by e.g. 0.2 would multiply the value by 5 :)
		-- And at the end there, if the entity is dead, it makes the character sad instead.
		delta = delta + maxSanityRateBonusPerEntity / math.max(1, distsq) * (isDead and -1 or 1)
	end
	return delta
end

This code allows the player to gain sanity when near those listed entities (which works fine), except I later noticed that if the player is holding one of those entities in their inventory, such as a Moleworm or a butterfly, the sanity aura continues. This is pretty OP since the player can just feed the Moleworm rocks for example and have infinite sanity gain. So really I just need to make it that if the player is holding an entity listed there, the sanity aura does NOT count.

 

Could anyone please inform me if this is possible to fix? All help is greatly appreciated. I've attached the prefab file there just in case.

 

Thanks!

 

wolyo.lua

Edited by Cthonicus

Share this post


Link to post
Share on other sites
-t-    222

Animals that can be picked up and held in the inventory have the 'inventoryitem' component. This component has a function called 'IsHeld' which can be used to determine whether the animal is being held currently. So you can add this check to your function:

local maxSanityRateBonusPerEntity = TUNING.SANITYAURA_SMALL

local function sanityfn(inst)
	local delta = 0
	local x, y, z = inst.Transform:GetWorldPosition()
	local max_rad = 10
	local oneoftags = { "bee", "flower", "butterfly", "rabbit", "koalefant_summer", "koalefant_winter", "chester", "squid", "glommer", "hutch", "mole" }
	local ents = TheSim:FindEntities(x, y, z, max_rad, nil, nil, oneoftags)
	
	for i, v in ipairs(ents) do
		if v.components.inventoryitem and not v.components.inventoryitem:IsHeld() then -- If the entity is NOT held
			local isDead = v.components.health ~= nil and v.components.health:IsDead() or false
			local distsq = inst:GetDistanceSqToInst(v) - 9

			delta = delta + maxSanityRateBonusPerEntity / math.max(1, distsq) * (isDead and -1 or 1) -- Add to the delta variable
		end

		if v.components.inventoryitem == nil then -- If the entity doesn't have the 'inventoryitem' component
			local isDead = v.components.health ~= nil and v.components.health:IsDead() or false
			local distsq = inst:GetDistanceSqToInst(v) - 9

			delta = delta + maxSanityRateBonusPerEntity / math.max(1, distsq) * (isDead and -1 or 1) -- Add to the delta variable
		end
	end

	return delta
end

 

  • Like 1

Share this post


Link to post
Share on other sites
Cthonicus    13
On 4/22/2021 at 2:33 AM, -t- said:

Animals that can be picked up and held in the inventory have the 'inventoryitem' component. This component has a function called 'IsHeld' which can be used to determine whether the animal is being held currently. So you can add this check to your function:


local maxSanityRateBonusPerEntity = TUNING.SANITYAURA_SMALL

local function sanityfn(inst)
	local delta = 0
	local x, y, z = inst.Transform:GetWorldPosition()
	local max_rad = 10
	local oneoftags = { "bee", "flower", "butterfly", "rabbit", "koalefant_summer", "koalefant_winter", "chester", "squid", "glommer", "hutch", "mole" }
	local ents = TheSim:FindEntities(x, y, z, max_rad, nil, nil, oneoftags)
	
	for i, v in ipairs(ents) do
		if v.components.inventoryitem and not v.components.inventoryitem:IsHeld() then -- If the entity is NOT held
			local isDead = v.components.health ~= nil and v.components.health:IsDead() or false
			local distsq = inst:GetDistanceSqToInst(v) - 9

			delta = delta + maxSanityRateBonusPerEntity / math.max(1, distsq) * (isDead and -1 or 1) -- Add to the delta variable
		end

		if v.components.inventoryitem == nil then -- If the entity doesn't have the 'inventoryitem' component
			local isDead = v.components.health ~= nil and v.components.health:IsDead() or false
			local distsq = inst:GetDistanceSqToInst(v) - 9

			delta = delta + maxSanityRateBonusPerEntity / math.max(1, distsq) * (isDead and -1 or 1) -- Add to the delta variable
		end
	end

	return delta
end

 

Super sorry for the late response, but this totally works! Thanks a ton! :D

  • Like 1

Share this post


Link to post
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
Sign in to follow this