- Home /
Advanced C# problem: Timings incorrect in script, assigning a variable that is not updated yet in an array (If someone wants a challenge, hop on in!)
Dear developers,
I am trying to make a match-three game from scratch ( a game like Candy Crush ) and I stumbled upon a timing issue I can't seem to resolve. I do not know how to get started.
When I switch two pieces, at the end at their moving co-routine I set the pieces to their new position, and update the 2 dimensional array where I store all pieces and raise an event saying it has stopped moving. In a handeler I wrote, check if its partner piece has matches. (the one we swapped with) and check if the piece itself has matches.
If they both don't, obviously they swap back. This works.
But if one of the pieces has a match, clear out the matches. As the other has to stay in the same place. This doesn't work.
I check with the debugging tool and found the problem, but I don't know a way to solve this and reach out to you guys for help since I am yet a beginner.
Here is the code I use: PasteBin link to my code
The problem is this par example:
I have a blue piece at 0,0 [X,Y] coördinates. I have a yellow piece at 0,1 [X,Y] coördinates.
If I swap those 2 pieces, the blue one does his check in the handeler first. The new position is already updated, so at 0,1 we have the blue piece.
But when I calculate the partner piece, using it's previous position hence the 0,0 coordinate where the yellow one is supposed to be, it ALSO thinks it has a blue piece. It seems like the yellow piece's moving routine still has not finished and hence why it thinks there are 2 the same pieces.
Thanks in advance, I hope you guys can help!
This results in a bug where the piece that is supposed to stand still, swaps back to his original spot. (1 of the pieces has a match, the other one doesn't. So it has to stay in the same spot. )
Answer by tanoshimi · Jul 12, 2017 at 12:02 PM
Don't you just need to add a yield return new WaitForEndOfFrame()
near the end of your coroutine movement function before the OnPieceStopped callback, to ensure that both pieces have updated to their new location before testing for matches?
Well, I added a WaitForSeconds(0.5f) before like you proposed. I just delays the bug.