Jump to content

How to read the contents of a chest in order?


Recommended Posts

 I'm looking to make a small server-side-only mod for fun that involves putting things in a chest, sort of like a puzzle, in a certain pattern. I'm new to DST modding and have only a tenuous grasp of Lua, so I still need to learn. Though it seems simple, I'm not sure how I'd go about this, and I couldn't find a relevant example in the API sample mod, so my question is this:

How can I read the contents of a chest, in order (order is important for pattern-making), into an array or list or whatever data structure the game likes to use for such things, whenever a player gets within a certain distance of it while holding a certain item? I know it's a complex question, but I feel like all these things need to sort of tie together. Basically I want the player to be able to put a certain "combination" of things into a chest, then equip an item while standing within a certain distance to cause the mod to detect what's in the chest, in what order, and possibly perform an action if the combination is correct.

I thought maybe looking at the chester script that lets him change phases when he's full of certain items might help, but I'm afraid I don't know where to find that script or if it would apply since it only uses a single ingredient instead of patterns. Any and all help is greatly appreciated, as I am very much a noob. Thank you in advance for your help.

 

EDIT: After some searching I found in containers.lua what appears to be the code used to decide which items can go in which containers, such as iceboxes that only allow spoilables (this one's for salt box):

function params.saltbox.itemtestfn(container, item, slot)
	return ((item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled"))
		and item:HasTag("cookable")
		and not item:HasTag("deployable")
		and not item:HasTag("smallcreature")
		and item.replica.health == nil)
		or item:HasTag("saltbox_valid")
end

Is there a way I can take this code and modify it to detect specific patterns based on the exact slots that stuff is in? I just can't seem to make the bridge from this to what I want.

 

EDIT 2: I thought maybe the bunnyman brain script would help me learn how to trigger events based on player proximity with certain items in-inventory, then apply that to my scenario, but I can't decipher it.

 

EDIT 3: Of course once I finally give up and ask for help, I go back into the files and find all kinds of useful stuff. I found this code in chester.lua used to transform him:

for i = 1, container:GetNumSlots() do
        local item = container:GetItemInSlot(i)
        if item == nil then
            return false, false
        end

        canShadow = canShadow and item.prefab == "nightmarefuel"
        canSnow = canSnow and item.prefab == "bluegem"

        if not (canShadow or canSnow) then
            return false, false
        end
    end

So now that I have the code to iterate over each chest item and do stuff if it detects the correct pattern, I need to know how to put it in modmain such that this function is called once or twice per second on every chest in the entire server, and then I need to add a check for players in proximity holding a certain item.

 

EDIT 4: This other mod topic (

) shows how to create a timed event with DoPeriodicTask, however I don't know how to use that function from the modmain script, and I want to use it from there so I'm not changing any objects, which might introduce the requirement for clients to download the mod, and I want this server-side only so that vanilla clients can join and it doesn't show up modded in the server list.

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