Torch dampening script

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Torch dampening script

Post 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.
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Torch dampening script

Post by cromcrom »

Code: Select all

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

nil instead of null maybe ?
A trip of a thousand leagues starts with a step.
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Torch dampening script

Post 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.
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Torch dampening script

Post 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.
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Torch dampening script

Post by petri »

"istorch" variable already holds the item, no need to use findEntity to find it :)
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Torch dampening script

Post by Lmaoboat »

Huh, seems to work. Could have sworn I had already tried that.
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Torch dampening script

Post 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
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Torch dampening script

Post 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.
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Torch dampening script

Post by Lmaoboat »

I see what I did, not only was I using "." instead of ":", nut I forgot the "()".
User avatar
Damonya
Posts: 134
Joined: Thu Feb 28, 2013 1:16 pm
Location: France
Contact:

Re: Torch dampening script

Post 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 ?
Orwak - MOD - Beta version 1.0
Post Reply