Page 2 of 2

Re: [Help] Remove torch from holder script?

Posted: Thu Jan 16, 2014 6:48 am
by chaoscommencer
Try this on for size:

Code: Select all

function extinguishWallTorches(torchHolders)
	for i = 1, #torchHolders do
		if torchHolders[i].name == "torch_holder" then
			local id, level, x, y, facing = torchHolders[i].id, torchHolders[i].level, torchHolders[i].x, torchHolders[i].y, torchHolders[i].facing
			print(id, x, y, facing)
			torchHolders[i]:destroy()
			spawn("torch_holder", level, x, y, facing, id)
		end
	end
end
Pass a table of torch holders (by id) to the function and it should destroy then recreate the torch holders in the same locations with the same ids as they originally had, effectively extinguishing the wall torches without destroying them or eliminating their fuel. This will help if you have other scripts which might reference a given torch holder by id.

Example usage:

Code: Select all

function pressurePlateTriggered()
	local torchHolders = {torch_holder_1, torch_holder_2, torch_holder_3,}
	script_entity_1.extinguishWallTorches(torchHolders)
end
To truly surprise a player and throw them into complete darkness, you can use the extinguishWallTorches function above in conjunction with the douse torches function by Lmaoboat found here:
viewtopic.php?p=37513#p37513

Hope this helps!

Re: [Help] Remove torch from holder script?

Posted: Fri Jan 17, 2014 6:56 pm
by AdrTru
Hi,
I create script for managing torches in player inventory, But I have problem with autonatic detection finding items where it is container.
Becouse this torches may be inside containes (normaly sack, wooden crate, motar but in some mods may be creted another).

Known somebody how detect if item is it container?
(All of my ideas ended by crash :) and I thing that pcall() dont worked.

Re: [Help] Remove torch from holder script?

Posted: Fri Jan 17, 2014 7:02 pm
by DesperateGames
Hi AdrTru,

the only way I know to do this is to check for the item name. In Vanilla Grimrock there are only 3 container types:

Code: Select all

if item.name == "sack" or item.name == "mortar" or item.name == "wooden_box" then
This should work fine as long as you don't have any additional container type items defined in your mod.

Re: [Help] Remove torch from holder script?

Posted: Fri Jan 17, 2014 9:14 pm
by JohnWordsworth
I think you can call item:containedItems() on any item in the inventory (even if it's not a container). If it doesn't contain any items, this will return nil? If this isn't true, I'm more confident that you can call 'getItem(slot)' on any item and it will return nil or an item.

Re: [Help] Remove torch from holder script?

Posted: Sat Jan 18, 2014 12:11 am
by AdrTru
Yes, thank you this worked

Code: Select all

function TestContained(container)
  local i
  for i in container:containedItems() do 
    return true
  end
  return false
end
Full working code for destroy all torches from party inventory

Code: Select all

function DestroyTorches()
local count = itemDestroy("torch")
  hudPrint("You feel touch of dark shadow")
  if count > 0 then 
    hudPrint("All "..count.." of your torches is missed.")
  end
end

function TestContainer(container)
local count = 0
local i
for i in container:containedItems() do 
  count = count + 1
end
return count
end

function itemDestroy(thisItem)
local thisPlayer,itemSlot,currItem,Champion
local count = 0
  for thisPlayer=1,4 do
    Champion = party:getChampion(thisPlayer)
    for itemSlot=1,31 do
      if Champion:getItem(itemSlot) ~= nil then 
        currItem = Champion:getItem(itemSlot)
        if currItem.name == thisItem then
          Champion:removeItem(itemSlot)
          count = count + 1
        else
          count = count + TestContained(thisItem,currItem)
        end
      end 
    end
  end
  currItem = getMouseItem()
  if currItem ~= nil and currItem.name == thisItem then
    count = count + 1
    setMouseItem(nil)
  end
  return count
end

function TestContained(thisItem,container)
local count,p,slot = 0,0,0
local citem,i,c
  c = TestContainer(container)
  while ((p<c) or (slot>31)) do
    slot = slot + 1
    citem = container:getItem(slot)
    if citem ~= nil then
      p = p + 1
      if citem.name == thisItem then
        count = count + 1
        container:removeItem(slot)
      else
        count = count + TestContained(thisItem,citem) 
      end
    end
  end
  return count
end


Re: [Help] Remove torch from holder script?

Posted: Sat Jan 18, 2014 2:48 am
by Eleven Warrior
Hi yo

Now that is some impressive Coding AdrTru well done.. :)