0

I am working on a game and want to move the interface logic out of the game logic. The following is what I tried but does not work. What is the correct way to do this?

// Game scene & logic
class GameScene: SKScene, ObservableObject {
    @EnvironmentObject var interfaceControls:InterfaceControls // This does not work

    func gameOver() {
      interfaceControls.isMenuShowing = true
    }
}

// Interface state
class InterfaceControls: ObservableObject {
    @Published var isMenuShowing = false
}
dooblr
  • 458
  • 1
  • 5
  • 18
  • 2
    You have to explicitly pass references (via `init` or setting a property) instead of using `@EnvironmentObject`). Aka: `GameScene(controls: interface controls)`. This may mean you have to make a custom `init` for your `View` – jnpdx Jan 15 '22 at 01:01
  • 1
    `@EnvironmentObject` won't be stable in a `class` it is only for SwiftUI `View`s. you have to use `.sink` to get updates – lorem ipsum Jan 15 '22 at 02:06

0 Answers0