Jump to content

Detecting an item in Inventory


aile11

Recommended Posts

Hello dear Helpers

I am trying to get a Character to gain Sanity while being near Players, or having a Rabbit in their Inventory.

I managed to get it to work when a Player is near, but i didn't get the Rabbit to work.

(The Code for the Players are from another Charactermod that we had)

 

The Code i that i tried to get to work

local x, y, z = inst.Transform:GetWorldPosition()
local ents = TheSim:FindEntities(x, y, z, 10 ,{"player"})
	
if table.getn(ents) > 2 or ThePlayer.components.inventory:FindItem(function(item) return item.prefab=="item_rabbit" or "item_rabbit_winter" end)
then
	inst.components.sanity.night_drain_mult = 0
	inst.components.sanity.current = inst.components.sanity.current + 1
else
	inst.components.sanity.night_drain_mult = 2.5
end

 

Link to comment
Share on other sites

The prefab is "rabbit", regardless of season or beardiness. Thus, you can probably use inventory:Has("rabbit",1) instead.

Do not raise sanity directly, use DoDelta() instead. The second or third parameter can be set to "true" to disable the sound and flash on the badge.

Link to comment
Share on other sites

Oh, i actually had rabbit, but i tested it that way too, forgot to change it back before posting.

I should know better than to post something before checking it :?

 

I had to search up how to correctly use DoDelta, found the awnser in a Post that you replied to.

That did fix the issue of my Friend being insane even if his Sanity is high, so thank you for that :D

 

My Problem is that it gives me true even if i don't have a Rabbit in my Inventory

I tried to print out the value of ThePlayer.components.inventory

But it gave me a few Letters and numbers witch didn't help me at all.

 

So no matter what, i'm getting Sanity all the time.

Link to comment
Share on other sites

I laughed as i read that.

Well, thats a great job i did there XD

 

It is called on the Server, i didn't think about about how logical Theplayer on the Server is.

But replaceing ThePlayer with inst didn't have any effect.

 

Thank you for the help so far.

Link to comment
Share on other sites

So, i changed the Code a bit  just so the Sanity gain from a nearby Player works somewhat dynamic.

 

local function PeriodicCheck(inst)
	local x, y, z = inst.Transform:GetWorldPosition()
	local SanityGain = 
	local NightDrain = 2.5
	local i = 1
	
	while i < 30 do
		if TheSim:FindEntities(x, y, z, i,{"player"}) > 1
			then
			SanityGain = 1 - i / 30
			NightDrain = 0.08 * i
			break
		end
		i = i + 1
	end
	
	-- if inst.components.inventory:FindItem(function(item) return item.prefab=="rabbit" or "rabbit_winter" end)
		-- then
			-- SanityGain + 0.2
			-- NightDrain - 0.5
	-- end
	-- if NightDrain <  then
		-- NightDrain = 
	-- end
	
	inst.components.sanity.night_drain_mult = NightDrain
	inst.components.sanity:DoDelta(SanityGain, true)
end

It gets called in the master_postinit function in a inst:DoPeriodicTask every 0.4 seconds.

just to give more information, i'm sure thats right since the part with the player worked before i changed it to the above, witch should work but we didn't test yet.

 

The part thats commented out is the Rabbit part that i can't get to work.

Link to comment
Share on other sites

On 1.5.2016 at 7:13 AM, Mobbstar said:

Wait, is this a client-side calculation? It should only happen on the server. And if it's a server-side calculation, you can imagine who ThePlayer is...

Use inst instead of ThePlayer

I'm still not getting ths to work

Quote

if inst.components.inventory:FindItems(function(item) return item.prefab=="rabbit" or "rabbit_winter" end)
        then
            SanityGain = SanityGain + 0.2
            NightDrain = NightDrain - 0.5
    end

 

I searched the Internet and couldn't find anything helpful.

Please help?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...