G
Giammarco Agazzotti
Guest
Hi everyone.
I am working on a randomly generated platformer with Game Maker 2 and as long as we talk about map generation it is doing pretty well.
The problem comes when I have to spawn enemies around the map.
My logic is:
The bug when choosing a random value from the list containing all coordinates, because the value is still the same (I manually checked the list and I can say that it contains all different values). Here is the code:
Some help would be appreciated.
Thank you all guys!
EDIT (Solved):
After googling I found the bug, and it is related to the list of arrays.
It appears that for some reasons it loses the reference and so copies it. The result is a list with every item equal to the last one added.
Here is the link to the thread: https://www.reddit.com/r/gamemaker/comments/3qr1qz/making_a_list_of_arrays_doesnt_store_the_arrays/
Thank as always for your support guys, hope this can help someone else too!
I am working on a randomly generated platformer with Game Maker 2 and as long as we talk about map generation it is doing pretty well.
The problem comes when I have to spawn enemies around the map.
My logic is:
- Create a list containing all the coordinates of empty spaces.
- Then, for each enemy which will be spawned, extract a random coordinate (array containing x and y)
- And create an instance there.
The bug when choosing a random value from the list containing all coordinates, because the value is still the same (I manually checked the list and I can say that it contains all different values). Here is the code:
Code:
// Add tiles
for (var yy = 0; yy < height; yy++) {
for (var xx = 0; xx < width; xx++) {
if (grid[# xx, yy] == FLOOR) {
// Draw floor
instance_create_depth(xx * CELL_WIDTH, yy * CELL_HEIGHT, 0, obj_Background);
if (not created) {
instance_create_layer(xx * CELL_WIDTH, yy * CELL_HEIGHT, layer_get_id("Instances"), obj_MagicStick);
created = true;
}
// Add current empty space to a list, so we can randomly place wizards later
couple[1] = xx;
couple[2] = yy;
empty_spaces = ds_list_add(empty_spaces, couple);
} else if (grid[# xx, yy] == WALL) {
// Draw wall
instance_create_depth(xx * CELL_WIDTH, yy * CELL_HEIGHT, 0, obj_Environment);
}
}
}
// Add wizards
for (var i = 0; i < wizards; i++) {
// Extract random space
pos = irandom_range(1, ds_list_size(empty_spaces));
//show_debug_message("pos " + string(pos));
//THE FOLLOWING LINE IS THE PROBLEM I THINK
space = ds_list_find_value(empty_spaces, pos);
show_debug_message("[" + string(space[1]) + ", " + string(space[2]) + "]");
// Spawn wizard
instance_create_layer(space[1] * CELL_WIDTH, space[2] * CELL_HEIGHT, layer_get_id("Instances"), obj_Wizard);
}
Thank you all guys!
EDIT (Solved):
After googling I found the bug, and it is related to the list of arrays.
It appears that for some reasons it loses the reference and so copies it. The result is a list with every item equal to the last one added.
Here is the link to the thread: https://www.reddit.com/r/gamemaker/comments/3qr1qz/making_a_list_of_arrays_doesnt_store_the_arrays/
Thank as always for your support guys, hope this can help someone else too!
Last edited by a moderator: