How do I manage a game based on grid logic?
Please help... I'm tired and about to give up.
I have a game with a non-jagged 2D array. The array rows represent x, and the columns represent y. Each position in the 2D array can hold an object of class type Machine, which is a class I made that can interact with other objects in the grid.
The player has the ability to place down sprites in the game on tiles that represent the grid positions that are in the code, and the sprites represent the objects of type Machine. This is where things get tricky. Objects in the grid have to interact with each other, simultaneously per frame.
The objects have to all interact simultaneously because if they interact iteratively (looping through each object) then there are inconsistencies. For example, say you have a grid of 3x3, labeling the objects numerically from bottom left to top right (0 - 8), so that the x axis ticks up the further you go to the right, and the y axis ticks up the further you move up. Now you do a standard double for loop iteration like so:
for(int i = 0; i < factoryGrid.GetLength(0); i++) {
for(int j = 0; j < factoryGrid.GetLength(1); j++) {
}
}
In my scenario one machine passes a value to another based on the direction it's 'facing'. Say object 0 passes a value to object 1, and object 1 passes a value to object 2. After the first full iteration, the value that was once in object 0 is now in object 2, whereas in a full iteration we only wanted it to go from object 0 to object 1. However if object 2 passes to object 1, which then eventually passes to object 0, the value would only be passed to object 1 because that's not the direction the loop iterates in.
The way I tried to fix this was to check if the value had been passed to the object the iteration is currently on, if true, then the current object is not allowed to pass it to another object. For some reason, this only worked as long as the object was not at a corner intersection. If it was a bottom left corner, it would skip the corner every time a value needed to be passed through, if it was a top right corner, it would take two iterations instead of one to get the value through. I have posted an image with this example, with the red squares being marked as the malfunctioning ones. I have no clue why this happens so I moved on to try a different solution.
Now I decide to try to activate all of my objects non-iteratively, with Unity events. This means I can tell all of my objects to pass a value at the exact same time, once per frame, so that no objects can do weird things like what happened in the corners when I was doing things iteratively. This summons a whole new problem. Now my values are passed through every possible object at the same exact time. If there are 4 objects, linked to pass the value from one to another, it all happens in one frame. I try to fix this by creating the same rule I tried iteratively, where an object is not allowed to pass a variable if that variables has already been passed, but unfortunately because the rule is being activated in all objects at the same time, the program is not able to catch the logic in that rule.
So that's my problem. I've been trying to debug this for 24 hours now, and because it's quarantine I haven't been doing much else. If you solve this, I would be forever thankful. Seriously.
I am currently trying to figure out how to use a sorted list to keep any weird things to happen, but could still use a lot of help with this question...
Answer by harperrhett · May 28, 2020 at 10:44 PM
After taking a break of programming for a whole day, I have found what seems to be the solution. The solution would be to create two separate lists. Each list stores objects if they are on alternating squares of the grid. For example, if x is 0 and y is 0, it is stored in the first list, if x is 1 and y is 0, it is stored in the second list, and so on and so forth. Then you can use those lists separately in foreach loops to call whatever methods you're using in your class and everything is happy, without conflicts, it would seem.