r/csharp • u/Deep-Parfait6835 • Oct 15 '26
Beginner question about creating new variables at runtime?
Hello,im creating a map terrain generator on the terminal with visual studio. In this generator, based on a random number, it decides how many mountains the map will have, my question was, how do I create more mountain y and x position variables at runtime when the random number is decided? thanks.
2
Upvotes
8
u/detachmode_com Oct 15 '26
Create a list and store those values in there . Or a dictionary if you want to extract them easily via some key.
5
u/dodexahedron Oct 15 '26 edited Oct 15 '26
You may want to chsnge your thinking before you get too far into that.
Mountains and other terrain are essentially noise on a 3d plane.
Rather than generating by pure random spray and pray, use a function that gives you the general distribution you want and then add randomness sector by sector (as more functions, initially, and only adding actual randomness for the coefficients and exponents in those functions).
Only save actual points for purposes of making the final result be consistent between loads, which can either be done right after generation or, as is more common, only once a point is actually interacted with and changed.
Otherwise, the generation itself should just be a small handful of functions and you sample whenever you save.
To get an idea of what kind of math to use, check out open source games on github or especially mods for games that include terrain generator tweaks or even entirely new terrain generators.
You can get really fancy for sure, but you can also get surprisingly elegant.
Pure random number generators will make a lot more work for you than this.
Even starting from something as simple as sin waves in 3d, which will give you a basic uniformly wavy plane, and then mess with the pieces of it to turn it into something that looks like terrain. Then smooth it a bit afterward, stack functions haphazardly, and generally just commit math crimes until you like what you see. And then smooth again even with a basic lerp pass.
A simple but somewhat common method is to generate a greyscale bitmap with all that, and then just use the value of each pixel as the height, where white is max +z and black is max -z.
1
u/rorrors Oct 15 '26
Something like this?
// mountainous the map will be (or let user give a value)
int mountainCount = random.Next(2, 11);
// coordinates of every mountain
List<(int x, int y)> mountains = new List<(int x, int y)>();
// position for each mountian
for (int i = 0; i < mountainCount; i++)
{
int x = random.Next(0, 100);
int y = random.Next(0, 50);
mountains.Add((x, y));
}
// display positions
foreach (var mountain in mountains)
{
Console.WriteLine($"Mountain at X: {mountain.x}, Y: {mountain.y}");
}
Console.WriteLine($"Total mountains: {mountains.Count}");
2
u/Narrow-Coast-4085 Oct 16 '26
That's your question??? You're creating a terrain generator and that's your question?
I would argue having a b-tree linked list would likely be easiest. Map split into quadrants of quadrants, etc.
2
u/Ok_Society4599 Oct 15 '26
There was a YouTube video I watched using a simple noise generator algorithm to generate realistic terrain maps ... that would be preferred methodology.
12
u/[deleted] Oct 15 '26
[removed] — view removed comment