fallahn/tmxlite

getLayerAs error

1-max-1 opened this issue · 4 comments

Im using the example code from the quick start. On compile, I get an error:
error: undefined reference to 'tmx::ObjectGroup& tmx::Layer::getLayerAs<tmx::ObjectGroup>()'

Here's the code as a refresher:

		tmx::Map map;
		if(map.load("map1.tmx")) {
			const auto& layers = map.getLayers();
			for(const auto& layer : layers)
			{
				if(layer->getType() == tmx::Layer::Type::Object)
				{
					const auto& objectLayer = layer->getLayerAs<tmx::ObjectGroup>();
					const auto& objects = objectLayer.getObjects();
					for(const auto& object : objects)
					{
						//do stuff with object properties
					}
				}
				else if(layer->getType() == tmx::Layer::Type::Tile)
				{
					const auto& tileLayer = layer->getLayerAs<tmx::TileLayer>();
					//read out tile layer properties etc...
					SDL_Log("%s", tileLayer.getName().c_str());
				}
			}

			const auto& tilesets = map.getTilesets();
			for(const auto& tileset : tilesets)
			{
				//read out tile set properties, load textures etc...
			}
		}

Are you making sure to include ObjectGroup.hpp in which the templated function is defined?

Which compiler/environment are you using? I'm able to copy/paste your code and compile it without issue in MSVC on Windows and g++ on linux.

Oops, I was making a small change to the library and had temporarily commented out where it was defined. Completely forgot about it, that was the problem.

Oh and just as a little side question, where would I place my .tmx files for android? Currently I have them in the app/src/main/assets folder but tmxlite says it failed to open the map because it couldn't find it.

No problem, it happens to us all :)

You make a good point about opening files, however. It appears fstream doesn't actually work with the NDK (so why they include it, I don't know, especially when they don't implement std::to_string()). You'll have to convert the library to use fopen() or possibly even use SDL2's file system functions. I'll have to look into creating some sort of interface for custom file streams in the future. As a work around you could load the tmx file into a string then feed that into the map loader via Map::loadFromString() although this will likely fail if it needs to load external tileset files, for the same reason.