0

I'm currently creating a game that uses several different timers to increase several numbers, displayed as 3D Text, on the screen simultaneously. When only one number on the screen is present it works perfectly and the number counts up pretty seamlessly. However when more than one number is present and consequently more than one timer is running, the numbers are really glitchy and they all count up showing the same numbers, even though they have different values.

I start the timer using this:

p1Price = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(p1PriceCalculator), userInfo: nil, repeats: true)

And use this to change the text:

    @objc func p1PriceCalculator() {
        var smallHouse1Incremental: Int = Int(arc4random_uniform(UInt32(100)))
        
        smallHousePrice1 = smallHousePrice1 + smallHouse1Incremental
  
        smallHouse1Incremental += Int(arc4random_uniform(UInt32(100)))

        if let smallHouse1TextGeometry = smallHouseText1.geometry as? SCNText {
            smallHouse1TextGeometry.string = String(smallHousePrice1)
        }
    }

There are several of this same setup throughout the code, with the only change being the name of the nodes.

Does anyone have knowledge as to why this is happening?

Thanks!

1 Answers1

0

That is a pretty frequent occurrence at 0.2, but it's doable - I've had 10-12 running at the same time with some decent stuff in the loops. I don't see a tolerance set to give it any flexibility - Docs(Setting a tolerance for a timer allows it to fire later than the scheduled fire date. Allowing the system flexibility in when a timer fires increases the ability of the system to optimize for increased power savings and responsiveness.) - also make sure your times are running in the main thread - I don't remember if they just won't work, or if they work sporadically - but DEF issues if you don't.

Past that, you may have to look into your calls and see if you're doing too much somewhere else. When it fogs up, did you run instruments to see if you are loosing memory or racking the CPU?

Voltan
  • 1,170
  • 1
  • 7
  • 16