Locking a container

Place to get help with not working mods / modding interface.
wizmut
Burner Inserter
Burner Inserter
Posts: 7
Joined: Fri Apr 26, 2019 3:46 am
Contact:

Locking a container

Post by wizmut »

I'm trying to make a container that can be locked and unlocked. A locked container would not let inserters interact with it (or anything really).

My 'canister' is copied from the car prototype. This gives it other qualities I need, such as mobility, disabled collision and being drawn under other objects. As I need canisters to move, I can't set them to inactive.

Right now my solution is to look for inserters that target a canister and set their target to nil. This is very slow, and doesn't even really work as the inserters will find a target again right away. I tried setting the inserters inactive (and reactivating the next tick), but this gave the same result, and also slow. So I would have to check if the canister was far enough away to turn the inserter back on again - even slower.

Code: Select all

function test_disable_can_inserters(surface, event)
	-- find all inserters of all kinds
	local all_entities = surface.find_entities()
	local inserters = {}
	for _, entity in ipairs(all_entities) do
		if string.find(entity.name, "nserter") then
			table.insert(inserters, entity)
		end
	end
	
	for _, inserter in pairs(inserters) do
		if inserter.drop_target then
			if inserter.drop_target.name == "canister" then
				inserter.drop_target = nil
			end
		end
		if inserter.pickup_target then
			if inserter.pickup_target.name == "canister" then
				inserter.pickup_target = nil
			end
		end
	end
end
I also tried setting the canister to active, teleporting it, and then deactivating it again, but it won't move like that. And letting it be active for any frame will let the inserter in. I know cargo wagons won't let inserters in while they're moving, but that prototype isn't suitable for a number of reasons.
Natha
Fast Inserter
Fast Inserter
Posts: 229
Joined: Sun Mar 15, 2015 1:48 pm
Contact:

Re: Locking a container

Post by Natha »

You could create a dummy chest, which is a simple entity that looks like a chest but doesnt do anything. Inventory is then stored in a storage variable as long it is locked.
Post Reply

Return to “Modding help”