Jump to content

How to cause a character to lose sanity when close to players


Recommended Posts

I want my character to lose sanity when nearby other players, but code ripped from Wolfgang's prefab file doesn't appear to be working. Is there a different way to do this, or what am I doing wrong? At the very least the presence of players looks like it should be influencing the multiplier of negative sanity drain, but it isn't.

I added the character prefab file. The lines with the code that should be causing sanity drain but aren't are from lines 46 to 67.

loboto.lua

Link to comment
Share on other sites

6 hours ago, Honeebey said:

Snipped

 

I read your Lua file, and you didn't reference the inst.playercheck_task on your character upon loading which means it doesn't trigger said function at all. If you use wolfgang's prefab file as reference again, you'll notice that in his onbecamehuman and onload functions, he calls the StartPlayerCheck function. Which means, upon loading in or upon being revived/turned human, the character fires the StartPlayerCheck function. 

 

So to fix this, in we do the following:
 

local function onbecamehuman(inst)
	-- Set speed when not a ghost (optional)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "loboto_speed_mod", 1)
	StartPlayerCheck(inst)
end

and
 

local function onbecameghost(inst)
	-- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "loboto_speed_mod")
   
     if inst.playercheck_task ~= nil then
        inst.playercheck_task:Cancel()
        inst.playercheck_task = nil
    end
end

^We cancel the task once we're dead

We don't need to call the function on loading since by default, onbecamehuman function is immediately triggered by onload

 

EDIT: Okay, upon posting everything above, I read what's inside the CheckForPlayers functions and your assumption is correct. I believe this function only affects his sanity multipliers and doesn't reduce your character's sanity when near player entities at all.

 

To do the sanity drain when near other players perk, we make a custom sanity function for loboto:

local MUST_NOT_TAGS = {"playerghost", "INLIMBO"} -- dead players are not counted
local function customsanityfn (inst)
	local delta = 0
	local x, y, z = inst.Transform:GetWorldPosition()
	local ents = TheSim:FindEntities(x, y, z, ENTER_HOW_FAR_HE_GETS_PARANOID_HERE, {"player"}, MUST_NOT_TAGS) -- searches for players within range
	for k, v in pairs(ents) do --means for each player do the lines of code below
		if v ~= inst then -- yourself is not included
			local bonus_sanity = -TUNING.SANITYAURA_SMALL --how powerful the drain can be, referenced from tuning.lua 
			local distsq = math.max(inst:GetDistanceSqToInst(v), 1) -- the closer you are to the player entity, the larger the drain
			delta = delta + bonus_sanity / distsq
		end
	end	
	
	return delta
end

^ We add the following anywhere above the common postinit then finally:

 

	inst.components.sanity.custom_rate_fn = customsanityfn

^ We add the following somewhere under the master_postinit

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