Post

Replies

Boosts

Views

Activity

How to use EnvironmentObject in an init()
I am trying to use an EnvironmentObject in an init. I am aware that this is not possible because it hasn't been initialised yet. But what is the solution? I have tried loading the scene.gameCenterManger in .onAppear but that doesn't seem to work. I want to use @State for my gamescene variable, because SKScene will reset itself when the view does a refresh. struct GameSceneView: View {     @EnvironmentObject var gameCenterManager:GameCenterManager @State var scene = GameScene()     init() {   scene.size = CGSize(width: 1000, height: 1800)       scene.scaleMode = .aspectFill // doing it as below will throw an error. //scene.gameCenterManager = gameCenterManager     }     var body: some View {         SpriteView(scene: scene )             .environmentObject(gameCenterManager)             .frame(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)             .ignoresSafeArea()             .overlay(ImageOverlay(), alignment: .bottomTrailing)            .onAppear{ scene.gameCenterManager = gameCenterManager  }     } }
0
0
832
Aug ’21
How to use EnvironmentObject in an init()
I am trying to use an EnvironmentObject in an init. I am aware that this is not possible because it hasn't been initialised yet. But what is the solution? I have tried loading the scene.gameCenterManger in .onAppear but that doesn't seem to work. I want to use @State for my gamescene variable, because SKScene will reset itself when the view does a refresh. struct GameSceneView: View {     @EnvironmentObject var gameCenterManager:GameCenterManager @State var scene = GameScene()     init() {   scene.size = CGSize(width: 1000, height: 1800)       scene.scaleMode = .aspectFill // doing it as below will throw an error. //scene.gameCenterManager = gameCenterManager     }     var body: some View {         SpriteView(scene: scene )             .environmentObject(gameCenterManager)             .frame(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)             .ignoresSafeArea()             .overlay(ImageOverlay(), alignment: .bottomTrailing)            .onAppear{ scene.gameCenterManager = gameCenterManager  }     } }
Replies
0
Boosts
0
Views
832
Activity
Aug ’21