Storing functions in tables?

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

Storing functions in tables?

Post by Lmaoboat »

I'm trying to set up a script where depending on a situation, a certain group of functions in a table well be used, but I can't figure out out make them do anything. Basically I have a table of translate functions their respect locations, and 10 more tables that decide which functions to move depending on the number.

Code: Select all

pixels = {"lcdA1:translate(-1,2.5,1.3)",
"lcdA2:translate(-0.5,2.5,1.3)",
"lcdA3:translate(0,2.5,1.3)",
"lcdA4:translate(0.5,2.5,1.3)",
"lcdA5:translate(1,2.5,1.3)",
"lcdA6:translate(-1,2,1.3)",
"lcdA7:translate(-0.5,2,1.3)",	
"lcdA8:translate(0,2,1.3)",
"lcdA9:translate(0.5,2,1.3)",	
"lcdA10:translate(1,2,1.3)",
"lcdA11:translate(-1,1.5,1.3)",
"lcdA12:translate(-0.5,1.5,1.3)",
"lcdA13:translate(0,1.5,1.3)",
"lcdA14:translate(0.5,1.5,1.3)",
"lcdA15:translate(1,1.5,1.3)",
"lcdA16:translate(-1,1,1.3)",
"lcdA17:translate(-0.5,1,1.3)",	
"lcdA18:translate(0,1,1.3)",
"lcdA19:translate(0.5,1,1.3)",	
"lcdA20:translate(1,1,1.3)",
"lcdA21:translate(-1,0.5,1.3)",
"lcdA22:translate(-0.5,0.5,1.3)",
"lcdA23:translate(0,0.5,1.3)",
"lcdA24:translate(0.5,0.5,1.3)",
"lcdA25:translate(1,0.5,1.3)"}


n0 = {1,2,3,4,5,6,10,11,15,16,20,21,22,23,24,25}
n1 = {1,2,3,8,13,18,21,22,23,24}
n2 = {1,2,3,4,5,10,11,12,13,14,15,16,21,22,23,24,25}
n3 = {1,2,3,4,5,10,11,12,13,14,15,20,21,22,23,24,25}
n4 = {1,5,6,10,11,12,13,14,15,20,25}
n5 = {1,2,3,4,5,6,11,12,13,14,15,20,21,22,23,24,25}
n6 = {1,2,3,4,5,6,11,12,13,14,15,20,21,22,23,24,25}
n7 = {1,2,3,4,5,6,10,15,20,25}
n8 = {1,2,3,4,5,6,10,11,12,13,14,15,16,20,21,22,23,24,25}
n9 = {1,2,3,4,5,6,10,11,12,13,14,15,20,21,22,23,24,25}


function makelight()
	for i=1,25 do 
		local check = findEntity("lcdA"..i)
		if check == null then	
			spawn("fx", self.level, self.x, self.y, 3, "lcdA"..i)
			local lcd = findEntity("lcdA"..i)
			lcd:setLight(1,0,0,500,0.3, 360000, false)
		end
	end
end

function removelight()
		for i=1,25 do
			local check = findEntity("lcdA"..i)
			if check ~= null then
				local lcd = findEntity("lcdA"..i)
				lcd:setLight(1,0,0,500,0.1, 1, false)
			end
		end	
end


function numlist(x)
	if x == 0 then
		for i=1,25 do
			local pix = n0[i]
			if pix ~= nil then
				local move = pixels[pix]
                                move
			end	
		end
	end
	if x == 1 then
		for i=1,25 do
			local pix = n1[i]
			if pix ~= nil then
			local move = pixels[pix]
							
			end
		end
	end	
	if x == 2 then
		for i=1,25 do
			local pix = n2[i]
			if pix ~= nil then
				local move = pixels[pix]

			end
		end
	end
		if x == 3 then
		for i=1,25 do
			local pix = n3[i]
			if pix ~= nil then
				local move = pixels[pix]
			end
		end
	end	
	if x == 4 then
		for i=1,25 do
			local pix = n4[i]
			if pix ~= nil then
				local move = pixels[pix]
			end
		end
	end	
	if x == 5 then
		for i=1,25 do
			local pix = n5[i]
			if pix ~= nil then
				local move = pixels[pix]
			end
		end
	end
	if x == 6 then
		for i=1,25 do
			local pix = n6[i]
			if pix ~= nil then
				local move = pixels[pix]
			end
		end
	end	
	if x == 7 then
		for i=1,25 do
			local pix = n7[i]
			if pix ~= nil then
				local move = pixels[pix]
			end
		end
	end	
	if x == 8 then
		for i=1,25 do
			local pix = n8[i]
			if pix ~= nil then
				local move = pixels[pix]
			end
		end
	end
	if x == 9 then
		for i=1,25 do
			local pix = n9[i]
			if pix ~= nil then
				local move = pixels[pix]
			end
		end
	end
end

makelight()
The local variable "move" is the one that contains the functions, but that's as far as I can get.
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Storing functions in tables?

Post by petri »

How about storing the translation values in the table instead?

pixels = {
-1,2.5,1.3,
-0.5,2.5,1,3,
...
}
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Storing functions in tables?

Post by Lmaoboat »

Almost got it I think

Code: Select all


pixels = {"-1,2.5,1.3",
"-0.5,2.5,1.3",
"0,2.5,1.3",
"0.5,2.5,1.3",
"1,2.5,1.3",
"-1,2,1.3",
"-0.5,2,1.3",	
"0,2,1.3",
"0.5,2,1.3",	
"1,2,1.3",
"-1,1.5,1.3",
"-0.5,1.5,1.3",
"0,1.5,1.3",
"0.5,1.5,1.3",
"1,1.5,1.3",
"-1,1,1.3",
"-0.5,1,1.3",	
"0,1,1.3",
"0.5,1,1.3",	
"1,1,1.3",
"-1,0.5,1.3",
"-0.5,0.5,1.3",
"0,0.5,1.3",
"0.5,0.5,1.3",
"1,0.5,1.3"}


function numlist(x)
	if x == 0 then
		for i=1,25 do
			local pix = n0[i]
			if pix ~= nil then
				local lcd = findEntity("lcdA"..i)
				local move = pixels[pix]
				print(move)
				lcd:translate(move)
				
			end	
		end
	end
But I'm getting an expecting number, bot string error. Is that because I'm using all three numbers together? should I make a one for each axis?
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Storing functions in tables?

Post by petri »

I would just store them as numbers and index by pixels[i*3+1], pixels[i*3+2], pixels[i*3+3] to obtain x,y and z coordinate
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Storing functions in tables?

Post by Lmaoboat »

pixelsx = {-1,-0.5,0,0.5,1,-1,-0.5,0,0.5,1,-1,-0.5,0,0.5,1,-1,-0.5,0,0.5,1,1,-1,-0.5,0,0.5,1,}
pixelsy = {2.5,2.5,2.5,2.5,2.5,2,2,2,2,2,1.5,1.5,1.5,1.5,1.5,1,1,1,1,1,0.5,0.5,0.5,0.5,0.5}


function numlist(x)
if x == 0 then
for i=1,25 do
local pix = n0
if pix ~= nil then
local lcd = findEntity("lcdA"..i)
local movex = pixelsx[pix]
local movey = pixelsy[pix]
print(move)
lcd:translate(movex,movey,1.3)

end
end
end

Seems to work so far. Thanks for the help, I didn't want to have had to spend hours on something that couldn't work.
Lmaoboat
Posts: 359
Joined: Wed Apr 11, 2012 8:55 pm

Re: Storing functions in tables?

Post by Lmaoboat »

Only problem left I think is that I can't remove the lights without using a timer, because even when I set the duration of the light to 0, it still tries and create new ones at the same time.
Post Reply