I am currently working on a project, where I use SpriteView to display game content and normal Views to display menus and navigation in general. I am able to create and load SpriteViews when pressing buttons in the normal view but the communication does not work the other way. I want to be able to change State variables in the parent View-Element by using buttons/SKShapeNodes of the child SpriteView-Element.
I tried Binding variables between the two instances and using callback-functions. But I wasn't able to change any content in the View-Element.
Is there a simple and effective way to send requests from a child-SpriteView to the parent-View ?
You can use the
ObservableObject-@Publishedpattern for this.Make your
GameSceneconform to theObservableObjectprotocol and publish the properties that you are interested to send the values of into the SwiftUI views, something like this:Then, in your SwiftUI view, use a
@StateObjectproperty (or an@ObservedObjector an@EnvironmentObject) to store theGameSceneinstance. You can then use theGameScene's published properties in your SwiftUI view:When you "press buttons in the normal view", change the value of the published properties, and they will change the SwiftUI views that uses them.