|
Hi all, I'm very new to learning swift and programming in general and I've got a bit stuck with why my LazyVGrid is not automatically updating. I have created a grid of hexagons and the image for each hexagon is controlled by an @Published dictionary in a seperate 'gameController' class. I want to select a hexagon and then update the image value to represent placing a building on the tile and the map refreshes Sorry if this is way too much code being posted but I wanted to try and include everything relevant! GameController class - there are other things in this and this is passed around all my other views and works fine
Build the hexagon shape. Change the build menu show flag and records the selected hexagon index into the game controller (numbers on tiles are just for testing purposes)
Map View which builds the hexagons into a honeycomb shape and shows the build menu popup is the flag is true
Build menu pop-up which selects what you want the tile image to change to and updates the dictionary value
I can print the dictionary at the tapped index value when I select an option on the map popup view and it correctly shows that the dictionary entry has been updated but the honeycomb map doesnt update the image. If i exit out of the map and go back in it then correctly displays the changes that have been made. I might be missing something very obvious but it doesnt seem to be updating the view unless I reload and I dont know how to force it to update when I change the value. I have looking into objectWillChange etc but I dont really understand how to implement them fully or how to use them. Any help from anyone is appreciated |
|
Chris ponders the
I suggest you take a few days off from your You'll get much more help from a few of @twoStraws' lessons than you might get from a forum post. Chris continues:
You don't. SwiftUI monitors the values used to build its parent and child views. If any of those |
|
@twoStraw's lessons will help you in other ways as well. TileViewBreak your big problem into a bunch of smaller problems that are easy to solve. Your game is made up of tiles that have icons, text, colors, and rules. Put all those parameters into one struct. Then build a second View struct to display ONE of your tile objects.
MapViewYour
Select a new icon for the TileDefine a different view to allow your player to change an existing tile. What did they tap? What are their options? Hand the heavy work load over to your Game Controller. That's where game logic belongs! It doesn't belong in a view.
Game ControllerYour game is a collection of
HexagonGridApp (Application starting point)
|
|
Thanks so much for this! The example code is really useful for giving me a good idea on what I need to go back and look at to make sure I am understanding correctly. I'll dive back into those lessons. One thing this straight away has helped me with is confirming what I had thought recently, which is that I should be inserting my Game Controller as a Environment Object instead of an Observed object |
|
I added a Also updated the In the MapView, swap out the default animation for one a bit springy:
|
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.