Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Iterating through an Object Group in a tmx file using fallahn's tmxloader  (Read 2711 times)

0 Members and 1 Guest are viewing this topic.

boniomri

  • Newbie
  • *
  • Posts: 9
    • View Profile
I made an Object Layer in tiled with afew spawn points.
I have been trying to use this code to iterate through the spawn points but it dosent seem to work.

if (type.find("enemy") != std::string::npos)
        {
                for (auto layer = ml->GetLayers().begin(); layer != ml->GetLayers().end(); ++layer)
                {
                        if (layer->name == "spawns")
                        {
                                for (auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
                                {
                                        std::unordered_map<std::string, Entity*>::const_iterator it = this->map.find(type);
                                        while (it != this->map.end())
                                        {
                                                if (it->second->getPosition() != object->GetPosition())
                                                {
                                                        e->setPosition(object->GetPosition());
                                                        return 1;
                                                }
                                        }
                                        return 0;
                                }
                                break;
                        }
                }
        }
It wont go inside the second for-loop for some reason.
This is the object group located in the tmx file:
 <objectgroup name="spawns">
  <object id="18" name="spawn" x="390" y="225"/>
  <object id="20" name="spawn" x="642" y="225"/>
  <object id="21" name="spawn" x="390" y="515"/>
  <object id="22" name="spawn" x="642" y="515"/>
 </objectgroup>
 

I thought since the collision code that he showed in his Github wiki page was pretty similliar with what I was trying to do, I'd use it for getting the spawn locations aswell.

Link to the git : https://github.com/fallahn/sfml-tmxloader

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Iterating through an Object Group in a tmx file using fallahn's tmxloader
« Reply #1 on: November 27, 2015, 02:58:19 pm »
If it's not entering the second for loop then

if (layer->name == "spawns")

is simply not returning true. You can use a break point and your debugger to check your layer data contains what you think it should.

You can also check a layer by its type such as tmx::ObjectGroup. If you're only reading layer data const references work as well as iterators, but look much tidier and are probably easier to read. This works for me:



There is a chance of course that the latest version of Tiled maps nest the Object layer xml differently. You can try this by testing the example map (in the picture above, included in the repo) in your code, and trying your map in the example code which comes with the map loader. If there are any bugs I'll be happy to address them.

boniomri

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Iterating through an Object Group in a tmx file using fallahn's tmxloader
« Reply #2 on: November 27, 2015, 06:34:52 pm »
I step-by-stepped the program, the if statement does return true. I will check your solution as soon as I get home and report back. Thank you for the detailed answer!

Edit: Fixed by making the spawn points using the polygon tool instead of just the rect tool.
Shitty workaround, but thats the only way it seems to work.
« Last Edit: November 27, 2015, 11:50:00 pm by boniomri »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Iterating through an Object Group in a tmx file using fallahn's tmxloader
« Reply #3 on: November 28, 2015, 11:33:57 am »
Fixed by making the spawn points using the polygon tool instead of just the rect tool.
Shitty workaround, but thats the only way it seems to work.

Interesting, this may be a bug. I'll look in to it when I get the time