Jump to content

Recommended Posts

I am trying to make it so that my character gains sanity near others. I had done this before and it worked just fine. But now I've tried some codes I found from looking up the website or other mods yet none of them works, some of them even crashes the player's game when I get near them. I checked again and again but there's not any mistakes I do. What's the problem?

Put this in your "common_postinit" function:

inst.components.sanity.custom_rate_fn = CustomSanityRate

Put this above "common_postinit":

-- These are kept outside the function for optimization.
local RANGE = 8 -- 4 = 1 tile, 8 = 2 tiles, 12 = 3 tiles...
local SANITY_MUST_TAGS = { "player" } -- All tags that will be used to determine if you are near someone.
local SANITY_CANT_TAGS = { "mycharactertag" } -- If the target has these tags, they won't count.
-- Add a tag to your character to put in SANITY_CANT_TAGS, so they are ignored by the sanity gain, or use one that already exists.

local function CustomSanityRate(inst)
  	local x, y, z = inst.Transform:GetWorldPosition() -- Your current position.
	local ents = TheSim:FindEntities(x, y, z, RANGE, SANITY_MUST_TAGS, SANITY_CANT_TAGS) -- Find all entities in a radius.
  	local result = 0 -- When no one is nearby, don't give any sanity.
  
  	if ents ~= nil and #ents > 0 then
    	result = 3/60 -- 3/min. If someone is found, start recovering sanity.
    end
  
  	return result -- Give sanity.
end

 

  • Like 2
17 hours ago, HarryPPP said:

Put this in your "common_postinit" function:


inst.components.sanity.custom_rate_fn = CustomSanityRate

Put this above "common_postinit":


-- These are kept outside the function for optimization.
local RANGE = 8 -- 4 = 1 tile, 8 = 2 tiles, 12 = 3 tiles...
local SANITY_MUST_TAGS = { "player" } -- All tags that will be used to determine if you are near someone.
local SANITY_CANT_TAGS = { "mycharactertag" } -- If the target has these tags, they won't count.
-- Add a tag to your character to put in SANITY_CANT_TAGS, so they are ignored by the sanity gain, or use one that already exists.

local function CustomSanityRate(inst)
  	local x, y, z = inst.Transform:GetWorldPosition() -- Your current position.
	local ents = TheSim:FindEntities(x, y, z, RANGE, SANITY_MUST_TAGS, SANITY_CANT_TAGS) -- Find all entities in a radius.
  	local result = 0 -- When no one is nearby, don't give any sanity.
  
  	if ents ~= nil and #ents > 0 then
    	result = 3/60 -- 3/min. If someone is found, start recovering sanity.
    end
  
  	return result -- Give sanity.
end

Thank you, this one looks different from the others so I will try it and I hope it works! Also thank you for the codes' meanings ^^

Edit: It crashed the game when I put the components thing under common postinit, but when I put it under master postinit it worked. Thank you again.

 

Oh and also, another question :')

What if I want to get sanity from goats too? Adding it to the MUST TAGS would make it so that I get sanity from a thing that is both a player and a goat, wouldn't it? Because I saw MUST TAGS and MUST ONE OF TAGS somewhere. So how should I make it?

Edited by kump11r
  • Like 1
1 hour ago, jekart021 said:

 


Harry, how can I make sure that all characters increase their sanity next to others,
 but not increase from themselves?
I would be very grateful to you.
Thank you.
^_^

 

I guess there's no way to affect other players unless you mod their characters, but I am not sure.

4 hours ago, jekart021 said:

 


Harry, how can I make sure that all characters increase their sanity next to others,
 but not increase from themselves?
I would be very grateful to you.
Thank you.
^_^

 

This should work. This makes all players have this behaviour, even modded characters that don't have a sanity aura already.

You won't receive any sanity from your own aura.

AddPlayerPostInit(function(inst)
	if not GLOBAL.TheWorld.ismastersim then
		return inst
	end
	
	if inst.components.sanityaura == nil then -- In case a modded character already has this.
		inst:AddComponent("sanityaura")
		inst.components.sanityaura.aurafn = function(inst, observer)
			return observer:HasTag("player") and not observer:HasTag("playerghost") and 6.7/60 or 0
		end
	end
end)

The size of the aura depends on the sanity it gives.

4 hours ago, kump11r said:

Edit: It crashed the game when I put the components thing under common postinit, but when I put it under master postinit it worked. Thank you again.

 

Oh and also, another question :')

What if I want to get sanity from goats too? Adding it to the MUST TAGS would make it so that I get sanity from a thing that is both a player and a goat, wouldn't it? Because I saw MUST TAGS and MUST ONE OF TAGS somewhere. So how should I make it?

Move the MUST_TAGS variable over to be the last value, like this:

local ents = TheSim:FindEntities(x, y, z, RANGE, nil, SANITY_CANT_TAGS, SANITY_MUST_TAGS)

The 7th value is the "MUST ONE OF TAGS" you're talking about.

 

Edit: Funnily enough, you helped me with my own mod. Haha! I needed to check if an entity had one of the tags in a table...

Edited by HarryPPP
Tweaked the values.

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
×
  • Create New...