Page 1 of 1

onEquipItem crash

Posted: Fri Mar 15, 2013 2:17 am
by Duncan_B
I'm having trouble creating a particular effect upon equipping an item. If I use the onEquipItem hook (as implemented/commented-out below), the game crashes without an error message upon equipping it in-hand. I have tried to create a work-around with a script entity (whose code I found and borrowed from the site... I don't remember who it's from, but will make sure they are properly credited if I do end up using it). The script entity work-around functions all of the time in the Dungeon Editor, but is prone to causing crashes (with an error log, below) upon holding the item in-hand and clicking a healing crystal when run as a Custom Dungeon.

My code/error messages are as follows:

onEquipItem hook [ideally I could find what's wrong with this and just use it]

Code: Select all

onEquipItem = function()
	script_entity_7.itemCheck()
	return true
	end
--onEquipItem = function(champion, slot)
--	for champ = 1,4 do
--	for slot = 7,8 do
--		party:getChampion(ch):setCondition("poison", 30)
--		return true
--	end
--	end
--end
--this commented-out onEquipItem section makes the Dungeon Editor crash when the item is in-hand (with no subsequent error message)
script_entity_7.itemCheck() [the work-around]

Code: Select all

function itemCheck()
-- supposing the item causing the crash is actually named "item"
for champ = 1,4 do
for slot = 7,8 do
x = party:getChampion(champ):getItem(slot)
      	if x ~= nil then
      	if x.name == "item" then
		party:getChampion(champ):setCondition("poison", 30)
	  end
	  end
	end
	end
end
The error read-out

Code: Select all

script_entity_7: cannot serialize table 'x' with metatable stack traceback:
	[C]: in function 'error'
	[string "ScriptEntity.lua"]: in function 'saveValue'
	[string "ScriptEntity.lua"]: in function 'saveState'
	[string "GameMode.lua"]: in function 'saveGame'
	[string "GameMode.lua"]: in function 'autoSave'
	[string "Crystal.lua"]: in function 'onClick'
	[string "GameMode.lua"]: in function 'mousePressed'
	[string "GameMode.lua"]: in function 'update'
	[string "Grimrock.lua"]: in function 'display'
	[string "Grimrock.lua"]: in main chunk
This error message does encourage me to submit the error log to feedback@almosthumangames.com, which I've done, but I thought I might get a faster response asking on the forum. As usual, any and all help appreciated. Thank you.

Re: onEquipItem crash

Posted: Fri Mar 15, 2013 2:56 am
by Marble Mouth
Hi Duncan_B
Duncan_B wrote:

Code: Select all

onEquipItem = function()
	script_entity_7.itemCheck()
	return true
	end
--onEquipItem = function(champion, slot)
--	for champ = 1,4 do
--	for slot = 7,8 do
--		party:getChampion(ch):setCondition("poison", 30)
--		return true
--	end
--	end
--end
--this commented-out onEquipItem section makes the Dungeon Editor crash when the item is in-hand (with no subsequent error message)
What is the variable ch ? Was it initialized somewhere else? If not, I think you're calling party:getChampion(nil):setCondition("poison", 30) which is likely the cause of your crash. Also, lua supports this syntax for multi-line comments:

Code: Select all

--[[ This is the first line of a multi-line comment
onEquipItem = function(champion, slot)
	for champ = 1,4 do
	for slot = 7,8 do
		party:getChampion(ch):setCondition("poison", 30)
		return true
	end
	end
end
--]] --This is the last line of a multi-line comment
--this commented-out onEquipItem section makes the Dungeon Editor crash when the item is in-hand (with no subsequent error message)
The really nice thing about this multi-line comment syntax is that by changing a single character, you can "uncomment" the code:

Code: Select all

---[[ This is now a single line comment
onEquipItem = function(champion, slot)
	for champ = 1,4 do
	for slot = 7,8 do
		party:getChampion(ch):setCondition("poison", 30)
		return true
	end
	end
end
--]] This is now a single line comment
--this commented-out onEquipItem section makes the Dungeon Editor crash when the item is in-hand (with no subsequent error message)
The error you're getting is because when you save (including autosave), Grimrock has to "serialize" every global variable, including x . But x is an item from the dungeon, and those are known to cause problems. You never saw this error in the editor, because the editor never autosaves. Fortunately, it's easy to make a variable non-global (i.e. local) and in this case it shouldn't interfere with the desired functionality, since x does not need to be preserved after itemCheck is done:

Code: Select all

function itemCheck()
-- supposing the item causing the crash is actually named "item"
for champ = 1,4 do
for slot = 7,8 do
local x = party:getChampion(champ):getItem(slot)
         if x ~= nil then
         if x.name == "item" then
      party:getChampion(champ):setCondition("poison", 30)
     end
     end
   end
   end
end
Edit: It would be nice if I could get the syntax correct when I'm explaining how the syntax works.

Re: onEquipItem crash

Posted: Fri Mar 15, 2013 6:28 am
by Duncan_B
Excellent, thank you! The variable "ch" was... what we'll call my punishment for making copy-paste my main mode of coding in lua. :oops: Regarding multi-line comments, I had only added in the one comment for the sake of this forum post, but I appreciate the new knowledge.

If changing the variable to "champ" in the onEquipItem bit doesn't work, I'll see if making "x" local will. My hope is that the first will suss it out so I won't have to do anything with the second, but I thank you again for providing both.

I'll follow up on this with news of my success or (despair the thought!) failure in some future minute.

Re: onEquipItem crash

Posted: Fri Mar 15, 2013 10:21 am
by Duncan_B
Making 'x' local did the trick, so it seems. I'll bump this thread if any problems re-appear, but I don't suspect they will-- not with this, anyway!