Creating the ASCII Parser

To parse an ASCII string into a table that can be used to generate the 3D map, you will need to create a parser that will remove invalid characters from the string, and turn it into a table that is the correct width and height.

Create ASCII Parser Script

A script will be created that will be responsible for parsing the ASCII string into a table that will contain the map. This will allow for easy access to the rows and columns of the map to spawn tiles later on.

  1. Create a new script called ASCIIParser.
  2. Place the ASCIIParser script into the Server Scripts folder in the Hierarchy.

Create Lua Code

Open up the ASCIIParser script. You will need to create a few variables and functions to help generate the map table from the ASCII string.

Create Variables

To be able to parse the ASCII string into a table, you will need to define a map width and map height. The map width would be how many columns there should be, and the map height would be how many rows there should be. To begin with, a map width of 6 and map height of 5 will be created, but feel free to adjust this based on the size you want.

At the top of the ASCIIParser script, create the following variables:

local MAP_WIDTH = 6
local MAP_HEIGHT = 5
Code language: Lua (lua)

Create Clean Function

A function will be created that will go through the ASCII string and take out any characters that are not going to be supported for the map generation. This will give you plenty of characters to work with so that you can specify what the tile should be and do.

The characters supported will be:

  • Numbers from 0 to 9.
  • Lowercase letters from a to z.
  • Uppercase letters from A to Z.

Create a function called Clean. This function will take in a string parameter called map and iterate through all of the characters one by one. If any character does not match the regular expression string [0-9a-zA-Z] then the character will be removed.

function Clean(map)
	for c = 1, string.len(map) do
		local character = string.sub(map, c, c)
	
		if not string.match(character, "[0-9a-zA-Z]+") then
			map = string.gsub(map, character, "")
		end
	end

	return map
end
Code language: Lua (lua)

Create BuildMap Function

The BuildMap function will take in the map string, MAP_WIDTH, and MAP_HEIGHT. It will iterate based on the height and width variables to build up the table. To verify that the table has been built correctly, the map string is rebuilt and stored in the mapStr variable. If this matches the ASCII map string passed in, then the BuildMap function was a success.

Building the tableMap requires a little thought because the map is a string containing all the ASCII characters that describe the map. A little bit of math can be used to make sure the correct number of characters is added to each row in the table. Using string.sub we can retrieve a charater at a specific place in the map string and place it in the tableMap using the r and c values for row and column.

function BuildMap(map, MAP_WIDTH, MAP_HEIGHT)
	local tableMap = {}
	local mapStr = ""

	map = Clean(map)

	for r = 1, MAP_HEIGHT do
		table.insert(tableMap, {})
	
		for c = 1, MAP_WIDTH do
			local character = string.sub(map, ((r - 1) * MAP_WIDTH) + c, ((r - 1) * MAP_WIDTH) + c)
			
			tableMap[r][c] = character
			mapStr = mapStr .. character
		end

		mapStr = mapStr .. "\n"
	end

	return tableMap, mapStr
end
Code language: Lua (lua)

Test the ASCII Parser

To test the ASCII Parser, you can call the BuildMap function and pass in the ASCII string, width, and height. The BuildMap function will return a table and the ASCII string that is built from the input string so it can be verified it was done correctly. Play the game and see if the output in the Event Log matches the input.

local map, mapStr = BuildMap([[

	111111
	100001
	100001
	100001
	111111

]], MAP_WIDTH, MAP_HEIGHT)

print(mapStr)Code language: PHP (php)
Scroll to Top