triplemocha
❔ How to get a random enum value?
I have a function here where I would like to get a random value from an enum type. The goal is to give NextValue any enum type, and it will calculate its length and return a random value. The cast on return isn't working. Is this possible to do?
30 replies
❔ What is causing my new thread to run slow?
Here's my code. I'm playing a song with System.Beep while throwing random 'character-sized pixels' to the screen with a font size of 1 and full screen. This is pure C# with a console utility library I'm building with some P/Invoking of the Windows API. But I'm always curious why when I'm setting the font size really small the beep song plays slower. It's its own thread, so I don't see why it matters how tiny the "pixels" are. When at font size of 24, the song plays beautifully. When at 1, it starts to crawl. It's like the thread isn't fully independent. Note I'm just trying to learn from why the created thread is behaving as such for academic reasons.
https://www.paste.org/125720
4 replies
❔ Should procedural 2d maps be with structs or instantiated with 'new'?
I am creating a game that can sometimes have a 1000x1000 interior grids in width and height. Exteriors will likely be larger, maybe a 5000x5000 2d array. Each grid square is called a Cell. A Cell class includes an enumerated type, two Lists (can hold up to 20 ints), and a float and boolean variable. My assumption is only a few maps will be loaded at a time, but I'm open to the idea of saving procedurally generated maps in a Dictionary key/value later. Should each Cell object be of a struct or class type?
6 replies
❔ Any issue with this for a game?
I'm making a game. I have a Array2D object that holds "Cells". So, each grid cell has data. Each cell can be a wall, path, etc. Every cell has two List<int> collections. If I have a 1000x1000 map that holds a cell in each, will I have any issues? Seems like a lot having 1000x1000x2 lists. Each list is estimated to hold at most 10 items.
14 replies
❔ Is this good practice with public accessors?
I come from C++ which used get/sets to access private members. In C#, there seems to be several ways of doing this. I learned the latest one. It seems like less time accessing these probbaly due to less function call overhead.
I'm creating a CustomDateTime class that's specific toward a game I'm working on. Is below good practice of using these public accessors? I assume the rule is if it fits in one line (member variable or expression), it's good practice?
36 replies
❔ Adding list in single line
In C++, I could add a list in a single line using a vector. For example:
Is there a similar way with C# and lists? That way I'm not calling "Add" 12 times, but keeping them like an array on initialization.
With C#, I think it's just
Or AddRange(): -- Which isn't too bad, but still feels bigger than it should be.
Ideal:
28 replies
❔ operator * cannot be applied to operands of type T and T
I'm getting this error:
operator * cannot be applied to operands of type T and T
I understand why, but I'm wondering if there's a way to add a constraint only to ints , floats and doubles. Sounds like it's a no. What else can be done?
15 replies
❔ How do I get the direction between two points?
Say that you are at position (10, 10) on the map, and your destination is (2, 2). I would like to calculate this and output this: "You need to go northwest." There will be all other directions, too. In my program, my Array2D class is being used to store the 2d array, and (0,0) is at the top-left corner.
My function is currently this, assuming I need vectors for it. What do I need to do to achieve this?
12 replies
❔ How can I create a fill method that creates unique elements?
I have the following 2D array method that is for basic types and references.
This works when I want basic types and every reference value pointing to the same object. I want to keep this method for those, which is the most common. However, I need a similar one that makes every element unique using
This works when I want basic types and every reference value pointing to the same object. I want to keep this method for those, which is the most common. However, I need a similar one that makes every element unique using
new
. For example, every Wall object in the array is unique. Each wall has its own properties. I do want the method generic so it allows any object of a class.
Here's the idea:
Again, this is just example code of what I want to see happen, but it has a compile error that T does not have a new() constraint. Is there a good solution to this?
For reference, my class is written as this:
24 replies