Jump to content

Character-specific item behavior


Recommended Posts

Hey, been working on porting my mod to Don't Starve Together. In my mod, I've modified the inv_rocks.lua file so that my character can equip them (and then use them as ranged weapons). It was fairly simple in Don't Starve because a given rock either was an equippable rock because the player was my character, or the rock wasn't equippable because the player wasn't. In DST, things get a little more complicated because I need my character to be able to equip the rocks while I need other characters to treat them normally.

So, I started with

    inst:ListenForEvent("onputininventory", toinventory)
    inst:ListenForEvent("ondropped", toground)

in the local function fn(), with 

local function toinventory(inst, owner)
	if owner ~= nil then
		if owner.prefab=="foo" then
			--Blah blah
			inst:AddComponent("equippable")
			--Blah blah blah
		end
	end
end

local function toground(inst)
	--Stuff here
end

in the item code as a whole. My toinventory function seems to work perfectly, as my character can pick up rocks and they have the right-click "equip" thing when you hover over them. Furthermore, other characters are able to interact with rocks normally. However, the moment I try to move the rocks when they're in my character's inventory, DST goes non-responsive and I have to shut it down.

Can anyone tell me what I'm doing wrong here?

Link to comment
Share on other sites

Well I don't know what happens in those blah blahs.

But wrong would be messing with equippable like that, here in DST. A lot can go wrong.

Removing the component back and forth, replicating and deleting the replica and the tags related (which may happen in such an order that the game crashes).

It would be nice if the equippable replica had a CanEquip method that relied on tags and/or netvars.

if GLOBAL.TheNet:GetIsServer() then
	local function _Add(inst)
		if inst.components.equippable == nil then
			inst:AddComponent("equippable")
		end
	end
	local function _Remove(inst)
		if inst.components.equippable ~= nil then
			inst:RemoveComponent("equippable")
		end
	end
	local function toinventory(inst, owner)
		if owner ~= nil then
			if owner.prefab == "foo" then
				_Add(inst)
			else
				inst:DoTaskInTime(0, _Remove)
			end
		end
	end
	local function toground(inst)
		inst:DoTaskInTime(0, _Remove)
	end
	AddPrefabPostInit("rocks", function(inst)
		inst:ListenForEvent("onputininventory", toinventory)
		inst:ListenForEvent("ondropped", toground)
	end)
end

I think this code should "just work". But I don't promise anything.

Link to comment
Share on other sites

10 hours ago, DarkXero said:

I think this code should "just work". But I don't promise anything.

After some tweaking and moving things around, I eventually got it to work, and it completely fixed the equippability issue. Unfortunately, now it crashes every time I try to throw a rock, but it's definitely a step in the right direction. Thanks!

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