How do I make a while loop that runs in the background?

So when my game start function I want my enemies appear until the player dies:

while isdead == false {
self.delayWithSeconds(Double.random(in: 0.5...5)) {
self.RightSpike.run(self.DownAndUpRight)
}
self.delayWithSeconds(Double.random(in: 1...5)) {
self.LeftSpike.run(self.DownAndUpLeft)
}
}

But since this is a loop that runs forever until the player dies. The script can't go through under this piece of code, if that makes sense. So my question is how do I make this go forever and the compiler can see the code under the loop.

The loop you’re talking about is usually called the game loop and it’s a huge topic with many possible solutions. Here’s one nice article about it. One important trick is to split the rendering from updating the game state, so you would have something like:

var lastTime = CFAbsoluteTimeGetCurrent()
var state = GameState(…)
while running {
    let now = CFAbsoluteTimeGetCurrent()
    // Recalculate game state according to how much real time has elapsed
    state.update(timeDelta: now - lastTime)
    // Render the state to the screen
    renderState(state)
}

I suggest that you research writing game loops on iOS.

2 Likes

DispatchQueue to the rescue :slight_smile:

Inside my delayWithSeconds function are there the DispatchQueue function :wink:

Inside my delayWithSeconds function are there the DispatchQueue
function

No. I believe that @kirilltitov is suggesting that you use DispatchQueue to run your game loop, something like this:

let queue = DispatchQueue(label: "game loop")
queue.async {
    while running {
        … your code here …
    }
}

Generally I recommend that you avoid using Dispatch for this but instead create a dedicated thread. I explain why in this post.

Regardless of how you implement this, I agree with @zoul that you really need to get your head around the core concept of the game loop.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

3 Likes

Thanks! I just tried this but instead of running several time it just plays its ones.

let queue = DispatchQueue(label: "game loop")
    queue.async {
        while self.isGameStarted == true {
           self.delayWithSeconds(Double.random(in: 0.5...5)) {
                self.RightSpike.run(self.DownAndUpRight)
            }
            self.delayWithSeconds(Double.random(in: 1...5)) {
                self.LeftSpike.run(self.DownAndUpLeft)
            }
        }
    }

instead of running several time it just plays its ones

I’m not entirely sure what this means, but I think you’re saying that the game loop exits after one iteration. If so, it’s hard to say what’s going on without seeing the logic that updates isGameStarted. My recommendation is that you step through the code to see when isGameStarted changes.

btw Earlier I recommended not to use Dispatch for your game loop, but instead use a thread. There’s a snippet showing this in the other post I referenced.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Psst, I think you mean this other post.