Page 1 of 1

Destroy Torchholder with torch

Posted: Tue Mar 26, 2013 3:32 am
by Diarmuid
Hey there,

Asking if someone already worked on this:

I want to destroy a torchHolder.

If i just destroy the torchHolder and there's a torch inside, the torch just stands floating there.

So I need to make an entitiesAt search for torches (easy with if entity:getFuel() then...) and delete them also, but how to avoid deleting torches if the torch is on the floor instead of in the torchholder?

One way I though of doing this is:
1. check if torchHolder:hasTorch()
2. if yes, cycle through all torches in the tile
3. foreach, destroy it storing facing property, check if still torchHolder:hasTorch() and if not assume that was the one, else respawn that torch on the floor with same facing and try next one.

Another idea:
1. check if torchHolder:hasTorch()
2. get number of torches in the tile
3. destroy all torches, storing facing properties, then respawn torchesNumber - 1 on the ground with same facing.

What do you guys think? Or if anyone has already a working (better) solution...

Re: Destroy Torchholder with torch

Posted: Tue Mar 26, 2013 4:08 am
by Komag
I've teleported all items away, destroyed any remaining torch (in the torch holder), then teleported back items. Teleports are not completely instantaneous, so this only works well if the player cannot see it.

Re: Destroy Torchholder with torch

Posted: Tue Mar 26, 2013 4:56 am
by Diarmuid
Hah. Clever! But I think I need this to be more instant. Just thought that my Idea #2 wont work as i need to store/restore getFuel/setFuel, so I need to know the right torch. Will try method one and report back.

EDIT: Well, this seems to work allright:

Code: Select all

function destroyTorchHolder(torchHolderId)

	local torchHolder = findEntity(torchHolderId)

	if not torchHolder then 
		return false 
	end
		
	if torchHolder:hasTorch() then
	
		local torches = {}
		
		for entity in entitiesAt(torchHolder.level, torchHolder.x, torchHolder.y) do
			
			if entity.getFuel and entity.facing == torchHolder.facing then
				table.insert(torches, entity)
			end
			
		end
		
		for i = 1, #torches do
			
			local torchFuel = torches[i]:getFuel()
			torches[i]:destroy()
			
			if torchHolder:hasTorch() then
				torches[i] = spawn("torch", torchHolder.level, torchHolder.x, torchHolder.y, torchHolder.facing)
				torches[i]:setFuel(torchFuel)
			else
				torchHolder:destroy()
				return torchFuel
			end
		
		end
		
	else
	
		torchHolder:destroy()
		return nil
		
	end

end
The function returns the fuel of the destroyed torch if the torch holder had one, in case you'd want to recreate it later. It returns nil if the torch holder was empty.