Jump to content

Recommended Posts

Hey there!

Soo, I've been thinking about a survivor perk, but I'm not sure how to make it performance efficient.

The idea is that she should gain different benefits (speed boost, strength increase, etc) based on certain items in her inventory. I've made this base coding for this ability (I didn't test it before though):

Spoiler

local function BunnyItem(inst)
    if inst and inst.components.inventory then
        local item = inst.components.inventory:FindItems(function(item) return item.prefab == "rabbit" end)
        if item then
            inst.components.locomotor.walkspeed = 4.5
        else
            inst.components.locomotor.walkspeed = 4
        end
    end
end

In short, her base speed should be 4, but as long as she holds a rabbit in her inventory it should be 4.5 instead. Item quantity shouldn't affect the outcome, it's an on or off scenario.

Let's assume my script works (please, note if it isn't). To make it run, I would add a periodic task to check the inventory reguralry. The problem with this is that if I'll have several similar perks to this, the game would be overpressured with the constant checkings.

What would be the most ideal approach to this perk-system?

Thanks in advance, as always!

I actually had a similar problem not to long ago where I wanted to check the inventory if a player has at least one of each gem.

This is what I came up with:

Spoiler

		local gems = {orangegem = false, yellowgem = false, greengem = false, redgem = false, bluegem = false, purplegem = false,} --list of all different gems
		local amount = 0
		local CheckGems --I need this here to remove the Event Callback, this is not necessary for you
		local function CheckForGems(inst,item,added)
			for k,v in pairs(gems) do --goes thorugh all different gems
				if k == item.prefab then
					if added then	--if the item was added and not there already, set it to true, you can add your perk
						if v == false then
							gems[k] = true
							amount = amount + 1
							inst:PushEvent("quest_update",{quest = "Colors of the Rainbow",amount = 1})
							if amount >= 6 then
								inst:RemoveEventCallback("itemget",CheckGems)
								inst:RemoveEventCallback("itemlose",CheckGems)
							end
						end
						return
					else
						if v == true then --if the item was removed and it was already there, check if there are more in the inventory
							if inst.components.inventory:Has(k,1) == false then --this checks if there is at least one of this item in the inventory
								gems[k] = false --here you can remove your perk
								amount = amount - 1
								inst:PushEvent("quest_update",{quest = "Colors of the Rainbow",amount = -1})
							end
						end
						return
					end
				end
			end
		end
		CheckGems = function(inst,data)
			if data then 
				if data.item then
					CheckForGems(inst,data.item,true)
				elseif data.prev_item then
					CheckForGems(inst,data.prev_item,false)
				end
			end
		end
		inst:ListenForEvent("itemget",CheckGems) --this is called each time an item is added to the inventory
		inst:ListenForEvent("itemlose",CheckGems) --this is called each time an item is removed from the inventory

 

You will probably need to change a few things to get it working for your perks, but this is the best basic structure I came up with.

Keep in mind that this won't apply to items that are added by being equipped, these send the event "equip" and "unequip", but if your character doesn't need to hold equippable items for his perks, it's not necessary.

If something is unclear as to how I did this let me know :)

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