• 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!

Legacy GM Random Rooms Only Once Issues

H

hacktually

Guest
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.
 

Simon Gust

Member
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);
}
 
H

hacktually

Guest
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 :)
 
H

hacktually

Guest
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 by a moderator:
D

Danei

Guest
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.
 
H

hacktually

Guest
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?
 
D

Danei

Guest
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.
 
Top