Question about loops and tickcount

Get help using Construct 2

Post » Tue Aug 26, 2014 2:54 pm

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).
B
123
S
43
G
41
Posts: 847
Reputation: 28,206

Post » Tue Aug 26, 2014 4:29 pm

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"
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Wed Aug 27, 2014 1:16 pm

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!
B
123
S
43
G
41
Posts: 847
Reputation: 28,206


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests