Jump to content

Custom character perk - External science bonus


Recommended Posts

Hello there, everyone! I've been trying to work on a perk for a character for a little while now and I'm not entirely sure how to accomplish it. I'm not sure if this has been asked before (or if I'm in the right area TO ask) but here goes.

Essentially, I'm attempting to figure out how to give a mod character the perk of providing nearby players with a science bonus, similar to how a science machine does. I thought I could modify Wickerbottom's code to do this as she gets the science bonus perk herself, but I've yet to figure out how to do so. I also looked through what code I could find for the game, but was unable find anything on the science machine or any similar devices. I'm pretty sure I'm missing something very obvious, but if anyone is aware of how I could manage this, I would be very grateful to know. Thank you in advance!

Link to comment
Share on other sites

Let me help you. I have implemented the code what you're trying to do.
Here's the code first. 

In master_postinit()

ScienceAura(inst)

and local functions(put this code in your character.lua where isn't belong to any other functions)

local AuraRadius = 5

local function AddScienceBonusRemovalHandler(inst)
	if inst.ScienceAuraRangeCheck ~= nil then return end
	inst.ScienceAuraRangeCheck = inst:DoPeriodicTask(10 * FRAMES, function(inst)
		if FindEntity(inst, AuraRadius, nil, {"scienceprovider"}) == nil then
			inst:RemoveTag("scienceaura")
			local bonus = inst.components.builder.science_bonus
			inst.components.builder.science_bonus = bonus - 1 
			inst.ScienceAuraRangeCheck:Cancel()
			inst.ScienceAuraRangeCheck = nil
		end
	end)
end

local function ScienceAura(inst)
	inst:AddTag("scienceprovider")
	inst.components.builder.science_bonus = 1
	inst:DoPeriodicTask(10 * FRAMES, function(inst)
		local x, y, z = inst.Transform:GetWorldPosition()
		local ents = TheSim:FindEntities(x, y, z, AuraRadius, {"player"}, {"scienceaura", "playerghost", "scienceprovider"})
		-- Added tag check to prevent perk vanishing that increases tech bonus(like Wickerbottom or other modded characters)
		if ents ~= nil then 
			for k, player in pairs(ents) do
				player:AddTag("scienceaura")
				local bonus = player.components.builder.science_bonus -- Rename it if you want.
				-- Available tech trees : science, magic, ancient, shadow   
				player.components.builder.science_bonus = bonus + 1
				AddScienceBonusRemovalHandler(player)
			end
		end
	end)
end


My first approach to implement the 'aura' was playerprox components. However, I soon realized that this is not the one for the entity who came close to your character. That component was focused on the subject rather than the approximated. It can set a 'target' to do something but only one at a time. Even it can't do anything to the approximated. So I have made a new function.

ScienceAura() is the implement of the perk. It grants your character to have one science bonus. And trying to give science bonus to the player where is in range of 5 around your character every 10 frames(0.33 seconds). If someone gets the perk, it will have the tag "scienceaura" which means the player has the bonus of the perk.
By the must & excluding tags,

..., {"player"}, {"scienceaura", "playerghost", "scienceprovider"})

you know which one should have the effect without overlapping issue.

AddScienceBonusRemovalHandler() is to remove the effect when they're out of the range of the aura.
It simply checks nearby "scienceprovider" is exist and if not, removes the effect every 10 frames.

You don't need to think about how to sync with the client. Because
builder component checks the bonus every tick.
Read comments and fix and tweak it as you want.

Also, codes I have implemented for other forum users including this are uploaded to here. So the other can see it.

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