Bros Before Giraffes
Member
This question is a bit complicated.
Basically, I'm trying to make a game where you can explore a randomly generated galaxy, kind of like No Man's Sky, only in 2D and it's more turn based strategy instead of real-time.
Currently, I have the game randomly place 200 star systems around a map.
I'm storing the data for each system in an array so I can reference certain properties in other places.
There are 5 variables for each system that define their name, position, and whether or not you have discovered them.
The 6th variable is the random seed dedicated to the system. When I assign the seed to a system, it will load this seed and generate the same planets in that system every time you go there.
All of that happens at the start of the game before anything else.
After it generates the systems, another object places them in the room using this code:
Now, the map screen looks like this:
That arrow represents your ship and you can move around by clicking.
When you go to a system, it takes you to a different room where it generates the planets inside the system. Before taking you there, it sets a variable global.current_system to the variable sys_id of that system. This way, it will be able to find the correct random seed in the array global.systems.
Here's the code I'm using to generate the planets. Right now I'm not really sure if I need to use another array for the planets, but whatever.
It uses dir to basically pick a random rotation around the sun so the planets don't all spawn in a straight line.
So after generating the planets, the room would look something like these:
So each star system generates a different arrangement of planets, and if you leave one and come back it will generate the same planets it had before.
Here's where my problem starts.
Most of the time, it works exactly how I want it to. But sometimes it generates the same planets no matter which system I go to. What happens is I'll run the game, and it will generate everything as expected. After closing and running the game again, every system will generate the same arrangement of planets. After running and ending the game a few more times or clearing the asset compiler cache, it will go back to normal, but it never stays normal.
Every time this happens it uses the same seed: 2147483648.
I thought maybe the game was saving the same seed to the array global.systems in the beginning where it generates the system data, so I displayed a list of the seeds on the screen while the game was generating the same system over and over, but all the seeds were different like I expected.
I also considered the possibility that the game thinks you're trying to go to the same system, so it continues to pick the same seed. But if that's the case, then why would it always pick 2147483648?.
The most frustrating thing about this is that the game works as expected half the time and the other half it does not.
So do any of you have any ideas on why this might be happening? I hope I was clear on everything and provided enough information.
And feel free to constructively criticize my coding techniques. I don't call myself a good programmer and there are probably way simpler ways to do this!
Basically, I'm trying to make a game where you can explore a randomly generated galaxy, kind of like No Man's Sky, only in 2D and it's more turn based strategy instead of real-time.
Currently, I have the game randomly place 200 star systems around a map.
I'm storing the data for each system in an array so I can reference certain properties in other places.
There are 5 variables for each system that define their name, position, and whether or not you have discovered them.
Code:
global.map_seed = random_get_seed();
/*
Systems Array
0 - System Name
1 - x
2 - y
3 - visited
4 - seen
5 - System seed
*/
//Generate Galaxy (place systems)
for (i=0 ; i<200 ; i++)
{
global.systems[i,0]="System "+string(i);
global.systems[i,1]=round(random_range(64,4096-64));
global.systems[i,2]=round(random_range(64,4096-64));
global.systems[i,3]=false;
global.systems[i,4]=true;
}
Code:
//Set System Generation Seeds
for (i=0 ; i<200 ; i++)
{
randomize();
global.systems[i,5]=random_get_seed();
}
random_set_seed(global.map_seed);
After it generates the systems, another object places them in the room using this code:
Code:
//Place Systems
for (i=0 ; i<200 ; i++)
{
sys = instance_create(global.systems[i,1],global.systems[i,2],obj_icon_system);
sys.name=global.systems[i,0];
sys.discovered=global.systems[i,3];
sys.sys_id = i;
sys.seen=global.systems[i,4];
}
That arrow represents your ship and you can move around by clicking.
Here's the code I'm using to generate the planets. Right now I'm not really sure if I need to use another array for the planets, but whatever.
Code:
//Get system Seed
random_set_seed(global.systems[global.current_system,5]);
//set number of planets
planet_num = choose(1,2,2,3,3,3,4,4);
/*
Planet Properties
0 - Name
1 - Type 0=terrestrial 1=gas giant
2 - Landscape 0=forest 1=jungle 2=desert 3=ice 4=volcanic
3 - radius
*/
//Generate planets
for (i=0 ; i<planet_num ; i++)
{
planets[i,0] = "Planet "+string(global.current_system)+string(i); //name
planets[i,1] = choose(0,0); //type
if (planets[i,1]==0) //landscape
planets[i,2]=choose(0,1,2,3,4);
planets[i,3]=112+(76*i);
}
//Place planets
for (i=0 ; i<planet_num ; i++)
{
n = instance_create(obj_sun.x,obj_sun.y,obj_planet);
n.radius=planets[i,3];
n.orbit_speed=choose(0.05,0.1,0.12);
n.dir=round(random(360));
n.x+=lengthdir_x(n.radius,n.dir);
n.y+=lengthdir_y(n.radius,n.dir);
n.image_speed=0;
//choose image
if (planets[i,1]==0)
n.image_index=planets[i,2];
}
So after generating the planets, the room would look something like these:
Here's where my problem starts.
Most of the time, it works exactly how I want it to. But sometimes it generates the same planets no matter which system I go to. What happens is I'll run the game, and it will generate everything as expected. After closing and running the game again, every system will generate the same arrangement of planets. After running and ending the game a few more times or clearing the asset compiler cache, it will go back to normal, but it never stays normal.
Every time this happens it uses the same seed: 2147483648.
I thought maybe the game was saving the same seed to the array global.systems in the beginning where it generates the system data, so I displayed a list of the seeds on the screen while the game was generating the same system over and over, but all the seeds were different like I expected.
I also considered the possibility that the game thinks you're trying to go to the same system, so it continues to pick the same seed. But if that's the case, then why would it always pick 2147483648?.
The most frustrating thing about this is that the game works as expected half the time and the other half it does not.
So do any of you have any ideas on why this might be happening? I hope I was clear on everything and provided enough information.
And feel free to constructively criticize my coding techniques. I don't call myself a good programmer and there are probably way simpler ways to do this!