I have a group of npcs who are supposed to spawn at a spawn point whenever the spawn point is clicked and head to an unoccupied tree. Because they’re woodcutters, but that’s not important right now, because all they’re supposed to do right now is contemplate the trees. A tree. A tree of their own.
Depending on how I do the events, however, they all only go to one of two trees. Even if I say, “if tree is not in collision with one of you dudes already, then you are allowed to go there.”
They still all go to the middle left tree. And if I say, “pick a random tree, then go to it,” either upon creation of the npc, or for each object of woodcutter, they all head to the middle right tree.
How do you pick an unoccupied instance of tree, so that the woodcutters will pathfind to their specific personal tree?
I would add a boolean variable to the tree objects, say named “occupied”. When the woodcutter is spawned, find all the trees with “occupied” = false, pick a random tree, set “occupied” to true and move the npc to it.
An alternative to this is to add a number variable to the tree object, say called “woodcutterId” and defaulting to 0. When a woodcutter is spawned, allocate it a unique id. Select all the trees with a “woodcutterId” of 0, pick a random one and set it’s "woodcutterId "to the new woodcutter’s id.
But either way, you’ll be best off adding a variable to the tree object to mark that it’s allocated to a wood cutter.
Thanks. That worked. I had to give the npcs a boolean, too. I changed it to mice, by the way. And i changed the target object because my concept is changing around a bit and they’re supposed to live in a planned society – mouse utopia – so the necessity of collecting wood is unneeded.
This is where allocating each facility a unique identifier and storing on each woocutter object the id of the target facility works. In the first event you can then check that the id of the facility in the collision is the same as the one stored on the woocutter .
Hi, when I saw your post yesterday, I tried to figure it out for myself and I came up with this solution (it is simpler and does not use a for each-condition):
There is is just one boolean variable ‘occupied’ for the tree-objects as MrMen has suggested. I used woodcutter is stopped as a condition for toggling the variable to avoid your current problem that instances stop if they accidentally collide with other targets. It seems to work, so maybe you find it useful.
Does that actually work properly? Because in the subevent the “woodCutter is stopped” condition is only checked when the mouse button is initially pressed (thanks to the “Trigger once while true” condition). Shouldn’t the subevent be at the same level as the first event and have a “woodCuttter is in collision with tree” condition added?
Also, it doesn’t cater for instances when multiple woodCutters stop at the same time - a possibility if they’re created at different times and have different distances to travel.
The reason is that the occupied-Variable is toggled instantly with the mouse click but the woodcutter starts just moving to its position. With a new click the occupied tree is not picked anymore, even though the woodcutter is still moving.
I like this solution - it’s nice and compact and requires minimal actions from GDevelop or the events. It’s pretty much a “get the woodCutter moving and leave them to their own devices”.