Jump to content

[SOLVED] Detect Held Item


Recommended Posts

Hey

I'm trying to make a Char that can only hold one item. Right now I'm testing it out with an axe, but I don't know how to do it.

Can someone please tell me what I'm doing wrong?

	-- Drop Items
	local function drop(inst)
	local hand = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	if hand ~= "axe" then 
		inst:DoTaskInTime(0.0, function()
				local item = inst.components.inventory:Unequip(EQUIPSLOTS.HANDS)
				inst.components.inventory:DropItem(item)
			inst.components.talker:Say(GetString(inst, "ANNOUNCE_UNEQUIP_TOOL"))
				inst.AnimState:ClearOverrideSymbol("swap_object")
		end)
	end

I know that this part is wrong, I just guessed what goes there.

if hand ~= "axe" then
Link to comment
Share on other sites

What you'll want to do (if it's going to be a DST exclusive mode) is change the "hand" variable to this instead:
 

local hand = inst.replica.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)

because "replica" is essentially a wrapper component that returns the "component" information if the player is the host or the "classified" information if the player is a client

But to solve the problem you're having, that function gets the entity of the item in your hand, but you want to check specifically what prefab it is which is a value in the entity's table. So just change the check to this and it should identify it properly:
 

if hand.prefab ~= "axe" then

Hope this helps :)

Link to comment
Share on other sites

It's only important to replace the "component" to "replica" when it comes to checking the data in the inventory component, so with the actual command to do whatever you want to do you can just use "inst.components.inventory" and the game will automatically do what it has to do to apply it to clients.

Also you will need to either need to call "EQUIPSLOTS" from the global table in your function there or write a local variable referring to the global table, like this:

local EQUIPSLOTS = GLOBAL.EQUIPSLOTS

 

Link to comment
Share on other sites

On 09/10/2017 at 3:29 PM, w00tyd00d said:

Also you will need to either need to call "EQUIPSLOTS" from the global table in your function there or write a local variable referring to the global table, like this:

where should i place this part?

Link to comment
Share on other sites

16 hours ago, Seilakk said:

where should i place this part?

Usually it's good to declare it at the top of your modmain since it must be declared before anything else that uses it in your modmain. Same rule applies for any variable that's local to a script. ;) 

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