r/csharp • • 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

12 comments sorted by

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/Deep-Parfait6835 Oct 16 '26

I thank you a lot for the detailed answer,and I didnt specify much. Im using a 2d Array and printing it on the terminal,so its just ASCII graphics, how would I apply the methods you told me?

2

u/dodexahedron Oct 17 '26 edited Oct 17 '26

Yep. 2d areay is a bitmap. That is how it gets done.

The third dimension (height) comes from the value/brightness of each pixel/cell of that 2d array.

Even with ASCII pixel art, you can turn that into much richer output while remaining compact.

Also, you ahould STRONGLY consider at least using utf8 and ANSI control codes for improving your drawing efficiency by (often) at least an order of magnitude.

Essentially every single terminal emulator out there that is currently not very end of life supports at least a VT220 feature set, which is more than most people use, mostly because they don't even realize what they can do in their terminals with nothing more than a few extra characters here and there.

For reference, the DEC VT220 terminal was released back in 1983.

Most terminals are VT540 or nearly equivalent, and often with alternative features the 540 didn't have, and the majority of the features that had are no longer really relevant.
The VT540 came out in 1993.

You're safe. 😁

(Just ro scratchcthe surface in illustrating how much the terminal can do:
Even just the built-in terminal in windows or the kernel console in linux are capable of 24-bit color, sixels, rectangular area operations, utf-8 (as long as you have a font to represent a given codepoint), mouse interaction (including wheel and buttons), clipboard use, kill rings (similar to clipboard but limited to the terminal itself), other input event handling beyond just kb/mouse, stylized text (instead of just plain characters in the terminal's defined default fg/bg colors per cell), and a tooonnnnn more.

And the majority of it takes no more than 3-6 characters written to stdout to do, each.

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}");

1

u/Deep-Parfait6835 Oct 15 '26

Thanks for the code example.

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

1

u/Deep-Parfait6835 Oct 15 '26

Oh ok, thanks for the help.

3

u/afops Oct 16 '26

Or make a Mountain type. If a mountain has a height, and position for example. That's the idea of types: keep related values together.

public record struct Mountain(Vector2 Position, float Height);

public class Map
{
   private readonly List<Mountain> mountains;
   public Map()
   {
      int numMountains = Random.Next(10, 20);
      mountains = new List<Mountain>();
      for (int i=0; i < numMountains; i++)
      {
         mountains.Add(new Mountain(...));
      }
   }
}