Picking the closest sprite and only one instance of it?

  • Hey guys,

    I'm in a bit of a mess with my project.

    In my game, people can construct buildings. However, I can get one person to construct a building, that's fine. But if I have more than one person that can build something, everyone rushes to build it which is not suppose to happen. I simply cannot get it to happen properly. I basically want the closest available person to be picked for the job.

    What happens:

    Buildings:

    For every building placed by the player, the game assigns that building with a unique Build_ID value from the Build_ID global variable which incrementally increased when a building blueprint is placed but before it gets assigned to the building. In this state, the building initially has Builder_On_The_Way set to false and Construction_Progress set to 0. Then the building waits for a person to come and construct it.

    Person:

    People (Person) sprites by default have their status set to 0 so they will just "wander around". The game currently sets the position of the Pathfinding_Destination_Helper sprite to a random co-ordinate on the layout (I use the helper sprite so I can get a visual of where they are going). Their status is set to 0, and by default when set to 0, their BuilderAvailable boolean is also set to false. Under these conditions, a person is available to be assigned a building to construct if they are the closest person to it.

    I believe the problem lies here somewhere, but I can't make sense of it.

    So you understand the statuses:

    Assigning builders:

    Please help, I'm going mad!

  • You can use "Pick random instance" (or "Pick nth instance" with '0') as a sub event when you need to isolate just a single instance.

  • It's hard to tell what's going on by these screenshot. My guess is the event 76 is triggered multiple times and multiple people are marked with status=1.

    Event 79 is definitely wrong. Condition "Build_ID=Person_Build_ID" checks instance 0 of person object only! So if there are many people, this event will almost always be executed on every tick, clearing Builder_On_The_Way state.

    If there are multiple buildings being constructed, event 78 may move the Pathfinding_Destination sprite from one building to another on every tick.

    In other words, it's a mess

    I suggest you put Brower.Log into these events and output some debug info, then you'll be able to see how everything works.

    Object.PickedCount is a useful expression, it can tell you how many instances are picked in each event.

    I would change these events completely. Testing a bunch of instance variables for lots of objects on every tick is bad for performance and it's hard to program these events correctly.

    Use triggered events! When player clicks on the map to put a new building, pick all free people (with status=0), then pick one that is closest, direct it to the building site, set status=1.

    On pathfinder arrival, change status to 2, start building. On completion, set status to 0.

  • Hey! I just want to thank you guys for responding despite the forum's current condition, which is very frustrating at the moment.

    Even though I have posted something here, I have been hard at work trying to get this right. This is now working but with terrible side-effects. Event 76 is this currently:

    dop2000 I much appreciate your detailed response and suggestions. I wasn't able to simplify things due to the mess I got myself into. I actually like your idea, however, there is one thing I am not sure about; In my game, my player will be able to place multiple blueprints regardless of people availability/resources. What if all my people are building and there is no one free? How is it possible to have a trigger event in those circumstances if I'm not constantly checking conditions every tick?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can start a Timer "Find_Builder" for each new building, say for 1 second.

    On Timer event try to find an available builder. If not found, restart the Timer.

    (Make sure to use "For each Building" in On Timer event, as it may be triggered for multiple building instances at once)

  • dop2000 This is now working very well. I just want to make sure that I have done what you have suggested correctly, in terms of performance. There is just one thing I had to change, though, and you will see it just after the timer is triggered for the building, because "For Each" was working but the person was not constructing buildings in the order they were placed (something I forgot to tell you earlier, but don't worry I worked it out!) What was happening was; because each building had an individual timer that was running at independent times to each other (caused by the player not being able to place all blueprints down at once), a building with a higher Build_ID would get attention first if it's timer triggered first, and the person would just get assigned to the later blueprint instead, so pick all blueprints placed seems to fix that.

    Anyway, please have a look now, I hope I have done this adequately:

  • Your code should work, however since you have to pick all buildings in On Timer event, I suggest you move your Timer behavior to some other object (which you only have one instance on the layout, for example Map sprite).

    In event 79 you don't need "Pick all person" condition.

    In event 80 you don't need to compare person.Build_ID with building.Build_ID, since you've just assigned it, so this condition will always be true.

  • dop2000 I have made those changes as well as put timers for every cooldown variable system I had, it was a bit of a learning curve but things are working very nicely, now. Thanks for your help!

    Edit: Thanks for your help, AGAIN!

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