Jump to content

How can I make Trader accept stacks?


Recommended Posts

The only piece of code i can see is responsible for this can be found in trader.lua 101-136 but I dont know how can I implement this.

Currently im only using onaccpet, onrefuse and setaccepttest but this allows me to take only 1 item from stack rather than whole stack

Link to comment
Share on other sites

It looks like you'll have to write a wrapper for the "Give" action to account for it. Are you trying to apply it globally or is it just meant for one particular NPC? Either way this should do the trick for you, you'll just have to remove the prefab check if you don't need it.

local ACTIONS = GLOBAL.ACTIONS

local _GIVE_fn = ACTIONS.GIVE.fn

ACTIONS.GIVE.fn = function(act)
	if act.target and act.target.prefab == "YOUR_TRADER" then
		local able, reason = act.target.components.trader:AbleToAccept(act.invobject, act.doer)
		if not able then
			return false, reason
		end
		local count = act.invobject.components.stackable and act.invobject.components.stackable.stacksize or 1
		act.target.components.trader:AcceptGift(act.doer, act.invobject, count)
		return true
	end
	return _GIVE_fn(act)
end

Keep in mind that if this is something you want to change universally, this won't change the behavior of any of the current trader NPCs after you give them something. This means the Pig King will not realize if you've given him a stack of meat, he'll still just give you 1 gold.

In order to change the behavior to account for the stack size, you'll have to change the trader's onaccept callback function so that it checks for the stacksize on the "item" parameter that gets passed (called using the same path as above, excpet in this case it would be item.components.stackable.stacksize)

Edited by w00tyd00d
clarity
Link to comment
Share on other sites

On 7/26/2022 at 2:31 AM, w00tyd00d said:

It looks like you'll have to write a wrapper for the "Give" action to account for it. Are you trying to apply it globally or is it just meant for one particular NPC? Either way this should do the trick for you, you'll just have to remove the prefab check if you don't need it.


local ACTIONS = GLOBAL.ACTIONS

local _GIVE_fn = ACTIONS.GIVE.fn

ACTIONS.GIVE.fn = function(act)
	if act.target and act.target.prefab == "YOUR_TRADER" then
		local able, reason = act.target.components.trader:AbleToAccept(act.invobject, act.doer)
		if not able then
			return false, reason
		end
		local count = act.invobject.components.stackable and act.invobject.components.stackable.stacksize or 1
		act.target.components.trader:AcceptGift(act.doer, act.invobject, count)
		return true
	end
	return _GIVE_fn(act)
end

Keep in mind that if this is something you want to change universally, this won't change the behavior of any of the current trader NPCs after you give them something. This means the Pig King will not realize if you've given him a stack of meat, he'll still just give you 1 gold.

In order to change the behavior to account for the stack size, you'll have to change the trader's onaccept callback function so that it checks for the stacksize on the "item" parameter that gets passed (called using the same path as above, excpet in this case it would be item.components.stackable.stacksize)

Its only for one entity, ill try this method

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