Room Alignment

Discussion in 'Programming' started by Lemonz, Oct 12, 2019 at 5:00 AM.

  1. Lemonz

    Lemonz Member

    Joined:
    Aug 1, 2016
    Posts:
    18
    Is there a way to make an object stay aligned to the grid of the room? I am making a small slider game and can't figure out what functions I should use to maintain. Here is what is happening. what I am currently doing is letting the wall object stop the player by using Solid on the object settings.

     
  2. obscene

    obscene Member

    Joined:
    Jun 21, 2016
    Posts:
    2,432
    Divide the x or y by the number of rows or columns, floor the result and then multiply it by the number of rows or columns.

    This will make it snap, but if you want to preserve motion between those spaces you'll have to do some fancy coding where you press a key, calculate the destination as I described above and move to that point before accepting another command.
     
    Lemonz likes this.
  3. Lemonz

    Lemonz Member

    Joined:
    Aug 1, 2016
    Posts:
    18
    I'm not sure what you mean. Could you give me an example of a room that is 640 by 480, and 32 by 32 for the grid size?

    what functions should I be using? I tried using move_snap, but didn't see any results. do I need a dsgrid?

    grids are a new thing for me, and I feel pretty confused about the whole thing.


    Edit: I tried this and it works a lot better. I removed Solid from Wall Object and added this to the collision with the player to the wall.

    canmove = true;
    move_snap(32, 32);
    place_snapped(32,32);

    The only problem I am having now is that the player will get stuck in the object some times. If I add solid to the wall it doesn't work at all.

    EDIT 2! I figured it out! The above code did work for me. The issue was that when I first made the rooms my grids where 16 be 16. one of the blocks was not on the grid correctly. after I fixed it everything worked fine.

    The issue I have now though is why did it work? I am not sure what move_snap and place_snap are actually accomplishing. I just used them because they sounded like they related to a grid in some way. I don't want to blindly use what worked for me, I want to understand why so I can learn from the result to make future projects better.
     
    Last edited: Oct 12, 2019 at 2:18 PM
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    move_snap moves the affected instance to the nearest position that is evenly divisible with the snapping sizes you provide (x and y are snapped invidually). place_snapped checks if the current position is snapped to a such grid and returns true or false... so it's completely useless in your code, because you don't use it in an if statement.
     
    Lemonz and ParodyKnaveBob like this.
  5. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    537
    Howdy, Lemonz,

    I'm glad you're experimenting and wanting to know why things do what they do. $:^ ]

    However, I believe you might be able to solve this without manually snapping the moving instance to a grid. It seems to me that what you've done is told the moving instance to set a pleasant-feeling speed when a player presses a key/button/etc., but you didn't make that pleasant-feeling speed divisible by your grid cell size. If this is the case, the simple fix would be to adjust the speed. You have 32x32 cells? Set hspeed = 8 and boom, it now takes 4 frames to be exactly inside the cell to the right. Of course, I don't condone "magic numbers," thus I would set it up more akin to this:

    Code:
    /// rm_init - very first and totally blank room - Room Creation Code
    enum CELL {
        _D = 32, // I'm abbreviating "distance" as "d" since it's neither "w" nor "h" specifically
        _D_MOVE = CELL._D div 4; // I normally use CELL._D >> 2, but I don't know if you're familiar with bitwise operations
    }
    room_goto_next();
    
    Code:
    /// Move Right
    hspeed = CELL._D_MOVE;
    
    Then of course stop it like you do already in the Collision Event with the solid instances, and you should be good to go.

    I hope this helps, $:^ J
     
    Lemonz likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice