- Home /
How to get actions in multiple instanced objects to occur in order
This is more of a question of strategy.
Basically, I'm trying to make a sort of path-finding system. There are "intersection" objects at each intersection, and they have a variable called "stepsToTarget". When I click, I lay down a target object, and each intersection object should set their "stepsToTarget" variable to a number. The one nearest the target will be "0", the ones next to that are "1", etc.
(Then, ideally, the player object would just look around for the intersection with the lowest number and walk toward that until it gets to the target)
So, if I have a map with 3x3 intersections, and I click on the bottom right intersection, it should look something like this:
4 | 3 | 2
3 | 2 | 1
2 | 1 | 0
So, I accomplished that by making a script for each intersection that basically says "Okay, look at each intersection around you. Find the one with the lowest number. Now set your number to that + 1.
This works great logically, except that I have to click multiple times to get it to work. The problem is that all of the intersections' scripts execute in some order, and they're all looking to their neighbors for their "stepsTotarget" number, but some of them haven't been set yet, which causes this one to be set wrong.
So, does anyone have any ideas on how to make sure they all get set correctly? I hope it makes sense. I can paste my code in if anyone really wants to see it, I just thought it wasn't necessary in this situation.
And I guess as a side-question, can anyone point me to the knowledge about what determines in what order scripts are executed? (I mean, one script over another. Not just within a single script)
Thanks in advance!
Answer by bubzy · Aug 16, 2013 at 06:29 AM
int mapWidth = 5;
int mapHeight = 5;
void makeMap()
{
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth ; x++)
{
//instantiate your object here
//you can then assign the object a value based on the x,y values.
//this will instantiate the objects in order, and allow you more control
//after this you may want to look for the manhattan method of
//calculating heuristic values.
//this method works OK, but doesn't allow you a lot of flexibility without
//the rest of the a* algorithm
}
}
}
I would've used a breadth first search, but this is probably a simpler way.
That's useful advice. I'll read up on that A* stuff.
But doesn't your method of instantiating them only work if a map only has one static target? If I'm creating a new target every time I click, then I'd have to destroy and re-instantiate them, wouldn't I? I guess that's not too terrible if it works...