Totopon_XD

Need help with a item that can take other items from afar

Recommended Posts

Totopon_XD    35

so i need help with the code of my "tool"/item that can take Items from afar thats the idea but i don't know how to make it work and how to do it, The idea is simple, one item/"tool" that the character hold like any other "tool"/item, but when you "pick" any item the "tool" will take the item for you, Like the Lazy forager!, but least annoying.

Share this post


Link to post
Share on other sites
Ziro2k    11

You will want to create a new component action to do this. It should be of type "EQUIPPED" since you want it to depend on having a particular tool equipped. First, you must define a new component file (in my example I called it "telepicker") and add it to the tool in order for the component action to trigger. Here is a basic framework I wrote for this ability.

local TELEPICKUP = AddAction("TELEPICKUP", "Retrieve", function(act) --Replace "Retrieve" with whatever you want the tooltip to say.
        if act.target and act.invobject and act.doer and act.doer.components.inventory then
       		act.doer.components.inventory:GiveItem(act.target, nil, act.target:GetPosition())
        end
        return true
end)

TELEPICKUP.priority = 10
TELEPICKUP.distance = 36 --Same range as Lazy Forager, but you can change it to whatever you want.

local PICKUP_CANT_TAGS = { "INLIMBO", "NOCLICK", "knockbackdelayinteraction", "catchable", "fire", "minesprung", "mineactive" }

AddComponentAction("EQUIPPED", "telepicker", function(inst, doer, target, actions, right)
    	--Below, "right" makes it so that the action is triggered by right clicking the mouse. Remove it if you want.
        if right and target and target.replica.inventoryitem and target.replica.inventoryitem:CanBePickedUp() then
                for i, tag in ipairs (PICKUP_CANT_TAGS) do
                     if target:HasTag(tag) then return end
                end
                table.insert(actions, TELEPICKUP)
    	end
end)

--These make the action use the "quickcastspell" animation, but if you want it to be instant then you remove these and put "TELEPICKUP.instant = true"
AddStategraphActionHandler("wilson", GLOBAL.ActionHandler(TELEPICKUP, "quickcastspell"))
AddStategraphActionHandler("wilson_client", GLOBAL.ActionHandler(TELEPICKUP, "quickcastspell"))

The above code will make it so that a player can right click on an item in the world that's in range while your tool is equipped, and the item will be put in their inventory. You can add your own FX to make it look nice, similar to the Lazy Forager, if you wish. 

Edited by Ziro2k
  • GL Happy 1

Share this post


Link to post
Share on other sites
Totopon_XD    35
1 hour ago, Ziro2k said:

You will want to create a new component action to do this. It should be of type "EQUIPPED" since you want it to depend on having a particular tool equipped. First, you must define a new component file (in my example I called it "telepicker") and add it to the tool in order for the component action to trigger. Here is a basic framework I wrote for this ability.


local TELEPICKUP = AddAction("TELEPICKUP", "Retrieve", function(act) --Replace "Retrieve" with whatever you want the tooltip to say.
        if act.target and act.invobject and act.doer and act.doer.components.inventory then
       		act.doer.components.inventory:GiveItem(act.target, nil, act.target:GetPosition())
        end
        return true
end)

TELEPICKUP.priority = 10
TELEPICKUP.distance = 36 --Same range as Lazy Forager, but you can change it to whatever you want.

local PICKUP_CANT_TAGS = { "INLIMBO", "NOCLICK", "knockbackdelayinteraction", "catchable", "fire", "minesprung", "mineactive" }

AddComponentAction("EQUIPPED", "telepicker", function(inst, doer, target, actions, right)
    	--Below, "right" makes it so that the action is triggered by right clicking the mouse. Remove it if you want.
        if right and target and target.replica.inventoryitem and target.replica.inventoryitem:CanBePickedUp() then
                for i, tag in ipairs (PICKUP_CANT_TAGS) do
                     if target:HasTag(tag) then return end
                end
                table.insert(actions, TELEPICKUP)
    	end
end)

--These make the action use the "quickcastspell" animation, but if you want it to be instant then you remove these and put "TELEPICKUP.instant = true"
AddStategraphActionHandler("wilson", GLOBAL.ActionHandler(TELEPICKUP, "quickcastspell"))
AddStategraphActionHandler("wilson_client", GLOBAL.ActionHandler(TELEPICKUP, "quickcastspell"))

The above code will make it so that a player can right click on an item in the world that's in range while your tool is equipped, and the item will be put in their inventory. You can add your own FX to make it look nice, similar to the Lazy Forager, if you wish. 

Omg thank you, another question i have to put that in the (Toolname).lua?

Share this post


Link to post
Share on other sites
Ziro2k    11
1 minute ago, Totopon_XD said:

Omg thank you, another question i have to put that in the (Toolname).lua?

You need to put AddComponent("telepicker") in (toolname).lua, the rest would go in modmain.

Share this post


Link to post
Share on other sites
Totopon_XD    35
On 11/27/2020 at 2:09 PM, Ziro2k said:

You need to put AddComponent("telepicker") in (toolname).lua, the rest would go in modmain.

how a component is created, and where i have to put it?

 

(sorry for all the question, i am new on modding for dst)

Share this post


Link to post
Share on other sites
Ziro2k    11
14 hours ago, Totopon_XD said:

how a component is created, and where i have to put it?

 

(sorry for all the question, i am new on modding for dst)

You create a component by defining a new lua file and putting it in scripts/components folder. You're not really using the component for anything else, so the component itself can be completely barebones. It's really just a way for the game to recognize that this component action should be associated with the item. Put the following in the new lua file:

local TelePicker = Class(function(self, inst)
end)

return TelePicker

Then name it "telepicker.lua" and put it in the folder I mentioned above. The game will automatically recognize it when you call AddComponent("telepicker").

Share this post


Link to post
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