Tiled to AGK: Step 1-Prepare the tilesets.

It has been a long time since I have done a proper blog post.  This one is the first part of a multi part series of importing Tiled maps to AGK.  I use AGK in Tier 2 as a game framework using C++.  This is the real draw to AGK for my use, as I have the simplicity of AGK with the power of C++.  However, this tutorial is written using an AGK Tier 1 snippet as there is no need for any added complexity.  I will eventually write a C++ variation that is more flexible, but none the less, let’s start part 1 of this tutorial.  It is important to note that this does not only work with Tiled, it can be used to load any tileset/atlas image easily, but Tiled is my motivation for making it.



Code snippet for Tiled tilesets to AGK

This is a code snippet that creates a file that can be used for tilesets, created in the AGK expected format for tilesets. I will write a full tutorial on how to use this code in a future post.  See this post on use of the snippet.

//Change these values if your tiles are a different size in pixels
tileWidth = 128
tileHeight = tileWidth
numHorizontalTiles = 16
numVerticalTiles = 16

//Open a new file to write.  I found no success writing to .txt.
fileid = OpenToWrite ( "myfile" )

//Variables used in the loop.
string$ = "" //This will be what gets written to the file.
tileid=0 //Used to count up and assign each tile an ID

//This is the same as saying for(int i=0;i<numHorizontalTiles;i++) with the i++ at the end of loop.
i=0 //Start at 0
While i<numHorizontalTiles

	//Same as above, nested for loops.
	j=0 //Start at 0 after each loop.
	While j<numVerticalTiles

		//increment tileid each loop. In Tiled 0 is no tile, so we start with 1.
		tileid = tileid + 1

		//Assign the line to be written to string$
		string$ =  Str(tileid) + ":" + Str(j*tileWidth) + ":" + Str(i*tileHeight) + ":" + Str(tileWidth) + ":" + Str(tileHeight)

		//Write string$ to file.  WriteToLine saves it in ASCII readable format.
		WriteLine(fileid, string$)
		/* Remove to watch progress.
		*/ //Remove to watch progress.

		//j++ for the loop
		j = j + 1

	//i++ for the loop
	i = i + 1

//Close the file
CloseFile ( fileid )