Question about loops and tickcount

0 favourites
  • 3 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • I have a small question. I have some code that loads up some rooms at the beggining of the game, and it works fine if I use tickcount (since it's on the beggining), but I was trying to move this code so I could use it later by replacing it with a loop. So I'd like to know what's the difference between this:

    For "t" 1 to 30 do -----

    And this:

    If tickcount > 30 do -----

    Aren't both supposed to loop 30 times? The "for" got stuck on an infinite loop when I tried it out. The way I found to fix it was to create a variable and add 1 to it if it's under 30 (since the code will update every tick).

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The difference is the for loop will do everything in one frame and the tickcount or adding variable way will take 30 frames. If the for loop takes too long the browser may complain, thinking it may be an infinite loop but it may not be.

    The "for" loop will run 30 times every time it's encountered in the events. That means that everything stops until it finishes. If the events it does is very time consuming then the web browser will show a popup guessing it's an infinite loop, but it's not always, it's just taking too long. Usually for something like level loading you only want to do it once instead of every tick so you'd use "start of layout" or something.

    Start of layout

    For "t" 1 to 30 do ----- "level loading events"

    The tickcount way just runs once per frame for 30 frames (ticks) instead of 30 times in one frame. Doing it that way has the advantage of distributing the time to do something over multiple frames. Specifically this will run once per frame for the first 30 frames.

    If tickcount < 30 do ----- "level loading events"

    You could also use "is in between values" to run at any frame range. For instance to start at tick 30:

    global number Start_Tick = 30

    If tickcount is between values Start_Tick to Start_Tick+30-1 do ----- "level loading events"

  • Thanks a lot R0J0hound ! I was actually messing with your "random room generation" example. When you load the rooms you used the tickcount method, so I was messing with it to see if the rooms could be loaded later.

    I totally get it now, thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)