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

GameMaker How do I do a list within a list?

L

linx

Guest
How can I do a list that would go along with a certain item in another list? Like, say I have a list that has the items male and female in it and I get it to choose male and I have another list with male names in it. How can I get it to choose an item from the male names list and if picks female to choose one from the female names list if that is selected?
 

roozilla

Member
So you are thinking of it like this?

List1: [Male, Female]
List2: [Francis, Mary, Joe]

You can do it many ways for example,
List1: [Francis, Joe] //List1 is always male
List2: [Mary, Liz] // List2 is always female

List1: [Male, Female] // You could have this or just List2
List2: [{Francis, 0}, {Mary, 1}, {Joe, 0}] // 0 is male 1 is female

If it is more about linking then you just need to add logic using variables or flags. If they are set to a certain value display values from a specific list
 

FrostyCat

Redemption Seeker
You do it by nesting data structures.
Code:
var male_names = ds_list_create();
ds_list_add(male_names, "Aaron", "Bob", "Charles");
var female_names = ds_list_create();
ds_list_add(female_names, "Alice", "Barbara", "Caitlyn");

global.names = ds_list_create();
ds_list_add(global.names, male_names, female_names);
ds_list_mark_as_list(global.names, 0);
ds_list_mark_as_list(global.names, 1);
Then you can just dig down twice to get at the result:
Code:
var female_list = global.names[| 1];
name = female_list[| 2]; //Caitlyn
Or if you use this script:
Code:
name = json_get(global.names, 1, 2); //Caitlyn
 

Bentley

Member
You do it by nesting data structures.
Code:
var male_names = ds_list_create();
ds_list_add(male_names, "Aaron", "Bob", "Charles");
var female_names = ds_list_create();
ds_list_add(female_names, "Alice", "Barbara", "Caitlyn");

global.names = ds_list_create();
ds_list_add(global.names, male_names, female_names);
ds_list_mark_as_list(global.names, 0);
ds_list_mark_as_list(global.names, 1);
Then you can just dig down twice to get at the result:
Code:
var female_list = global.names[| 1];
name = female_list[| 2]; //Caitlyn
Or if you use this script:
Code:
name = json_get(global.names, 1, 2); //Caitlyn
Never new about ds_list_mark_as list. ds_list_destroy(global.names) destroys its nested lists. Very cool.
 
L

linx

Guest
Now where would the list with male and female come in? I'm sorry I should have expanded more on what I'm trying to accomplish. I'm trying to make an app that will randomize or choose items from a list. Like if it's chooses male I want it to also pick a list of male names. I hope this is a little more clearer.
 

FrostyCat

Redemption Seeker
Now where would the list with male and female come in? I'm sorry I should have expanded more on what I'm trying to accomplish. I'm trying to make an app that will randomize or choose items from a list. Like if it's chooses male I want it to also pick a list of male names. I hope this is a little more clearer.
You should not have to ask anyone else this question if you did your homework on data structures.

Assuming that you set up the global list at the beginning of the game (as described in post #3) and the user's choice is stored in choice, the fragment below pulls a random result into result for you to work on later:
Code:
var source_list;
if (choice == "male") {
  source_list = global.names[| 0];
} else {
  source_list = global.names[| 1];
}
result = source_list[| irandom(ds_list_size(source_list)-1)];
 
Top