Jump to content

How to detect if the player is holding a item


Recommended Posts

This may seem strange but i want to do a character that has night vision when holding a torch, i already did the night vision but i don't know how to turn it on or off. can anyone help me?

local BEAVERVISION_COLOURCUBES =
{
    day = "images/colour_cubes/beaver_vision_cc.tex",
    dusk = "images/colour_cubes/beaver_vision_cc.tex",
    night = "images/colour_cubes/beaver_vision_cc.tex",
    full_moon = "images/colour_cubes/beaver_vision_cc.tex",
}
local function common_init(inst)
	inst.components.playervision:SetCustomCCTable(BEAVERVISION_COLOURCUBES)
end

 

Link to comment
Share on other sites

What would probably be the easiest way to implement it would be to add it to the "onequip" and "onunequip" functions of the torch. From what I can tell in the code, the only thing the game looks for to turn on the nightvision mechanic is to add the tag "nightvision" to piece of armor you're equipped with. So if you wanted to make it character specific, add a specific tag for your character that you can check for when equipping the torch. Then for the unequip all you have to do is write "RemoveTag("nightvision")" so that if someone else were to equip the torch after you, they wouldn't get the same nightvision effect.

Spoiler

local function onequip(inst, owner)
    inst.components.burnable:Ignite()

    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("equipskinneditem", inst:GetSkinName())
        owner.AnimState:OverrideItemSkinSymbol("swap_object", skin_build, "swap_torch", inst.GUID, "swap_torch")
    else
        owner.AnimState:OverrideSymbol("swap_object", "swap_torch", "swap_torch")
    end
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")

    owner.SoundEmitter:PlaySound("dontstarve/wilson/torch_swing")

    if inst.fires == nil then
        inst.fires = {}

        for i, fx_prefab in ipairs(inst:GetSkinName() == nil and { "torchfire" } or SKIN_FX_PREFAB[inst:GetSkinName()] or {}) do
            local fx = SpawnPrefab(fx_prefab)
            fx.entity:SetParent(owner.entity)
            fx.entity:AddFollower()
            fx.Follower:FollowSymbol(owner.GUID, "swap_object", 0, fx.fx_offset, 0)

            table.insert(inst.fires, fx)
        end
    end

	-- This bit is what you would add
	
	if owner:HasTag("your-tag-here") then
		inst:AddTag("nightvison")
	end
end

local function onunequip(inst, owner)
    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("unequipskinneditem", inst:GetSkinName())
    end

    if inst.fires ~= nil then
        for i, fx in ipairs(inst.fires) do
            fx:Remove()
        end
        inst.fires = nil
        owner.SoundEmitter:PlaySound("dontstarve/common/fireOut")
    end

    inst.components.burnable:Extinguish()
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal")

	-- Added
	inst:RemoveTag("nightvision")	
end

 

Hope this helps :)

Link to comment
Share on other sites

it didn't work :(... probably because i changed the night vision code to a better one. I tried to adapt the code but it didn't work either, i don't know anything about programming, but thanks anyway

If you could adapt it to me i will be very thankful

Spoiler

local FODASE =
{
    night = "images/colour_cubes/purple_moon_cc.tex",
    full_moon = "images/colour_cubes/purple_moon_cc.tex",
}

local function toneiai(inst, enable)
    if TheWorld.state.isnight or TheWorld:HasTag("cave") then
        inst.components.playervision:ForceNightVision(true)
        inst.components.playervision:SetCustomCCTable(FODASE)
    else
        inst.components.playervision:ForceNightVision(false)
        inst.components.playervision:SetCustomCCTable(nil)
    end
end

local function common_init(inst)
	toneiai(inst)
	inst:WatchWorldState( "isday", function() toneiai(inst) end)
  	inst:WatchWorldState( "isdusk", function() toneiai(inst) end)
  	inst:WatchWorldState( "isnight", function() toneiai(inst)  end)
	inst:WatchWorldState( "iscaveday", function() toneiai(inst) end)
  	inst:WatchWorldState( "iscavedusk", function() toneiai(inst) end)
  	inst:WatchWorldState( "iscavenight", function() toneiai(inst)  end)
end

 

 

Link to comment
Share on other sites

On 10/11/2017 at 10:35 AM, Seilakk said:

it didn't work :(... probably because i changed the night vision code to a better one. I tried to adapt the code but it didn't work either, i don't know anything about programming, but thanks anyway

If you don't know anything about programming, how do you know you changed it to a better one ;P

lol Ball-busting aside, I'm assuming you're changing the code to try change the color of the nightvision? Unfortunately I'm not too familiar with how the nightvision code works myself, I only took a quick peak at how it was set up. What exactly happened when you tried to apply the changes that I suggested?

Link to comment
Share on other sites

1 hour ago, w00tyd00d said:

I'm assuming you're changing the code to try change the color of the nightvision?

I changed the code to one that activates at night, and the first code has a lot of problems, but to change the color you just have to change night = "images/colour_cubes/beaver_vision_cc.tex" to a different filter  like purple_moon_cc.tex.

1 hour ago, w00tyd00d said:

What exactly happened when you tried to apply the changes that I suggested?

The detection simply didn't work, i tried removing the nightvision tag and adding the tag "bookbuilder" instead to see it it actually works but it didn't

Link to comment
Share on other sites

34 minutes ago, Seilakk said:

The detection simply didn't work, i tried removing the nightvision tag and adding the tag "bookbuilder" instead to see it it actually works but it didn't

Well I hope you didn't copy and paste the code I put here because upon re-reading it, there was a typo lol I accidentally wrote "nightvison" instead of "nightvision"...so that part is my mistake :p

I just checked tho and it does work as I expected, the only problem I was having was applying a custom ccgraph to it, for some reason the custom one wasn't getting applied and unfortunately I don't really have the time to look in to it much further atm, my apologies :\

But if you try the method I had suggested again and make sure you don't use my atrocious typo, then it at least gets the nightvision detection upon equipping the torch to work.

Link to comment
Share on other sites

i think i am doing something wrong

Spoiler

local function common_init(inst)
	inst:AddTag("fodase")
end

local function onequip(inst, owner)
    inst.components.burnable:Ignite()

    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("equipskinneditem", inst:GetSkinName())
        owner.AnimState:OverrideItemSkinSymbol("swap_object", skin_build, "swap_torch", inst.GUID, "swap_torch")
    else
        owner.AnimState:OverrideSymbol("swap_object", "swap_torch", "swap_torch")
    end
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")

    owner.SoundEmitter:PlaySound("dontstarve/wilson/torch_swing")

    if inst.fires == nil then
        inst.fires = {}

        for i, fx_prefab in ipairs(inst:GetSkinName() == nil and { "torchfire" } or SKIN_FX_PREFAB[inst:GetSkinName()] or {}) do
            local fx = SpawnPrefab(fx_prefab)
            fx.entity:SetParent(owner.entity)
            fx.entity:AddFollower()
            fx.Follower:FollowSymbol(owner.GUID, "swap_object", 0, fx.fx_offset, 0)

            table.insert(inst.fires, fx)
        end
    end

	-- This bit is what you would add
	
	if owner:HasTag("fodase") then
		inst:AddTag("bookbuilder")
	end
end

local function onunequip(inst, owner)
    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("unequipskinneditem", inst:GetSkinName())
    end

    if inst.fires ~= nil then
        for i, fx in ipairs(inst.fires) do
            fx:Remove()
        end
        inst.fires = nil
        owner.SoundEmitter:PlaySound("dontstarve/common/fireOut")
    end

    inst.components.burnable:Extinguish()
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal")

	-- Added
	inst:RemoveTag("bookbuilder")	
end

 

i am using the bookbuilder tag because it displays a tab on the crafting menu, here is the video of it not working,

Don't Starve Together 12_10_2017 17_12_51.mp4

sorry if i am bothering u too much..... :p

Edited by Seilakk
Link to comment
Share on other sites

Maybe you misunderstood the HasTag part, it is used to detect your character prefab, so the 

inst:AddTag("fodase")

should be in your character's prefab. Or you can simply replace 

if owner:HasTag("fodase") then

with

if owner.prefab=="your character prefab name" then

 

Link to comment
Share on other sites

Well this is my code for wilson in torch.lua

local ct=
{
    night = "images/colour_cubes/purple_moon_cc.tex",
    full_moon = "images/colour_cubes/purple_moon_cc.tex",
}

local function onequip(inst, owner)
	if owner.prefab=="wilson" and not inst:HasTag("nightvision") then
		owner.components.playervision:SetCustomCCTable(ct)
		inst:AddTag("nightvision")
	end
-- leave out the original code here

local function onunequip(inst, owner)
	if owner.prefab=="wilson" and inst:HasTag("nightvision") then
		owner.components.playervision:SetCustomCCTable(nil)
		inst:RemoveTag("nightvision")
	end
-- leave out the original code here

 

Link to comment
Share on other sites

Well, Let's go back to your original ideal. Modifying torch seems a bad idea for synchronizing.

local FODASE =
{
    night = "images/colour_cubes/purple_moon_cc.tex",
    full_moon = "images/colour_cubes/purple_moon_cc.tex",
}

local function updatesight(inst, phase)
	if phase=="night" or (type(phase)~="string" and TheWorld.state.isnight) or TheWorld:HasTag("cave") then
		local item=inst.replica.inventory and inst.replica.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		if item and item.prefab=="torch" then
			inst.components.playervision:SetCustomCCTable(FODASE)
			inst.components.playervision:ForceNightVision(true)
			return
		end
	end
	inst.components.playervision:SetCustomCCTable(nil)
	inst.components.playervision:ForceNightVision(false)
end
	
local function common_init(inst)
	inst:ListenForEvent("equip",updatesight)
	inst:ListenForEvent("unequip",updatesight)
	inst:WatchWorldState("phase", updatesight)
	if not TheWorld.ismastersim then
		inst:ListenForEvent("inventoryclosed",updatesight)
	end
end)

 

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