|
The project i'm working on for this is here: https://github.com/johncwelch/SOS-Swift/tree/main/SOS-Swift (I'm not sure how to create a "small" version of this, so hopefully I can explain. I have a game grid that is anywhere between 3x3 and 10x10. THat works. The idea is to spell "SOS" first. That works. part of the project is to, when enabled, record and playback moves. The record was easy, if recording is enabled, which runs this function:
the moveRecord array is an array of structs:
myCommitedButtonIndex is the index of the button that was clicked in the game, myGridArray is a Game Class object:
that class contains an array of Cell:
and myCurrentPlayer is just a String with either "Blue" or "Red" once the game is finished, it can be played back, and this is where I'm having the problem. Here's the button action code:
playBackGame():
I think I know what the problem is, mainly that because of how swiftui works, the playback code isn't updating the button view until the button is "finished", i.e. the loop has completely run. If I comment out the sleep function, the result is the same, just much faster, i.e. nothing happens until the entire for loop has run. I've tried wrapping the call to currentPlayer() in DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) and there's no difference. how do I get the view to update after each run? (I would add a screenshot of the game, but linking to an image is now literally awful.) |
|
Someone said that part of my issue was sleep(1) and that I should use a timer instead, but that creates another set of problems. So let's say I have my struct:
and I have my main ContentView with some state vars:
and we have our body code doing stuff:
for print1, theMoveArray.count is 2 as expected, but for print2, theMoveArray.count is now 0, and I have NO idea what is causing that |
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.