• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Nicer way to shift everything to the other side of the room

NeoShade

Member
Hi all,

Sorry for the vague title, this doesn't summarise very well.

This is one of those instances where I have code that works perfectly well, but it feels like it could be achieved in a better way. If we can't come up with anything better then it's no skin off my nose and I'll keep on using this method, but I just wanted to put it out ther in case anybody had a super simple or clean approach that I didn't think of.

I'm working on a chunkloading system that will provide effectively infinite worlds. To achieve this I have chunk objects that are generated around the player. The chunk objects hold a DS Grid of terrain values, which are then placed in the room as tiles. Because I'm using tiles, I need to keep the player and all the chunks within the bounds of the room, so my aproach so far is when the player reaches a certain point in the room (1/3 of the room size on each size) the player and all visible chunks are shifted to the opposite side of the room where the chunks proceed to relay their tiles.

This is the code in the step event of my player object (this only fires when the player is aligned to the grid, so as to not misalign tiles):

GML:
    // Request a chunk shift when approaching room edges
    if (x < room_width / 3) {
        x += ((room_width / 3) div TILE_SIZE) * TILE_SIZE;
        targ_x += ((room_width / 3) div TILE_SIZE) * TILE_SIZE;
        with (Ob_Chunk) { x += ((room_width / 3) div TILE_SIZE) * TILE_SIZE; }
    }
    if (x > room_width / 3 * 2) {
        x -= ((room_width / 3) div TILE_SIZE) * TILE_SIZE;
        targ_x -= ((room_width / 3) div TILE_SIZE) * TILE_SIZE;
        with (Ob_Chunk) { x -= ((room_width / 3) div TILE_SIZE) * TILE_SIZE; }
    }
    if (y < room_height / 3) {
        y += ((room_height / 3) div TILE_SIZE) * TILE_SIZE;
        targ_y += ((room_height / 3) div TILE_SIZE) * TILE_SIZE;
        with (Ob_Chunk) { y += ((room_height / 3) div TILE_SIZE) * TILE_SIZE; }
    }
    if (y > room_height / 3 * 2) {
        y -= ((room_height / 3) div TILE_SIZE) * TILE_SIZE;
        targ_y -= ((room_height / 3) div TILE_SIZE) * TILE_SIZE;
        with (Ob_Chunk) { y -= ((room_height / 3) div TILE_SIZE) * TILE_SIZE; }
    }
Please let me know what you think, as I'm keen to hear your input.
 
Top