Pathfinding and Families

0 favourites
  • 6 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • Not sure if this is the right place to ask but I thought it was worth a try. Maybe this could be considered a bug, but I don't know nearly enough to post there.

    Basically, my question is about the pathfinding behavior and families. I'll try to keep it simple for this question. In short I have NPCs in my game that need to navigate around the map on a schedule. I am using pathfinding for this. All of these NPCs are in one family, the pathfinding behavior is attached to that family not the individual NPC. The NPCs are pathing around solids and not some crazy custom settings. The only other thing they are told is there is a tilemap overlaying the game world that has a cost. Basically this tilemap just keeps them on roads and prevents them from taking the quickest route to their destination.

    The problems seem to arise when there are more than one NPC in the game world. For whatever reason pathfinding seems to grow volatile. Seemingly at random when an NPC is told to find a path it will sometimes take around 15 seconds to find it, other times finding the same path will be instant. Sometimes NPC's just calculate their path forever. I also can't say for sure but I'd swear I've seen the wrong NPC trigger the "On Path Found" event. There are also times when NPCs just ignore the tilemap that has cost attached and walk through it, this seems to only happen when there are multiple NPCs in the game world, if there is one the NPC will follow the tilemap cost.

    The best I can describe it is it feels like the whole system is breaking down. As far as I can tell there is no problem when there is only one NPC but if you add in more it all becomes shaky, I say shaky because like I said the errors seem to occur randomly.

    I guess my question to anyone out there is, has anyone had similar issues? Are there known issues when combining pathfinding and families or is my code just bad (which I'm willing to believe)? I'd post the capx but its massive and I'd rather not just put it out there, also the system surrounding pathing is complex enough that I can't just recreate it in an example. The only other relevant piece of info I can think of is that the game is a one layout game, I was told C2 could handle it so I took the devs at their word, the layout size is 20184, 9664. If anyone has ideas I'd welcome them, otherwise I'm gonna get to work on making a custom pathfinding solution.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • What kind of logic are you using to pick NPCs and ask them to find paths? This kind of logic is just more difficult than it sounds and it can easily get out of control. I once made a theme park style game with hundreds of guests and came across a lot of issues. I would say an easy way to make sure that the correct NPC is assigned the correct task is to use functions and pass the UID of the NPC through the function as a parameter, this gives a bit of control to it, more than if you just use NPC find path.

  • First off, thanks for your reply. That is actually exactly what I'm doing. I'll try to tell you what happens in the code, hopefully that can better explain what happens. The one thing to know preemptively is that each NPC has a schedule telling it what to do. The schedule is stored in an array and broken up into events. So the first event in the day would be like walk to your kitchen table, or something. Once the NPC has done that it will grab its next event which also has a time that it should execute. Once the game reaches that time it will perform the next event, so on and so forth.

    Without delving too deep the basic logic is as follows.

    1. Call initial event to tell each NPC to grab its very first event in their schedule.

    2. Each second there is an event that checks if any NPC should be starting its next event.

    3. When the prior step triggers a function is called which passes the NPC UID.

    4. This newly called function does a few extra things but then using the NPC UID is was passed tells the NPC to find a path.

    5. Last there is an "on path found" trigger that tells the NPC to move along its path, I will say that this "on path found" event doesn't reference any UID, I figured it didn't need to since only one NPC should ever have "found a path" at a time.

    Another note for step 5, from what I've seen only one NPC can be finding a path at a time, so I'm assuming the engine should know exactly who found a path without needing to reference a UID. If one gets stuck trying to find a path it holds up the rest until it is done. From what I've read, assuming I've read right, this is intentional for C2.

  • I helped someone debug a similar project a few months ago, and it was definitely their code - picking issues, not handling all the path finding triggers, setting things up incorrectly, etc.

    see thread: https://www.construct.net/en/forum/construct-3/how-do-i-8/why-objenemy-going-south-141207?kws=path

    it is amazing how minor little details can make a big difference in tracking down issues!

    There is nothing wrong with the approach you are taking - using a family is a great way to optimize your code. Like plinkie said, using functions and picking by UID is a great way to help keep things organized. Just make sure you aren't breaking the object picking before you call the function.

    if none of this helps, then maybe try to save your project with a new name and then delete as much as you can to make it smaller so you can share it...

  • you typed that last message while I was typing and looking for the old thread...

    you are right in that "on path found" does not need a UID.

    But my understanding of pathfinding is that multiple objects can be looking for a path at the same time, so I don't think one can be holding up others.

    my gut feeling is that you still have a picking issue of some kind, or possibly a scheduling issue with your array (giving objects conflicting orders).

    One thing to check out is if multiple NPCs start things at the same time. That is a common place for picking issues. if several NPCs are picked, but the action only calls the function once, or with the wrong UID, that would cause trouble. Depending on your code, you may need a "For Each" loop to make sure each NPC gets processed...

  • Thanks for the insight. I did check if multiple NPCs were starting at the same time but that isn't the case. While most of the start times in the schedule events are similar (since they are on the hour or half hour) each event gets off-set by the time it takes an NPC to travel there from where they are so they end up getting randomized. I put a for each in the "on path found" just in case, but no luck.

    If I had to guess I'd say you are probably right. The pathfinding in my game is fairly complex and it wouldn't surprise me if I broke it somewhere along the way. Honestly though I think I'm just going to make my own pathfinding system and ignore C2's. I think if I set up some stuff manually it can be done easily.

    The way this game is set up doesn't really play well with the pathfinding engine anyway. There's lots of jumping around since NPCs are going into and out of buildings and those interiors are actually placed across the map so they have to teleport there then path again, it all sort of becomes a mess. Not to mention just pathing across like the massive layout feels like it would invite problems.

    Anyway, thanks for your help, but I think I'll just do something a little different to cover the issue, I mostly just wanted to make sure there wasn't some weird C2 bug regarding families and the pathing behavior.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)