Page 2 of 3

Re: Torch dampening script

Posted: Mon Mar 18, 2013 12:55 pm
by Komag
You'll want to check out this more closely:
http://www.grimrock.net/modding/save-ga ... variables/

basically, any variable you save must be a number, a string, a true/false, or a table (with those things), but cannot be an object or an entity or text that is not a string, etc. You can use those "bad" variables within functions as local variables that will never be saved, but anything non-local must be one of the "safe" types.

Re: Torch dampening script

Posted: Mon Mar 18, 2013 12:58 pm
by Damonya
I know that, but I am not an expert in script, I know use them, but not correct them if it's too complicated.

This means that the script can't work anyway ? I don't know what to change in.

Perhaps remove it from here, in this case : viewtopic.php?f=14&t=3099 :|

Re: Torch dampening script

Posted: Mon Mar 18, 2013 3:22 pm
by Damonya
I tried instead of :
party:getChampion(i):getItem(j)
party:getChampion(i)

putting:

local x = party:getChampion(i):getItem(j)
local y = party:getChampion(i)

but always a CTD
SpoilerShow

Code: Select all

 TF = {}
TID = {}
function torchout()
   for i=1,4 do
      for j=1,31 do
       local x = party:getChampion(i):getItem(j)
		if x ~= nil then
            local istorch = x
            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
	local x = party:getChampion(i):getItem(j)
         if x ~= nil then
            local istorch = x
			local y = party:getChampion(i)
            y:removeItem(j)
            y:insertItem(j, istorch)
         end
      end
   end
end


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

Re: Torch dampening script

Posted: Mon Mar 18, 2013 3:40 pm
by Komag
I can't really get into this now, but I've gone ahead and cleaned up the script a little:

Code: Select all

 TF = {}
TID = {}
function torchout()
  for i=1,4 do
    for j=1,31 do
      local t = party:getChampion(i):getItem(j)
      if t then
         if t.name == "torch" and t:getFuel() > 0 then
            table.insert(TID,t)
            local f = t:getFuel()
            table.insert(TF,f)
            t:setFuel(0)     
         end
      end     
    end
  end
  local mt = getMouseItem()
  if mt and mt.name == "torch" then
     table.insert(TID,mt)
     local f = mt:getFuel()
     table.insert(TF,f)
     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
    local x = party:getChampion(i):getItem(j)
      if x ~= nil then
         local istorch = x
         local y = party:getChampion(i)
         y:removeItem(j)
         y:insertItem(j, istorch)
      end
    end
  end
end

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
It still has the same problem, saving disallowed variables would crash, but it could be useful for a very short term effect, like a couple seconds of darkness or something, where the player is extremely unlikely to save (even then it could be risky, some player might just hit quicksave at that moment.) I'm really not sure how to save the exact torch fuel of every torch - you would have to save the inventory position, also check inside all the containers (which this script doesn't do), and even then players could rearrange their inventory before you "restore" the torches, or toss them on the ground, etc. Also, the original spawned timer is sort of worthless, as it's only on the starting level, and if the player descends a couple levels the timing will be very infrequent depending on the size of the dungeon. Anyway, it's quite complex.

Re: Torch dampening script

Posted: Mon Mar 18, 2013 3:49 pm
by Damonya
Ok thanks, that is too complex for me, I give up then. It's pity, I liked the idea of the script.

PS : always CTD with your correction.

Re: Torch dampening script

Posted: Tue Mar 19, 2013 11:07 am
by Damonya
All the same, I wonder a question.

As noted in post-it in "Useful scripts repository" Nobody has ever used this script in his mod ? Because everyone has the same bug as me (perhaps unknowingly), or you found a solution. :?

Well anyway I removed it from my mod.

Re: Torch dampening script

Posted: Tue Mar 19, 2013 12:07 pm
by Komag
sometimes the script repository is more for learning or ideas than direct use, so it's still reasonable to leave it for now ;)

Re: Torch dampening script

Posted: Tue Mar 19, 2013 1:50 pm
by Alcator
The most likely problem is that you are putting IDs (= objects) into the table, which you want to save later. A table to be saved may only contain numbers, strings or bools, and object is neither of these.

what might be worth trying is

Code: Select all

tmpString = tostring(torch.id)
tostring converts what is given to it into a string, which would then be possible to save.

then, you'd find the original object using findEntity(tmpString)

Sorry, I'm "programming" without the game actually launched, so this may be complete nonsense.

Re: Torch dampening script

Posted: Tue Mar 19, 2013 2:01 pm
by Komag
I haven't tried that, but Grimrock's auto-assigned IDs are weird numbers that may not work for that, but worth a try maybe

Re: Torch dampening script

Posted: Tue Mar 19, 2013 2:18 pm
by Alcator
Komag wrote:I haven't tried that, but Grimrock's auto-assigned IDs are weird numbers that may not work for that, but worth a try maybe
The auto-assigned IDs are always in the format xxxxxxxxxx_[NUMBER], where xxxxxxx is the object_name from the definition script
(eg. "torch_1" for first torch, "torch_2" for second torch). But as I said, I didn't test it yet, so it may be nonsense.