Game slow down
-
@waldron One trick which may or may not help you is to think of absolutely everything as having a cost; every loop through an array you have within your main game loop is chewing up CPU cycles. Modern computers like the Switch are enormously powerful for these types of simple 2d games we're making, but there's still a limit :)
Something I've been employing is, "what kind of pre-processing can I do during level loading, to make the game loop as streamlined as possible?" Users won't care if the levels take an extra half second to load, if it means the framerate can be higher during gameplay.
So, if you can somehow structure your data in an "uglier" way, but which means fewer "sub-loops" during your main loop, the better off you'll be.
I learned this trick when doing STOS programming on the Atari ST-- even when compiled, virtually anything (relative to something like the Switch) in those programs' main loops would bring that Motorola 68000 to its knees :) In my racing game (which even had simple AI opponents), it was a struggle just to maintain something like 15-20 fps.
Just my two cents anyway-- I'm not very experienced with game programming either in general, so others on the forum will probably have better advice!
-
That's great help iv shortened the array which has fixed it so il just add to it till it breaks again to test its limit anything after il load into the next level
-
@Spacemario i noticed i placed some code in the wrong place causing slow down but all good now my level array now is nearly 200 columns long....
-
@Spacemario iv hit the limit switch just shuts down ....
What's the best way to load into another loop? -
can someone help and tell me ?
at the moment my level array
level = [( 0, 0, 0, 0 ),
( 0, 0, 0, 0 )
]but mine is 200 in length causing my switch to crash as it is drawing to much between update()
so at the minute you get about 30 seconds gameplay till you reach where im up to ...
im running 3 level arrays one for collision background and foreground so the switch is actually reading 3 lots of level arrays a total of 600+can i do this and will it work?
level = [( 0, 0, 0, 0 ),
( 0, 0, 0, 0 ),
update()
( 0, 0, 0, 0 ),
( 0, 0, 0, 0 )
] -
It sounds like you're drawing the whole level all the time. Could you adapt your code to only draw the columns that are on the screen, so it's not drawing too much between each update?
-
@Richard that would be a great fix ! but how would i accomplish this im still learning
-
i cant work on this till later but have an idea
if i structure my next part of my array as level.2 so on
i can add at the bottom of the for loop to then level.2 then level.3
... please work -
You know where your player is in the level. So which column they're on, then you know how many columns can be seen each side of the player, for the purposes of an example well assume 5 to the left and 10 to the right. Then you change your loop that was something like
for i = 0 to len (level) loop // Draw level repeat
With
for i = playerColumn - 5 to playerColumn + 10 loop // Draw level repeat
-
ooooh thankyou il give this a go fingers crossed
-
Ah perfect! This is what I was talking about earlier when I said about using a "window" in your array, rather than the whole array. I hope this works for you (apologies, I've not been able to look at your code).
-
@pobtastic no problem mate hoping this works should do the trick
-
I would think that should work. At the end of the day you know that you can draw a screens worth because it's been working OK up until some point.
-
My code is
For row = 0 to len( level ) loop
For col = 0 to len ( level[0] ) loop
If level[row][col] >= 0 then
X = col * size
Y = ( row + levelOffset ) * tsize
drawSheet(..........Confused on how to do this
-
Think iv hit a serious bug switch keeps crashing on the coding screen so even if I do manage to fix the loop every time I try to extend the level array it will crash
-
@waldron You're trying to draw only what is going to be visible, yes? So what you need is to keep track of the position within the level, then work with that. If you're making a horizontal scroller where it scrolls from left to right (no vertical scrolling), the left-most position would be position 0. The width of what the camera displays extends from the left of the screen to the right of the screen. Assume a display dimension of 400x256 pixels. If your tiles are 16x16 pixels, then you can fill the display with 25x16 tiles (assume 26x16 for a horizontal scroller because when you scroll, you'll be moving in pixels where tiles can crop on the sides, so technically one extra column to render).
So, the camera displays from position 0 to 400 (in pixels). Than means you'll be drawing the tiles columns from columns 0 to column 25 (divide positions by the width of the tile size). If you move right by 16 pixels, then it displays from position 16 to 416. If you convert that to tile position (divide by 16), that could make for columns 1 and columns 26. So you'd draw from column 1 to column 26. Now, what if you move right only an extra 8 pixels? Well, that is now position 24 to position 424. Dividing by 16 to get the tile range results in column 1.5 and 26.5.
So how does one work with that? Simple. Take the whole number position (1 and 26), and draw from the left column to the right. This will result in the right side having an empty portion because you only compensated for 25 columns. This is why you should draw one additional column beyond the display range. This sort of design can allow you to have a large area, yet only have to deal with displaying a small section. You're basically cropping out everything except the area that you need to work with.
-
@Discostew martins working on it, i understand what needs to happen but to make it happen im at a loss
-
basically arrtived at this:
drawX = floor(playerX / tsize) drawLevelChunk(level, drawX, scale) drawLevelChunk(levelb, drawX, scale) /** * Draw a chunk of a level (nothing to do with draw * windows, this function exists because the levels * are so big the same code is called multiple times) * _l - level array * _x - current player position in tiles * _s - scale */ function drawLevelChunk(_l, _x, _s) _low = _x - 7 _high = _x + 22 _max = len(_l[0]) for row = 0 to len(_l) loop for col = clamp(_low, 0, _x) to clamp(_high, _x, _max) loop if _l[row][col] >= 0 then x = col * tsize // tsize is defined outside y = (row * levelOffset) * tsize drawSheet(tileSheet, tiles[_l[row][cold]], x - screenX, y, _s) endif repeat repeat return void
The -7 and +22 are the values that are currently enough to smoothly draw "just enough"
-
Oh, PS: Not overly surprisingly after this the game is back up to 60fps and CPU is down to around 4% so plenty of headroom.
The editor is still choking when it comes to displaying a 200 x 9 array so I'll taise that as a big if it is not already.
-
After messing with a single array and extending it, it still crashes