Need help fixing a crash

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
LocalFire
Posts: 261
Joined: Thu Feb 21, 2013 1:16 am
Location: Auckland, New Zealand

Need help fixing a crash

Post by LocalFire »

So I have three scripts here that all work fine on there own, the problem I have is if the three are all used at some point it causes the game to crash when trying to save, otherwise everything works fine

Code: Select all

function LvlComp()
   if (party:getChampion(2):getEnabled()) then
   
      local C1 = OrdinalScript.getChampionWithOrdinal(1);
      local C2 = OrdinalScript.getChampionWithOrdinal(2);
      
      while (C2:getLevel() < C1:getLevel()) do
         C2:levelUp();
      end
   end
end

function Lvl3()
 if (party:getChampion(3):getEnabled()) then
   
      local C1 = OrdinalScript.getChampionWithOrdinal(1);
      local C2 = OrdinalScript.getChampionWithOrdinal(3);
      
      while (C2:getLevel() < C1:getLevel()) do
         C2:levelUp();
      end
   end
end

function lvl4()
 if (party:getChampion(4):getEnabled()) then
   
      local C1 = OrdinalScript.getChampionWithOrdinal(1);
      local C2 = OrdinalScript.getChampionWithOrdinal(4);
      
      while (C2:getLevel() < C1:getLevel()) do
         C2:levelUp();
      end
   end
end
This first script just equalizes the levels of the party to the first party member, since the other members are found later in the dungeon

Code: Select all

function getChampionWithOrdinal(ordinal)
  for i=1,4 do
    if ( party:getChampion(i):getOrdinal() == ordinal ) 
 then     return party:getChampion(i);
    end
  end

  return nil;
end
This script is called on by the first script, its in an SE called OrdinalScript.

Code: Select all

function VainCheck(item)
   
   for champ = 1,4 do
   for slot = 1,8 do
      vainz = party:getChampion(champ):getItem(slot)
      if vainz ~= nil then
      if vainz.name == item then
        dm_irondoor_1:open()
timer_v:destroy()
      end
        end   
   end    
   end   
end



function checkShirt()
   VainCheck("Dandy_shirt")

end

function checkPants()
   VainCheck("Dandy_pants")

end
This last one checks if the party has a certain item on and opens a door, all three work fine, but if both the first and third script are run it causes a crash when saving "unable to serialize vainz against metatable"
Any ideas on how to fix this?
My Dungeons
Paths of the Earth: viewtopic.php?f=14&t=5136
Beneath the Sands: viewtopic.php?f=14&t=5216
Videos: viewtopic.php?f=11&t=5443
User avatar
msyblade
Posts: 792
Joined: Fri Oct 12, 2012 4:40 am
Location: New Mexico, USA
Contact:

Re: Need help fixing a crash

Post by msyblade »

A coder I am not, but I'm sure you're in a hurry for ideas to try, so I'll throw one out there (likely completely off base/wrong though.

I think I recall something to the effect of the variable (Vainz) being inside of a function, makes it a "local" variable, where it will not serialize a "Global" table. I'm not sure if I'm stating it correctly, or even if this is true. Anyway, you may try taking "Vainz" out of the function, and making it a global. Or perhaps combining the scripts so they aren't each "local", and trying to reference each other globally. Someone knowledgeable will likely come along shortly and see the actual problem. (Rather than my blind guessing)
Currently conspiring with many modders on the "Legends of the Northern Realms"project.

"You have been captured by a psychopathic diety who needs a new plaything to torture."
Hotel Hades
User avatar
DesperateGames
Posts: 90
Joined: Sun Oct 06, 2013 1:54 pm

Re: Need help fixing a crash

Post by DesperateGames »

msyblade is guessing in the right direction: The problem is that when creating the savegame, the engine tries to store the variable "Vainz". At this time the variable holds an object (the item) which is not allowed for global variables / savegames. This will always result in a crash when trying to save a game. In this case it seems you don't really require the variable to be stored in a savegame, so the solution would be to declare Vainz as local:

Code: Select all

local  vainz = party:getChampion(champ):getItem(slot)
In case you need an object to be saved in a savegame for a script, you can save the object id instead and fetch the object via findEntity() when you need it in the script.

This official article explains it further:

http://www.grimrock.net/modding/save-ga ... variables/
User avatar
LocalFire
Posts: 261
Joined: Thu Feb 21, 2013 1:16 am
Location: Auckland, New Zealand

Re: Need help fixing a crash

Post by LocalFire »

:) It was just adding local, I noticed it after I posted but was all nah can't be cause it only crashes if the other script is run first, I had assumed that it had to be some kind of interaction but as if turns out was something simple, Thanks for the help guys.
My Dungeons
Paths of the Earth: viewtopic.php?f=14&t=5136
Beneath the Sands: viewtopic.php?f=14&t=5216
Videos: viewtopic.php?f=11&t=5443
Post Reply