cannot serialize table 'text' with metatable

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

cannot serialize table 'text' with metatable

Post by Jouki »

Hello guys, I wanted to look on my map but apparently there's something wrong in the map. I'd say I saved the map executable last time. When I tried to launch the map several errors appears with same text: "cannot serialize table 'text' with metatable". I have absolutely no idea what's wrong or how to solve this problem. for the information I've reinstall windows (to W10) and did backup of whole folder with map (nothing external in other folders).

it appears on these functions

Code: Select all

function saturation()
c = findEntity("forest_day_sky_5")
d = findEntity("cemetery_sky_1")
if GameMode.getTimeOfDay() > 1.01 and GameMode.getTimeOfDay() < 1.94 and c.sky:getTonemapSaturation() > 0.5 then
		c.sky:setTonemapSaturation(c.sky:getTonemapSaturation()-0.001)
		d.fogparticles:setOpacity(d.fogparticles:getOpacity()+0.002)
	else
		if (GameMode.getTimeOfDay() < 1.01 or GameMode.getTimeOfDay() > 1.94) and c.sky:getTonemapSaturation() < 0.75 then
			c.sky:setTonemapSaturation(c.sky:getTonemapSaturation()+0.001)
			d.fogparticles:setOpacity(d.fogparticles:getOpacity()-0.002)
		end
	end
end
or

Code: Select all

function setMineTextForest(c)
	text = c:getWorldPosition()
	c:setWorldPosition(text[1],text[2],text[3]-0.1,text[4])
end

Code: Select all

function setItemPosition(sender,a,b,c,d)
	local lever = sender:getWorldPosition()
	sender:setWorldPosition(lever[1]+a,lever[2]+b,lever[3]+c,lever[4]+d)
end
I barely recognize purpose of these functions cause I forgot a big part of LUA ^^'

can someone help me, pleease :(
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: cannot serialize table 'text' with metatable

Post by minmay »

This is your problem:

Code: Select all

   text = c:getWorldPosition()
GameObject:getWorldPosition() returns a vector, which can't be serialized. The solution is to stop trying to store unserializable things, such as vectors, in permanent variables. Either change it to a local variable so that it's temporary, or convert it to a plain table. Converting vectors and matrices to plain tables is really easy, incidentally:

Code: Select all

-- Convert vector
local vector = c:getWorldPosition()
local vectorTable = {vector[1],vector[2],vector[3],vector[4]}

-- Convert matrix
local matrix = c:getWorldRotation()
local matrixTable = {
  x = {matrix.x[1],matrix.x[2],matrix.x[3],matrix.x[4]},
  y = {matrix.y[1],matrix.y[2],matrix.y[3],matrix.y[4]},
  z = {matrix.z[1],matrix.z[2],matrix.z[3],matrix.z[4]},
  w = {matrix.w[1],matrix.w[2],matrix.w[3],matrix.w[4]},
}
For reference, serializable values are booleans, numbers, strings, functions*, and tables containing serializable values. But values with a metatable are never serializable. Since the scripting interface doesn't provide access to the setmetatable() function, the practical effect of this rule is that you can't serialize any of Grimrock's "objects" such as vectors, matrices, GameObjects, Components, Champions, etc.
Upvalues are also never serializable.

*If you are storing the same function in multiple places, read this so that you don't introduce bugs that can be really hard to track down.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

Re: cannot serialize table 'text' with metatable

Post by Jouki »

thanks alot I dunno why it starts doing that problem while it was fine year ago.. anyway thanks for help I was afraid for a sec. :D
Post Reply