Page 1 of 3

Torch dampening script

Posted: Thu Oct 11, 2012 6:57 am
by Lmaoboat
I'm working on script to find all the torches in a players inventory, store their fuel in a table for later, and extinguish them, but I seem to have it a road block early on.

Code: Select all

function torchout()
	for i=1,4 do
		for j=1,31 do
			if party:getChampion(i):getItem(j) ~= null then
				local istorch = party:getChampion(i):getItem(j)
				if istorch.name == "torch" then
					local tor = findEntity(istorch.id)
					print(tor)

					
				end
			end		
		end
	end
end

istorch.id itself seems to show the proper is of the torch when I do print(istorch.id), but the findEntity keeps coming back as nil. I've done similar things scripts, so I'm probably just missing something really obvious.

Re: Torch dampening script

Posted: Thu Oct 11, 2012 7:37 am
by cromcrom

Code: Select all

party:getChampion(i):getItem(j) ~= null

nil instead of null maybe ?

Re: Torch dampening script

Posted: Thu Oct 11, 2012 7:45 am
by Lmaoboat
cromcrom wrote:

Code: Select all

party:getChampion(i):getItem(j) ~= null

nil instead of null maybe ?
I was never sure which, but I had null working in several scripts. The problem is further down, anyway.

Re: Torch dampening script

Posted: Thu Oct 11, 2012 7:53 am
by Lmaoboat
It occurs to me that I probably should have been using nil the whole time, but whatever else I used still worked because it hadn't assigned a value to it, and this it was also nil.

Re: Torch dampening script

Posted: Thu Oct 11, 2012 9:02 pm
by petri
"istorch" variable already holds the item, no need to use findEntity to find it :)

Re: Torch dampening script

Posted: Thu Oct 11, 2012 9:11 pm
by Lmaoboat
Huh, seems to work. Could have sworn I had already tried that.

Re: Torch dampening script

Posted: Thu Oct 11, 2012 9:41 pm
by Lmaoboat
Excellent, I seem to have it working quicker than I expected. I needed to remove, and add back in held torches is they would relight, and I was worried readding the item would reset the fuel but it seems to work fine.

Code: Select all

TF = {}
TID = {}
function torchout()

	for i=1,4 do
      for j=1,31 do
         if party:getChampion(i):getItem(j) ~= nil then
            local istorch = party:getChampion(i):getItem(j)
            if istorch.name == "torch" then
				table.insert(TID,istorch)
				local fuel = istorch:getFuel()
				table.insert(TF,fuel)
				istorch:setFuel(0)
		            
            end
         end      
      end
   end
end


function torchrestore()
	for i=1,25 do
		local tor = TID[i]
		if tor ~= nil then
			tor:setFuel(TF[i])
		end
	end
	for i=1,4 do
		for j=7,8 do
			if party:getChampion(i):getItem(j) ~= nil then
				local istorch = party:getChampion(i):getItem(j)
				party:getChampion(i):removeItem(j)
				party:getChampion(i):insertItem(j, istorch)
			end
		end
	end
end

Re: Torch dampening script

Posted: Fri Oct 12, 2012 6:23 am
by Lmaoboat
Okay, I wanted to make it so players couldn't try throwing the torch across the darkness triggers to avoid having them go out. The original script that snuffs out inventroy and moue held torches, which I have named "darkness", works fine:

Code: Select all

TF = {}
TID = {}
function torchout()
	for i=1,4 do
      for j=1,31 do
         if party:getChampion(i):getItem(j) ~= nil then
            local istorch = party:getChampion(i):getItem(j)
            if istorch.name == "torch" and istorch:getFuel() ~= 0 then
				table.insert(TID,istorch)
				local fuel = istorch:getFuel()
				table.insert(TF,fuel)
				istorch:setFuel(0)
		            
            end
         end      
      end
   end
	local mtor = getMouseItem()
	if getMouseItem() ~= nil and mtor.name == "torch" then
		table.insert(TID,mtor)
		local fuel = mtor:getFuel()
		table.insert(TF,fuel)
		getMouseItem():setFuel(0)
		
	end
end


function torchrestore()
	print(TF[1])
	print(TID[1])
	for i=1,25 do
		local tor = TID[i]
		if tor ~= nil then
			tor:setFuel(TF[i])
		end
	end
	for i=1,4 do
		for j=7,8 do
			if party:getChampion(i):getItem(j) ~= nil then
				local istorch = party:getChampion(i):getItem(j)
				party:getChampion(i):removeItem(j)
				party:getChampion(i):insertItem(j, istorch)
			end
		end
	end
end

But the extra script I made to detect thrown/ground torches seems work until it's time to relight them:

Code: Select all

spawn("timer",self.level,self.x,self.y,0,self.id.."gcheck")
:setTimerInterval(0.1)
:addConnector("activate", self.id, "groundcheck")
:activate()


function groundcheck()
	for i in entitiesAt(self.level, self.x, self.y) do
		if i.name == "torch" and i.getFuel ~= 0 then
			local fuel = i:getFuel()
			table.insert(darkness.TID,i)
			table.insert(darkness.TF,fuel)			
			i:setFuel(0)
		end
	end
end

I'm pretty sure it's adding to the tables correctly, since the print functions in torchrestore() show the fuel and torch being added to the table, but torch won't relight like it does if it was put out the other two ways.

Re: Torch dampening script

Posted: Sat Oct 13, 2012 2:33 am
by Lmaoboat
I see what I did, not only was I using "." instead of ":", nut I forgot the "()".

Re: Torch dampening script

Posted: Mon Mar 18, 2013 12:48 pm
by Damonya
I like your script and it was emphasized in the "Useful scripts repository" topic here : viewtopic.php?p=37513#p37513

But it has a big problem. You c'ant save your game after, with a big CTD :

script_anti_light: cannot serialize table with metatable

I can't find what to change in this script. Someone would be able ?