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:
    19
    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:
    415
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,780
    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:
    6,780
    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:
    415

    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:
    6,780
    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:
    19
    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,580
    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:
    6,780
    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:
    19
    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:
    499
    What GameMaker version do you use?
     

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