|
Okay, this is a bit long, apologies. So thanks to the folks here, I've figured out some stuff for my SOS game https://github.com/johncwelch/SOS-Swift here's a much shorter repro case that has the exact same problem. ContentView.swift contents:
All the classes and functions used:
so the idea is, the board is built, all cells have a title of "" (blank) and Commit Move is disabled.
So a three-click rotation: "", "S", "O" what i'm actually getting is a four click rotation:
that's the cycle, but it gets weirder. suppose on button 0, i leave it at "S". Then i click on button 1. I get:
if I keep clicking, it goes back to a four-click cycle if I move the buttonBlank @state var underneath body, the "all buttons blank" problem goes a way, it's a three click cycle as I expect, but buttonBlank never changes so the commit move button is always disabled. I am absolutely sure this is related to changing the value of buttonBlank, because as soon as I put any statement with that in, even just: buttonBlank = false or self.buttonBlank = false in the grid button click action, I get that behavior. If i never try to change buttonBlank, then the weird behavior disappears |
|
Hi John, After tinkering with your code i think the problem is having the |
|
@Hectorcrdna, do you have some sample code? I'm still pretty new to swiftui, so a lot of the subtleties of the various @vars are kind of confusing. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
No problem, There are a few ways you could do this but for example since you're using iOS 17 you should be able to do something like this:
and then
|
|
okay, so that makes a lot of sense and thank you!!! Next question: in the "real" version of this, the grid size is selected via a picker with values from 3 to 10 in an @StateVar (gridSize). How do I incorporate the ability to change the gridSize? would I just call game.buildStructArray(theGridSize: gridSize)? thank you so much for all of this, i keep feeling like i'm this close and it's that last part that is making me bonkers. (the "real" project is up at: https://github.com/johncwelch/SOS-Swift) |
|
I was able to look at your code and i added the following: In
In
then inside the body i changed the
and in the grid
Even with some of the changes i kept geting let me know if it works for you. |
|
|
|
Hector, thanks for helping fix it; I'd also been looking at it. What struck me about this is the implication that the .onChange handler is running on a background thread. (Because the UI is redrawing even in the middle of the .onChange handler). This led me to think the ideal solution (rather than a workable solution) might be to force the variable change to happen on the main thread, which would have the effect of blocking UI updates. Another thing I'd been thinking about was keeping ContentView's @State variable for gridSize BUT have the grid read from the game.gridSize variable and update that after the array had been updated, but that might still have timing issues when the array shrinks. (Just wanted to document these thoughts in case others have similar problems and needed to consider other approaches.) |
|
@deirdresm you are right, i forgot to mention to @johncwelch that i removed the .onChange and the boardSize @State var since i was taking the size straight from game.gridSize.
Yea it's still giving me the index out of range when the array shrinks; i was searching around and found a release note for iOS13 that says
So that might be the issue, i was noodling around and made the game.gridCellArray a 2D array and had the grid read from the array directly which eliminated the need for the myIndex var and any index out of range issues, plus the forEach gives you the actual cell you click on, that also is another option. |
|
It seems you have a question or issue related to a "Button WTHeck?" or some form of button behavior. However, your message is quite brief, and I need more context and details to provide a specific answer or assistance. Could you please provide more information about what you're experiencing or what you need help with regarding this button? Specifically, it would be helpful to know: What is the button used for or where it is located (e.g., on a website, in an application)? What behavior or issue are you encountering with the button? Do you have any specific questions or tasks related to this button? The more details you can provide, the better I can assist you in understanding and resolving the issue. |
|
|
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.