Jump to content

Mod Coding help - Bee allergy


Recommended Posts

I'm actually wondering if it's possible to make my character Groggy when stung by a bee, and then only until using an adrenaline shot they can be back to normal. Would anyone happen to know how? I had made a character that got groggy under 30 of hunger, with that code: 

if inst.components.hunger.current >= (0) and inst.components.hunger.current < (30) then
			if inst:HasTag("groggy") then return
		else
			inst:AddTag("groggy")
    		inst.components.locomotor:SetExternalSpeedMultiplier(inst, "grogginess", 0.6)

This could somehow be written to when the character gets stung?

Link to comment
Share on other sites

You can do this with a special component:

scripts/components/allergy.lua

local defaultAllergens = 
{
	bee = true,
	killerbee = true,
	beequeen = true,
	beeguard = true,
}

local function CheckPushAllergy(inst, data)
	local self = inst.components.allergy
	if not self.allergy and data and data.attacker and self.allergens[data.attacker.prefab] then
		self:PushAllergy(data)
	end
end

local function CheckRemoveAllergy(inst)
	local self = inst.components.allergy
	if self.allergy then  
		self:RemoveAllergy()
	end
end

local Allergy = Class(function(self, inst)
    self.inst = inst
	self.allergens = defaultAllergens
	self.allergySlowMult = 0.5
	self.allergy = false
	
	inst:ListenForEvent("attacked", CheckPushAllergy)
	inst:ListenForEvent("lifeinjectorused", CheckRemoveAllergy)
end)

function Allergy:OnRemoveFromEntity()
    self.inst:RemoveEventCallback("attacked", CheckPushAllergy)
	self.inst:RemoveEventCallback("lifeinjectorused", CheckRemoveAllergy)
end

function Allergy:PushAllergy()
	local inst = self.inst
	if inst.components.locomotor ~= nil then
		inst.components.locomotor:SetExternalSpeedMultiplier(inst, "allergy", self.allergySlowMult) 
	end
	
	self.allergy  = true
	inst:AddTag("groggy")
end

function Allergy:RemoveAllergy()
	local inst = self.inst
	if inst.components.locomotor ~= nil then
        inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "allergy") 
    end
	--dont need to remove this tag if the player is groggy (when fighting a bearger for example)
	if inst.components.grogginess == nil or inst.components.grogginess.grog_amount == 0 then
		inst:RemoveTag("groggy")
	end
	
	self.allergy = false
end

function Allergy:OnSave()
    return { allergy = self.allergy }
end

function Allergy:OnLoad(data)
    if data and data.allergy then 
        self:PushAllergy()
    end
end

return Allergy

modmain.lua

--lifeinjector has no events, so we need to push it
AddComponentPostInit( "maxhealer", function(self) 
	self._oldHeal = self.Heal
	self.Heal = function(self, target)
		local check = self:_oldHeal(target)
		if check then
			if self.inst.prefab == "lifeinjector" then
				target:PushEvent("lifeinjectorused")
			end
		end
		
		return check
	end
end)

Component makes an allergy status savable. May be there is a way to keep these functions in a character file, but I don't think that using OnSave/OnLoad in players prefabs is a good idea.

Link to comment
Share on other sites

@ksaab WOAH, I didn't expect so much!! I never used components before so I'm glad to learn about it now for future mods, didn't have the reflex for it! I can't thank you enough, it works like a charm!! If you ever need an artist for assets, I'm all up for you!

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