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

GM:S 1.4 Random Rooms Only Once Issues

Discussion in 'Programming' started by hacktually, Oct 20, 2018.

  1. hacktually

    hacktually Member

    Joined:
    Sep 17, 2017
    Posts:
    6
    Hi everybody,

    I'm trying to go to create a random list of rooms based on what the player selects on a menu. The list of rooms could potentially be huge (200+) since there are various options that could be selected. I've tried messing around with ds_lists for this, but I haven't had any luck. Here's what I tried on a small scale for testing once the player hits continue on the menu:

    Code:
    testList = ds_list_create();
    
    if Spells = 1
        {
        ds_list_add(testList, "2");
        ds_list_add(testList, "3");
        ds_list_add(testList, "4");
        ds_list_add(testList, "5");
        }
    
    if !ds_list_empty(testList)
        {
        randomRoom = random_range(1, ds_list_size(testList));
        ds_list_delete(testList, randomRoom);
        room_goto(randomRoom);
        }
    The idea here is to add rooms to the list if certain variables = 1 (these rooms will be used the rest of the game until there are no rooms left), pick a random number, delete that entry to shrink the list size and keep rooms from showing up twice, and then go to that room. Can anyone explain where I'm going wrong? Is there a more efficient way to do this? This is the only thing holding up my project so any help would be appreciated.
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,221
    Ds lists start at index 0 instead of 1, so randomRoom should be irandom(ds_list_size(testList)-1)

    If you want to use a string to load a room it wont work because rooms aren't strings. You should directly write the room names into the list and not "1", "2" etc.

    in your code, randomRoom is a position somewhere in the list and not the actual entry.
    ->
    Code:
    room_goto(testList[| randomRoom]);
    
    Make sure you save out the room before removing it from the list.

    Example
    Code:
    testList = ds_list_create();
    
    if (Spells == 1)
    {
       ds_list_add(testList, room_stage_1);
       ds_list_add(testList, room_stage_hall_3);
       ds_list_add(testList, room_dungeon_4);
       ds_list_add(testList, room_stairs_2);
    }
    
    if (!ds_list_empty(testList))
    {
       var randomRoom = irandom(ds_list_size(testList) - 1);
       var nextRoom = testList[| randomRoom];
       ds_list_delete(testList, randomRoom);
    
       room_goto(nextRoom);
    }
    
     
  3. hacktually

    hacktually Member

    Joined:
    Sep 17, 2017
    Posts:
    6
    Thank you so much for the help and cleaning up my messy code! I really appreciate it, and now I can get back to making stuff :)
     
  4. hacktually

    hacktually Member

    Joined:
    Sep 17, 2017
    Posts:
    6
    It appears i've marked this solved pre-maturely. It does a random room, but now when in another room I've added only this code to a button:

    Code:
    if (!ds_list_empty(testList))
    {
       var randomRoom = irandom(ds_list_size(testList) - 1);
       var nextRoom = testList[| randomRoom];
       ds_list_delete(testList, randomRoom);
    
       room_goto(nextRoom);
    }
    The player always goes to a new room via a button, but for some reason the code above isn't sending them to a new room. Is the list not global and known from room to room? I see that you said to "save out the list", but how is that accomplished?

    As another edit, the goal is HTML5 with this...in case that matters.
     
    Last edited: Oct 20, 2018
  5. Danei

    Danei Member

    Joined:
    Mar 23, 2018
    Posts:
    278
    Is the object containing this code persistent? If not, the variable you use to access the list (testList in this case) is gone and you no longer have any variables pointing to the list. You can just make it persistent, or declare the variable pointing to the list to be global (if you do the latter, make sure you only create the list once!).

    If the button just does nothing, it sounds like it's checking the list and it's empty, so make sure you're not just making a new list with a new testList variable in the new room, because that list would be empty, while the old list is still out there taking up memory but not accessible in any way I know of. Also hopefully you already know this but always destroy your data structures in clean-up scripts.
     
  6. hacktually

    hacktually Member

    Joined:
    Sep 17, 2017
    Posts:
    6
    Ok making the list a global variable has solved that part! However, I need the data list to be available throughout the game as it always decides the next room until the end. When am I supposed to destroy the data structure if I pretty much always need it?
     
  7. Danei

    Danei Member

    Joined:
    Mar 23, 2018
    Posts:
    278
    For data structures that are going to be needed for the entire game it probably doesn't matter. When the program ends the structures are, I would assume, automatically destroyed. But personally, I would still destroy them in the clean-up event of a game controller object. It's good to get into the habit of always destroying them just so you don't forget and have to track down memory leaks later.
     

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