Get sepecific property value of tiled Map

Hi,
I have defined a property as tag in tiled map editor, and what to retrieve that in cocos2dx

Here is how I do that:

for (int h=0; h< size.height; h++){
    for (int w=0;w<size.width; w++){
         auto sprite = layer->tileAt(Vec2(w,h));
         auto gid = layer->getTileGIDAt(Vec2(w,h));
         Value v = tmap->getPropertiesForGID(gid);
         ValueMap m = v.asValueMap();
         auto tag = m.at("tag");
         if (!tag.isNull()) {
	       CCLOG(" tag string is %s", tag.asString());		
          }
   }
}

I was expecting the output to be like blue, gray, black.... which is the tag I put in the properties of the tiled sprite, but the actually out is some thing like:

   tag string is ?SEgray

Some thing like that
I also tried this:

  tag.asString();

But that doesn’t turn out to be right either.

I figured this out

I should use this

std::string type = tag.asString().c_str();

to cast to the final string, not sure what’s .c_str() is, but it worked.
Thought should put it here to help some one meet with the same issue.

Hi,
c_str() is the “C” representation of a string, aka a const pointer to char (const char*) which is what the CCLOG function expects. These problems appear when you mix C and C++ code which cocos does a lot.

Hi,

Thanks for explaining that, so if I didn’t use CCLOG to print the value, and only use that as a compare string in my game, then there will be no need to use the c_str() function, and things will still work, did I get that right?

Yes, you’re right.
Actually if you want to compare string you CANNOT use the c_str() function because you would be comparing a pointer instead of a string.