C
C#3w ago
Zilly

Console Game scenes equivalent

I usually use Unity for C# because I'm not very fluent in it. I'm trying to learn by making a very basic idle game, and want to add a new feature. I'd like a second scene i can open up when I use left arrow, which completely clears the console and writes the new screen. I also want updates on that page to be saved and to be able to change when they aren't loaded. I've talked to a close friend who's really good with C# and they suggested using arrays, but I don't understand how I could use them. Lmk if you want my code I can send it
94 Replies
Zilly
ZillyOP3w ago
I considered using a new one of these (idk what they're called), but idk what type of array to use to be able to have those as values, if I knew how to do that I think I could do the rest myself
Zilly
ZillyOP3w ago
No description
No description
Mąż Zuzanny Harmider Szczęście
u mean classes? if you dont know what that is please learn c#
Zilly
ZillyOP3w ago
Ik what they are, I didn’t know what they were called I thought classes were the things that they were inside of lol
Mąż Zuzanny Harmider Szczęście
you could store a copy of them and iteract with the *them in an array
Zilly
ZillyOP3w ago
How would you store a copy?
Mąż Zuzanny Harmider Szczęście
List<Game> Consoles = new List<Game>();
// in some other code:
Console.Add(new Game());
List<Game> Consoles = new List<Game>();
// in some other code:
Console.Add(new Game());
Zilly
ZillyOP3w ago
Alr I’ll give that a try when I get home, thank you so much ok I forgot but like this?
class Game {

List<Game> Consoles = new List<Game>();
// Random other code idk
if (key == ConsoleKey.RightArrow)
{
windowIndex = Math.Clamp(windowIndex + 1, 0, 1);
Consoles.Add(new Game());
}
}
class Game {

List<Game> Consoles = new List<Game>();
// Random other code idk
if (key == ConsoleKey.RightArrow)
{
windowIndex = Math.Clamp(windowIndex + 1, 0, 1);
Consoles.Add(new Game());
}
}
so how would I configure the new screen and stuff? tsk why is mine not coloured I can’t figure this out
Zilly
ZillyOP3w ago
BlazeBin - uayzhdtxkghv
A tool for sharing your source code with the world!
Zilly
ZillyOP3w ago
@FusedQyou
FusedQyou
FusedQyou3w ago
the issue is nothing is printing anymore and the code around it is idk because idk what the issue could be or where it's from
So is UpdateMainConsole the method that does the printing?
Zilly
ZillyOP3w ago
yes
FusedQyou
FusedQyou3w ago
Debugging 101, the first thing you do is check if the method is called at all So you place a breakpoint at the start of the method, and see if it pauses on it If that doesn't happen, place them above the method close to where it's called Specifically line 91, you see if (jobPoints > 0) in the Update method. Place a breakpoint down in there, see if it breaks
Zilly
ZillyOP3w ago
the code just ended it didn't pause
FusedQyou
FusedQyou3w ago
Specifically there you can hover over jobPoints and see what the value is. The method executes when it's higher than 0 and it probably won't be that If that breakpoint isn't called, it is possible that Update is not called
Zilly
ZillyOP3w ago
pause back up
FusedQyou
FusedQyou3w ago
But then I see the method is async void. Why are you using an async void method?
Zilly
ZillyOP3w ago
UpdateMainConsole didn't pause
FusedQyou
FusedQyou3w ago
Who told you that is a good idea?
Zilly
ZillyOP3w ago
my friend who is literally teaching me everything I know about c#
FusedQyou
FusedQyou3w ago
If Update throws an exception now, it is silently ignored and your program will end without an error
Zilly
ZillyOP3w ago
he just wrote it and I never questioned it
FusedQyou
FusedQyou3w ago
Your friend is a dumbass Never use async void Respectfully, I don't know the man But async void is a very bad habit
Zilly
ZillyOP3w ago
I put it at the (if jobPoints >0) part and the code just ended again
FusedQyou
FusedQyou3w ago
Let's fix this async void method first In C# we have the async Task pattern for asynchronous programming
Zilly
ZillyOP3w ago
ok let's fix the completely unrelated problem first, cool wow that sounds a lot ruder than I intended
FusedQyou
FusedQyou3w ago
If you're going to be snarky I'm not going to help you
Zilly
ZillyOP3w ago
;-;
FusedQyou
FusedQyou3w ago
This could very well be the issue Do you know what asynchronous programming is?
Zilly
ZillyOP3w ago
I know it runs asynchronously from the rest of the code
FusedQyou
FusedQyou3w ago
Yes, and possible in a background thread That's why you can run it infinitely and add delays In C# there's a state machien that it builds in that adds proper async support, but you have to use that right pattern What you have now is a basic method that is just run in the background and forgotten about
Zilly
ZillyOP3w ago
ok
FusedQyou
FusedQyou3w ago
The issue is that if it breaks, just nothing happends and any errors are then left confusing you why the program doesn't work
Zilly
ZillyOP3w ago
so to fix that do I just remove the void part?
FusedQyou
FusedQyou3w ago
1 sec
Ion
Ion3w ago
Yes, anything async that from which you don't intend to return anything should be async Task. Using async void is a recipe for disaster
FusedQyou
FusedQyou3w ago
public async void Update()
{
try
{
await UpdateAsync();
}
catch(Exception ex)
{
Debug.Fail(ex.Message);
}
}

public async Task UpdateAsync()
{
while (true)
{
jobPoints += (10 * workers);

workerCost = 10 + (workers * 2);
workers += (10 * managers);

managerCost = 1000 + (managers * 200);
managers += (10 * owners);

ownerCost = 1000000 + (owners * 20000);

if (jobPoints > 0) UpdateMainConsole(jobPoints, workers, workerCost, managers, managerCost, owners, ownerCost, selectedButtonIndex);
await Task.Delay(1000);
}
}
public async void Update()
{
try
{
await UpdateAsync();
}
catch(Exception ex)
{
Debug.Fail(ex.Message);
}
}

public async Task UpdateAsync()
{
while (true)
{
jobPoints += (10 * workers);

workerCost = 10 + (workers * 2);
workers += (10 * managers);

managerCost = 1000 + (managers * 200);
managers += (10 * owners);

ownerCost = 1000000 + (owners * 20000);

if (jobPoints > 0) UpdateMainConsole(jobPoints, workers, workerCost, managers, managerCost, owners, ownerCost, selectedButtonIndex);
await Task.Delay(1000);
}
}
This is a somewhat simple way to make a background task
Zilly
ZillyOP3w ago
ok I changed it and it is still very much broken
FusedQyou
FusedQyou3w ago
It's still bad, but at least it will not break the code unexpectetly
Zilly
ZillyOP3w ago
oh you did something ok can you like explain it
FusedQyou
FusedQyou3w ago
By returning a Task C# can properly introduce a state machine that can be awaited and returns exceptions
FusedQyou
FusedQyou3w ago
Task-based asynchronous programming - .NET
In this article, learn about task-based asynchronous programming through the Task Parallel Library (TPL) in .NET.
FusedQyou
FusedQyou3w ago
Read this
Zilly
ZillyOP3w ago
the name debug does not exist in the current context
FusedQyou
FusedQyou3w ago
Add the namespace
Zilly
ZillyOP3w ago
oh yeah that makes sense add the thing that makes it work ;-; ok it instantly ended again I assume that isn't ideal
FusedQyou
FusedQyou3w ago
Generally for things like this you should create a proper system that makes these background tasks. Another way is this:
public void StartUpdate()
{
_ = UpdateAsync().ContinueWith(async (x) =>
{
try
{
await x;
}
catch (Exception ex)
{
Debug.Fail(ex.Message);
}
});
}
public void StartUpdate()
{
_ = UpdateAsync().ContinueWith(async (x) =>
{
try
{
await x;
}
catch (Exception ex)
{
Debug.Fail(ex.Message);
}
});
}
I personally like this because it removes the whole async void part This makes the task, and it basically adds a delegate that is code to invoke when it ends
Zilly
ZillyOP3w ago
what does try and catch do
FusedQyou
FusedQyou3w ago
Then you await the result, that is the task you returned, much like you awaited the UpdateAsync before, and any exceptions are then thrown
Zilly
ZillyOP3w ago
and why greater than or equal to sign?
FusedQyou
FusedQyou3w ago
Exceptions and Exception Handling - C#
Learn about exceptions and exception handling. These C# features help deal with unexpected or exceptional situations that happen when a program is running.
Zilly
ZillyOP3w ago
I can't believe I'm getting homework 😭 alr do I need to read this now? or can it wait
FusedQyou
FusedQyou3w ago
It can wait, you can also just use the first bit of code This one What it does there is it runs the code in the background, and if it throws an exception it calls Debug.Fail and your code will pause much like a breakpoint
Zilly
ZillyOP3w ago
oK oo I see it instantly ended with no debugs or errors or anything
FusedQyou
FusedQyou3w ago
So the writing still didn't work
Zilly
ZillyOP3w ago
ye ;-;
FusedQyou
FusedQyou3w ago
Well now check UpgradeMenu which calls the update
Zilly
ZillyOP3w ago
or the game loop tbh
FusedQyou
FusedQyou3w ago
That method is never called anywhere
Zilly
ZillyOP3w ago
nono that's for later not done with that yet
FusedQyou
FusedQyou3w ago
Go back to UpdateMainConsole, and you see it's called in MainMenu as well Just like Update
Zilly
ZillyOP3w ago
the update is called elsewhere
FusedQyou
FusedQyou3w ago
So where is MainMenu called? Place a breakpoint in there
Zilly
ZillyOP3w ago
MainMenu is the main class where my code is
FusedQyou
FusedQyou3w ago
See if it's called
Zilly
ZillyOP3w ago
like if I have
MainMenu();
MainMenu();
anywhere? because I do not
FusedQyou
FusedQyou3w ago
So the code doesn't run And therefore the text is never drawn
Zilly
ZillyOP3w ago
;-;
FusedQyou
FusedQyou3w ago
Problem solved, run the method
Zilly
ZillyOP3w ago
shouldn't it just run the code? where do I put it? that's my main thing so idk where I'd put MainMenu();
FusedQyou
FusedQyou3w ago
Your main thing is Main on line 8 That's that run by C# convention
FusedQyou
FusedQyou3w ago
Main() and command-line arguments - C#
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
FusedQyou
FusedQyou3w ago
All you did was make a new instance of Game, and that's it
Zilly
ZillyOP3w ago
oh I never new what that was for do I make that with curly brackets and put the line in there?
FusedQyou
FusedQyou3w ago
That's why I mentioned you should check out the very basics first Your main method should make an instance of Game, and then call MainMenu However, it will still end instantly It runs it, the method ends, the program ends
Zilly
ZillyOP3w ago
tsk idk why my friend put all this complicated crap in
FusedQyou
FusedQyou3w ago
There's plenty of things that are quesitonable in this code Why the fuck are you using BigInteger
Zilly
ZillyOP3w ago
oh we were fucking around with stupidly high numbers and he told me to just leave it as that ;-;
FusedQyou
FusedQyou3w ago
Well, the best way to fix is is by making the Update method the main thing that runs the application
Zilly
ZillyOP3w ago
anyways it works but updates rly slowly
FusedQyou
FusedQyou3w ago
First of all, you should readd the basic Program class and put the main method in there This is very messy, having it in here Make a file Program.cs and put this in here
public sealed class Program
{
[STAThread]
public static void Main(string[] args)
{
}
}
public sealed class Program
{
[STAThread]
public static void Main(string[] args)
{
}
}
Zilly
ZillyOP3w ago
that's the current file's name
FusedQyou
FusedQyou3w ago
Then rename it to Game It has the Game class Single purpose per file, this is the game class
Zilly
ZillyOP3w ago
uh ok so I renamed it and now it won't load nvm I did it
FusedQyou
FusedQyou3w ago
Because C# by default tries to find the Main method in the Program class Can also be changed but there's no reason to
Zilly
ZillyOP3w ago
ok done I think
FusedQyou
FusedQyou3w ago
So what you want to do is just have the async Task Update method, and in the Main method you want to start a new game but then await the Update method This way it will constantly run
while (true)
{
if (!Console.KeyAvailable) continue;

ConsoleKey key = Console.ReadKey().Key;

if (key == ConsoleKey.Enter && selectedButtonIndex == 0)
{
jobPoints++;
}
if (key == ConsoleKey.Enter && selectedButtonIndex == 1)
{
if (jobPoints >= workerCost)
{
workers++;
jobPoints -= workerCost;
}
}
if (key == ConsoleKey.Enter && selectedButtonIndex == 2)
{
if (jobPoints >= managerCost)
{
managers++;
jobPoints -= managerCost;
}
}
if (key == ConsoleKey.Enter && selectedButtonIndex == 3)
{
if (jobPoints >= ownerCost)
{
owners++;
jobPoints -= ownerCost;
}
}
if (key == ConsoleKey.DownArrow) selectedButtonIndex = Math.Clamp(selectedButtonIndex + 1, 0, 3);
if (key == ConsoleKey.UpArrow) selectedButtonIndex = Math.Clamp(selectedButtonIndex - 1, 0, 3);

if (key == ConsoleKey.RightArrow)
{
windowIndex = Math.Clamp(windowIndex + 1, 0, 1);
}
}
while (true)
{
if (!Console.KeyAvailable) continue;

ConsoleKey key = Console.ReadKey().Key;

if (key == ConsoleKey.Enter && selectedButtonIndex == 0)
{
jobPoints++;
}
if (key == ConsoleKey.Enter && selectedButtonIndex == 1)
{
if (jobPoints >= workerCost)
{
workers++;
jobPoints -= workerCost;
}
}
if (key == ConsoleKey.Enter && selectedButtonIndex == 2)
{
if (jobPoints >= managerCost)
{
managers++;
jobPoints -= managerCost;
}
}
if (key == ConsoleKey.Enter && selectedButtonIndex == 3)
{
if (jobPoints >= ownerCost)
{
owners++;
jobPoints -= ownerCost;
}
}
if (key == ConsoleKey.DownArrow) selectedButtonIndex = Math.Clamp(selectedButtonIndex + 1, 0, 3);
if (key == ConsoleKey.UpArrow) selectedButtonIndex = Math.Clamp(selectedButtonIndex - 1, 0, 3);

if (key == ConsoleKey.RightArrow)
{
windowIndex = Math.Clamp(windowIndex + 1, 0, 1);
}
}
This thing Should be called by the Update method as a side thing
Zilly
ZillyOP3w ago
wait so put that into update method?
FusedQyou
FusedQyou3w ago
Just call a method that does this Ideally your Update method does nothing but call methods for other things So even the stuff in the method currently should be in a separate method That keeps it clean
Zilly
ZillyOP3w ago
wait I'm confused wdym where do I put that? sorry I'm just not understanding rn nvm I gotta go out I'll come back after a while cya
FusedQyou
FusedQyou3w ago
Scriptbin - Share Your Code Easily
Use Scriptbin to share your code with others quickly and easily.
FusedQyou
FusedQyou3w ago
Like this Single loop calls other stuff Added tics so you can call things per second, like that points method And running so when you put it to false the game ends Look at this when you get back
Zilly
ZillyOP3w ago
Very nice very nice I’ll do that soon Can’t right now tho

Did you find this page helpful?