Where do I place my code to instantiate my Project’s `struct` code within `SwiftUI`?

Where do I place my code to instantiate my Project’s struct code within SwiftUI?

As you can see way below, I call:

_ = StaticObjects()

Within GameScene’s didMove

It seems the reason I don’t see the same #Preview image in my GameScene is that I am not explicitly returning a SwiftUI some View within my struct layout.

I do know that didMove is really called .. but I just don’t see anything within the Simulator

My #Preview works great:

The following code appears within GameScene.swift of my Xcode Project:

import SpriteKit

import SwiftUI

struct StaticObjects : View {
          
    // some View is returned when our struct is instantiated
    var body: some View {
        
        let theBuildings = ["hotel",
                            "saloon",
                            "watertower",
                            "jail",
                            "farmhouse",
                            "barn"]

        let theFigures = ["desperado",
                          "sheriff",
                          "space",
                          "space",
                          "horse",
                          "guy"]

        VStack {
                        
            HStack(alignment: .bottom) {
                
                ForEach(theBuildings, id: \.self) { theBuilding in
                    Image(theBuilding)
                        .resizable()    // for rotation
                        .scaledToFit()
                        .zIndex(2)
                    Spacer()

                }   // ForEach
                
            }   // HStack for theBuildings
                        
            HStack(alignment: .bottom) {
                
                ForEach(theFigures, id: \.self) { theFigure in
                    Image(theFigure)
                        .resizable()    // for rotation
                        .frame(width: 120, height: 230)
                        .offset(x: 0.0, y: -50.0)
                        .zIndex(2)
                    Spacer()

                }   // ForEach
               
            }   // HStack for theFigures
            
            // aligns vertically the entire struct to the top
            Spacer()

        }   // VStack
                
    }   // body:
    
}   // struct StaticObjects


/*
#Preview {
    StaticObjects()
}
*/

class GameScene: SKScene, SKPhysicsContactDelegate {
        
    override func sceneDidLoad() {
        
        super.sceneDidLoad()
                               
     }   // sceneDidLoad
    
    
    override func didMove(to view: SKView) {
       
        super.didMove(to:view)
        
        physicsWorld.contactDelegate = self
        
        _ = StaticObjects()   // doesn’t work
                                         
    }   // didMove
    
    
    func didBegin(_ contact: SKPhysicsContact) {
                                
	    // ...

    }   // didBegin
    
}   // class GameScene

You should use UIHostingController: https://sarunw.com/posts/swiftui-view-as-uiview/

Thanks bunches, Claude31.

Unfortunately it didn't work .. and I take full responsibility for that because I feel I did not fully explain all the details necessary to describe my goal ..

  1. I faithfully inserted all the code your doc stated should be placed within my viewDidLoad. It compiled okay.`

  2. My GameViewController consists of 2 vertical halves. The bottom shows several animated SKSpriteNodes that move. This bottom half is not the subject of my quest for help.

  3. It's the top half that contains my GameStaticObjects. As you can see in the above screen shot, this struct comprises 2 rows of static objects using HStack and Vstack. I discovered the latter that docs said would cause them to spread out edge-to-edge as the destination iPad rotated 90 degrees. In short, as the length of the string along which these HStack objects were strung, they would spread out and go edge-to-edge of the parent window frame.

  4. Before GameStaticObjects, these SKSpriteNodes were added in the addGamePieces function of my GameViewController via aSKScene.addChild(aSKSpriteNode). As already noted, however, the just talked about edge-to-edge rendition did not happen .. not to mention the fact there was a lot of hard coding required to set the position of each of these static objects.

  5. So, here I am .. I added all the code specified in your doc reference to my viewDidLoad function and nothing happened.

  6. I'm tempted to think that I need to add a small amount of code to the above addGamePieces function.

I really apologize for being too wordy here .. but I don't know to do it differently so you are fully aware of where I am.

Where do I place my code to instantiate my Project’s `struct` code within `SwiftUI`?
 
 
Q