Jump to content

[Need help] Sanityaura on mobs


Recommended Posts

Hello,
I am just trying to mod a new Char and learn LUA, like many here.
Trying the whole day to make a simple aura on bees to regen some sanity ended up in few code-trys, but all failed.

I have collected 5 of them in a file and attached them to the post. 

I absolutely not understanding what's going on with number 1?

I was using some tutorials and codes of other mods and changed them a bit or got inspired by them.
One tutorial I remember was this one here . Even it is meant for DS, it, as said in the attached file, worked somehow for the first times and then collapsed.

Would be amazing if someone has the knowledge to help me fixing one of the codes or can reference me to some other thread or maybe has another solution?

testingstuff.lua

Link to comment
Share on other sites

If you want only your character to get a sanity boost from being close to bees, then adding a sanity aura to the bees is not the way to go. Then everyone will get the sanity aura from the bees when your character is near the bees. Also, if you keep adding sanity auras to the bees, that's gonna be a problem at some point.

I would do it like #3 is doing it...almost, anyway. I'm guessing the reason why it never crashed, is because you never actually called the function. You can't just copy/paste a function and not call it. If you had called it, it would have crashed because it is referencing a variable called "range" which you don't have. I've attached a new code snippet.

Put this in your character's master_postinit:

	-- DoPeriodicTask calls the given function every X seconds.
	-- I set it to 1.0. You can set it to e.g. 0.5 seconds if you want.
	inst:DoPeriodicTask(1.0, function(inst)
		-- Do nothing if the player is dead.
		if inst.components.health:IsDead() or inst:HasTag("playerghost") then
			return
		end
		
		local x,y,z = inst.Transform:GetWorldPosition()
		-- Description of important function:
		-- TheSim:FindEntities(x, y, z, radius, mustHaveTags, cantHaveTags, mustHaveOneOfTheseTags)
		-- I have set the radius to be 10. You can set it to whatever radius you want.
		local bees = TheSim:FindEntities(x, y, z, 10, {"bee"}, {"INLIMBO"}, nil)
		local count = 0
		for _ in pairs(bees) do count = count + 1 end
		
		-- Two versions of this next part.
		-- VERSION 1 - START
		-- This version gives the same sanity no matter how many bees are within range.
		if count > 0 then
			inst.components.sanity:DoDelta(TUNING.SANITYAURA_MED)
		end
		-- VERSION 1 - END
		
		-- VERSION 2 - START
		-- This version multiplies the sanity received by the number of bees within range.
		inst.components.sanity:DoDelta(TUNING.SANITYAURA_MED * count)
		-- VERSION 2 - END
	end)


The code above counts killer bees as well, though. They don't have special tags on them, so if you want them to not count, you can do this (I only changed the way we count the entities):

	-- DoPeriodicTask calls the given function every X seconds.
	-- I set it to 1.0. You can set it to e.g. 0.5 seconds if you want.
	inst:DoPeriodicTask(1.0, function(inst)
		-- Don't do anything if the player is dead.
		if inst.components.health:IsDead() or inst:HasTag("playerghost") then
			return
		end
		
		local x,y,z = inst.Transform:GetWorldPosition()
		-- Description of important function:
		-- TheSim:FindEntities(x, y, z, radius, mustHaveTags, cantHaveTags, mustHaveOneOfTheseTags)
		local bees = TheSim:FindEntities(x, y, z, 10, {"bee"}, {"INLIMBO"}, nil)
		local count = 0
		for i,v in ipairs(bees)do
			if v.prefab == "bee" then
				count = count + 1
			end
		end
		
		-- Two versions of this next part.
		-- VERSION 1 - START
		-- This version gives the same sanity no matter how many bees are within range.
		if count > 0 then
			inst.components.sanity:DoDelta(TUNING.SANITYAURA_MED)
		end
		-- VERSION 1 - END
		
		-- VERSION 2 - START
		-- This version multiplies the sanity received by the number of bees within range.
		inst.components.sanity:DoDelta(TUNING.SANITYAURA_MED * count)
		-- VERSION 2 - END
	end)

 

Edited by Ultroman
  • Like 1
Link to comment
Share on other sites

You are very welcome :)

If you are new to modding for DS/DST, I highly recommend saving hours of your life and loads of frustration, by looking through my newcomer post. It covers a lot of basic DS/DST modding stuff and some QoL stuff. If you are new to LUA, I urge you to do the LUA crash course. It may look boring, but it won't take long, and you'll skip sooo much frustration and grasping at straws.

Good luck in your ventures! See you on the forum ;)

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...