This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TheOddByte's profile picture

Converting this into a table?

Started by TheOddByte, 11 February 2014 - 05:11 PM
TheOddByte #1
Posted 11 February 2014 - 06:11 PM
Hello everyone, I'm currently developing a game I'm kinda tired right now and can't quite figure this out..
How would I convert this into a table?

{
	["fX"] = 94,
	["sY"] = 1,
	["sX"] = 44,
	["background_color"] = 1,
	["fY"] = 19,
	["blueprint"] = {
		[1] = "	 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC																																																																																																						  ",
		[2] = "	 C																			 CCCCCCC																																																																																																						  ",
		[3] = "	 C																			 CCCCCCC																																																																																																						  ",
		[4] = "	 C						  ^												  CCCCCCC																																																																																																						  ",
		[5] = "	 C						  C										   CC																																																																																																									  ",
		[6] = "	 C															   CC	 CCCCC																																																																																																								   ",
		[7] = "	 C  @					   C									  CCC  CCCCC^																																																																																																								  ",
		[8] = "	 C  C				C	  C							   _  ^^	   CCCCCCC																																																																																																								 ",
		[9] = "	 C  C C			C C <CCCCCCCCC> CC  CC  CC		  CC  C4  CC	   CCCCCCCCCC																																																																																																							  ",
		[10] = "	 C  C C C		C C C   C <C> C   CC^^CC^^CC		  CC^^CC^^CC	   CCCCCCCCCC^																																																																																																							 ",
		[11] = "	 C  C^C^C CCCCCC C^C^C   C  v  C   CCCCCCCCCC^	 _   CCCCCCCCCC	   CCCCCCCCCCCCCC																																																																																																						  ",
		[12] = "	 CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA																																																																																																						  ",
		[13] = "	 CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA																																																																																																						  ",
		[14] = "	 CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA																																																																																																						  ",
		[15] = "	 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC																																																																																																						  ",
		[16] = "																																																																																																																													",
		[17] = "																																																																																																																													",
		[18] = "																																																																																																																													",
		[19] = "																																																																																																																													",
}
NOTE: This is the map itself, Let's say it's saved into the file asdf.level

And I've tried todo this

local map = {}

local file = fs.open( "asdf.level", "r" )
map = textutils.unserialize( file.readAll() )
file.close()

print( type( map ) ) --# I've done this to check what type it is, It always prints nil! D: And the file exists and everything..

So please help me with this (probably super simple ) problem :P/>
Rybec #2
Posted 11 February 2014 - 06:34 PM
That is already a table, can you be more specific as to what you mean?
TheOddByte #3
Posted 11 February 2014 - 06:57 PM
Well in the first code tag you can see the map and it's saved to asdf.level
Everytime I try to load it with the code in the second code tag it shows that map is nil.
So how would I load the code in the first code tag as a table? Since it doesn't seem to work with textutils.unserialize.
surferpup #4
Posted 11 February 2014 - 07:12 PM
Where are the closing quotes? It should serialize fine if you add them.
Bomb Bloke #5
Posted 11 February 2014 - 07:58 PM
Well in the first code tag you can see the map and it's saved to asdf.level
Everytime I try to load it with the code in the second code tag it shows that map is nil.
So how would I load the code in the first code tag as a table? Since it doesn't seem to work with textutils.unserialize.
What if you just directly load the file contents into a variable, without trying to unserialise it? Does that give you nil, or a string?

Where are the closing quotes?
They all appear to be present. Some horizontal scrolling is required to find them, though.

To my eyes the only thing missing is a } at the end, so I suppose that's worth adding.
wieselkatze #6
Posted 12 February 2014 - 06:53 AM
Why don't you put your actual level inside that file? So for example

XXX
X
XX

Then just:


f = io.open("asdf.level", "r")
level = {}
  for i in f:lines() do
  level[#level+1] = i
  end
f:close()

This would save you the textutils.serialize and unserialize stuff.
TheOddByte #7
Posted 12 February 2014 - 08:58 AM
@Bomb Bloke
Well it prints that it's a string, I added the missing '}'

@wieselkatze
Hmm.. I could do that, But then I would have to save the map variables into another file or something.
wieselkatze #8
Posted 12 February 2014 - 09:19 AM
You maybe could do sth like

variable1:123;
variable2:234;
blueprint:XXX
X
XX;

and just let your program iterate over the lines and insert in the named table before the ':' and insert until you hit a ';'.
So e.g. with string.match. Then you could just say, if there is a ';' on the same line, it's just a string, else it'll create a table and insert in this table until it hits a ';'.
I'm still thinking about making this as a config program with recursive functions calls to let the program insert tables in another table and so on.
Hope this helps