1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

How do I go to a room 10 rooms away?

Discussion in 'Programming' started by Ethan Uffelman, Sep 7, 2019.

  1. Ethan Uffelman

    Ethan Uffelman Member

    Joined:
    Jun 3, 2019
    Posts:
    25
    I'm making a maze game, and I'm trying to make things easier on myself. I don't want to have to say "goto room ____," I want to essentially go to a room that is ten rooms away from my current room in the list, so that I don't have to create a million different objects just to go to each room. Is there a way to do that?
     
  2. MilesThatch

    MilesThatch Member

    Joined:
    Jul 12, 2016
    Posts:
    423
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    room = room+10

    Or

    room_goto(room+10)

    This will work fine in Studio. In GM8 it takes some finessing.
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    I neglected this part. Whoops.

    room = roomlist[|ds_list_find_index(roomlist, room) + 10];

    Or if you're not in studio

    room = ds_list_find_value(roomlist, ds_list_find_index(roomlist, room) + 10);

    You can skip the whole ds_list_find_index if you store the index in a variable and then just update that variable as you move around.

    room_position += 10;
    room = roomlist[|room_position];
     
  5. MilesThatch

    MilesThatch Member

    Joined:
    Jul 12, 2016
    Posts:
    423

    Does game maker arrange rooms in a numerical manner? So if you created room_1, room_2 and room_3, and move 3 above 2, does it also get it's index changed?

    room_1 id of 1
    room_3 id of 2
    room_2 id of 3

    Because I think all resources share the same resource ID and changing the order doesn't change the id?
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    Changing the order changes the ID.

    And in your example there, unless you have another room that you left out, it should be

    room_1 = 0
    room_3 = 1
    room_2 = 2
     
  7. Ethan Uffelman

    Ethan Uffelman Member

    Joined:
    Jun 3, 2019
    Posts:
    25
    Shoot, I forgot to mention that I'm in DnD. I used the Go To Room action and put in room+10 and that worked fine, so thanks! The only problem is now I want to go to a room ten rooms behind where my current room is. I cant say room+10 or room+-10, so what should I do?
     
  8. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,760
    room+-10 should work fine... does it not? (Could be shortened to room-10, though.)

    Either way, I don't recommend doing this, as it relies entirely on the order of the rooms in the room editor.
    If the order is ever changed - accidentally or intentionally - this system will break in the cases that are affected by the change.
    If the way IDs are assigned to rooms is ever changed, it will also break.

    If your rooms are laid out in a straight-forward manner, where you can only ever go back or forward, and there are no more than two connections in between rooms (or one for the starting and end room), I suggest to store your room IDs in an array (or a list, as suggested before). This will be a data structure that you have full control over, and there is no chance that any internal changes will break it. In fact, the only one that will be able to break it is you - whether you consider that a good thing or a bad thing is something I'll leave up to you to decide. ;D

    Sort of like this: (treat it like variable assignments in DnD - arrays are no different from variables in this regard)
    Code:
    rooms[0] = room_1;
    rooms[1] = room_2;
    rooms[2] = room_3;
    ...
    ... where room_1/2/3 are the actual names of your rooms, in order. Also keep track of where you currently are - if you're in room_2, save 1 in a variable (as it's in array cell 1). You can then skip ahead by going to the room stored in the array cell equal to the current room's number + 10.
     
  9. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    I don't usually fear Yoyo changing internal structure that drastically, especially when it's been that way for so many years now. lol Although I do admit the method I first posted has some drawbacks compared to the method in my other post (fear factors aside).

    Dumping the rooms into a ds_list or array (like Tsuriko did) is actually quite flexible at the cost of somewhat duplicated data. First off, it lets you randomize the layout of the rooms. If you base room layout on the resource tree, you can't randomize room order -- the order of the rooms is the order they appear in the resource tree. Another advantage is world wrapping. You take the list/array index of whatever room you're in (again, pointing back to Tsuriko's post) and add or subtract to it, then get the modulo of that index by the number of rooms in the list.
    Code:
    room_index = (room_index + 1) mod ds_list_size(rooms_map);
    Using the resource tree, if you try to go to a room that is less than 0 or higher than the number of rooms you have, you'll crash the game, so you'd need to verify that the player doesn't try to warp beyond the limits of the room. Using the list/array method, you can easily find the first or last room, or wrap around. While you can have world wrapping using just the resource tree, this prevents you from having any miscellaneous rooms, like a main menu, options menu, or whatever else. With the list/array, only the game levels would be included.

    With a 2D array or ds_grid, you can easily make sprawling world maps. Adding or subtracting the first index moves the player left or right on the world, while adding or subtracting the second index moves the player up or down on the world.


    Update: What about
    Code:
    repeat 10 room_goto_next()
    ?
     
    ParodyKnaveBob likes this.
  10. Ethan Uffelman

    Ethan Uffelman Member

    Joined:
    Jun 3, 2019
    Posts:
    25
    I am trying to make a grid layout. How do I use the code you suggested in DnD?
     
  11. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    537
    What GameMaker version do you use?
     
  12. Ethan Uffelman

    Ethan Uffelman Member

    Joined:
    Jun 3, 2019
    Posts:
    25
    I'm using Game Maker Studio 2 if that's what you mean, or do you need to know the exact update version?
     
  13. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    537
    Yes, you answered the question, thanks. I'm unfamiliar with GMS2 and its much more robust DnD system. It might be doable there, but if not directly, then that's exactly why DnD has always included "Execute Code/Script" actions for writing GML to perform nuanced commands not otherwise found in the DnD.
     
  14. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    Create Grid
    Set the width to the number of rooms horizontally your world map will be.
    Set the height to the number of rooms vertically your world map will be.
    Specify a variable to save the ID of the grid in. If the variable hasn't been created yet, it will be created here.

    Set Grid Value
    Specify the variable you created above.
    The X is the horizontal room coordinate.
    The Y is the vertical room coordinate.
    The value is the index of the room you want to save in that position.

    Get Grid Value
    Specify the variable you created above.
    The X is the horizontal room coordinate you're going to.
    The Y is the vertical room coordinate you're going to.
    Specify a new variable to hold the ID of the room you're trying to go to.
     
    ParodyKnaveBob likes this.
  15. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,926
    One method nobody has mentioned so far is: have a single object that changes rooms, based on a variable called my_room. Then use Instance Creation Code (right click --> creation code) to set this variable from the room editor, so each object can have a different value for this variable. (GMS2 also added a local variable editor, I believe)

    The stable way to go 10 rooms forward would be:
    Code:
    r = room
    repeat(10){
        r = room_next(r)
    }
    if(room_exists(r)){
       room_goto(r)
    }
    
     
    ParodyKnaveBob likes this.
  16. Ethan Uffelman

    Ethan Uffelman Member

    Joined:
    Jun 3, 2019
    Posts:
    25
    TheoAegis Should I create this in a Game Start event?
     
  17. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    you can. You just need to remember that the game start event is only run at the very very beginning of the game. This means whatever object you put it in needs to be created at the very very start of the game.

    You create the grid and you fill it at the start, you read from the grid when you try to move to a new room. you are also going to want two variables to keep track of your position in the grid. For example, if room4 is at (3,5) in the grid, you need a variable to hold that 3 and another variable to hold that 5.
     
  18. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,926
    I find game start events to be too unreliable (what if you decide to add a new title screen / intro room last second and suddenly EVERYTHING stops working?!?!) since they're only run in the first room, I prefer having it run in a Create event in a setup room that you will only visit once so you can be sure it's run even if you reorder rooms later.
     
    ParodyKnaveBob likes this.
  19. Ethan Uffelman

    Ethan Uffelman Member

    Joined:
    Jun 3, 2019
    Posts:
    25
    How will it know which rooms I want to put in the grid and where to put them?
     
  20. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    You tell it what rooms to put in the grid. It's your game, you need to code it yourself.

    map[#0]=stage1;
    map[#1]=stage3;
    map[#2]=stage4;
    map[#3]=stage2;

    Edit: oh wait, DnD. lol But same idea. You can use Set Grid Value or Change Variable (using the # accessor like I did) to set the entries in the grid.
     
  21. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,926
    It won't, but the topic title is "How do I go to a room 10 rooms away?". I assumed you already know the room 10 rooms forward is where you want to go.
     
  22. Ethan Uffelman

    Ethan Uffelman Member

    Joined:
    Jun 3, 2019
    Posts:
    25
    So do I set the grid value in the room's actions?
     
  23. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    You set the grid's values wherever you want, but they need to be set before the actual gameplay starts. If the layout of the rooms will never change from playthrough-to-playthrough, then set the grid in the very first room (the one at the very top of the resource tree) either inside the room's actions or inside an object placed in that room. I don't know if you can use DnD in the room actions, though; you couldn't in older versions of GM, so you'd have to use an object if that's the case.
     
    ParodyKnaveBob likes this.
  24. Ethan Uffelman

    Ethan Uffelman Member

    Joined:
    Jun 3, 2019
    Posts:
    25
    How will the game know what grid values are assigned to what room if I just put them wherever? Is there a way to specify a room in the SetGridValue action? Sorry I don't have my basics down and if I'm being difficult. I've worked with GameMaker in the past, but GameMakerStudio2 has proven more difficult.
     
  25. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,694
    Putting room IDs into a grid is not new to GMS 2. If you don't recognize the technique, your grasp of basic programming needs work.

    At the beginning of your game, use the Create Grid action to create a grid, then use a series of Set Grid Value actions to set grid cells to room IDs like this:
    • Grid: global.roomgrid
    • X: 0
    • Y: 0
    • Value: rm_player_home
    Then you should keep track of your current position in the grid in two global variables, say global.roomX and global.roomY. Then if you want to go to another room listed in the grid, set these variables to the new value, then use the Go To Room action with a grid accessor:
    • Room: global.roomgrid[# global.roomX, global.roomY]
     
  26. Ethan Uffelman

    Ethan Uffelman Member

    Joined:
    Jun 3, 2019
    Posts:
    25
    I'm just going to say a few things here. The reason that Game Maker Studio 2 was so difficult was mainly the layout being so different. I didn't know where things were or how to do certain things I could in Game Maker. I took a class on Game Maker, and I'm currently taking a programming class, so I really hope that helps. Long story short, I'm setting aside my current project in favor of a much larger one. My original goal was to create 3 or 4 games by the end of June, since I wanted to assemble a portfolio for collages, which is why I've been asking so many questions instead of figuring it out myself. However, I've decided to put quality over quantity. I'll be looking up tutorials from now on, since I don't want to waste your time with such obvious questions as, "how do I create a grid." I guess I just don't learn very well like that. Thank you so much for all of your help!
     
  27. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,123
    Personally, I say go for quantity over quality. You don't know what you're doing yet, so quality is pretty much on a much higher plateau than you're currently capable of reaching. On the other hand, going for quantity will teach you the things you need to know in order to make quality games. Well, there's also the possibility nothing you make will ever be quality, but let's just assume you're destined to be the next Shigeru Miyamoto rather than the next Vince Perri. Going for quantity, you essentially give yourself a series of much lower plateaus up which to climb until eventually that quality gaming plateau will be within your reach. Granted, Nintendo had multiple programmers and development teams, but just look at their gaming history. In the case of their first hit Donkey Kong, Miyamoto and Gunpei Yokoi were responsible for coming up with ideas and planning the game, but not coding it. A lot of the ideas they came up with for the game -- like see-saws -- were dismissed because the coders couldn't figure out how to pull such things off, although the following year they did include see-saws in a different game; nowadays, see-saw mechanics are relatively commonplace. Look at the evolution of the Super Mario Bros. games. It started off as a single-screen game with very, very simple mechanics; it was similar to Donkey Kong, but different enough to be recognized as its own thing. Then came Super Mario Bros., which had horizontally scrolling stages, branching paths, swimming stages, the maligned springboards...and really crummy graphics. Super Mario Bros. 3 had full-range scrolling stages, improved spring blocks, an overworld map, multiple bosses, improved swimming, stowed power-ups, and vastly improved graphics and audio. Super Mario World put the whole NES library to shame by building off everything they had tried and learned.
     

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