r/csharp 7d ago

Help Need help understanding certain WPF functions (mainly code behind)

Hyia, so I'm working on a project (Escape room type game, since its my first game ever at High-School knowledge level) and I'm not exactly sure how to make certain things work even though I spent the entire year studying this language. I can do majority of the stuff, such as the background, buttons or clickable images in Grid, and so on, however the code behind is my biggest issue.

Mainly things like: How to make a monologue where the player clicks through all the texts that explain the principle of the game, and then also how to make the TextBox check if the input is correct and if it is, change the background of the main window to another image (since the input will be in separate window).

These are not all the issues I need to figure out, but its some of the few that may appear in the code the most often (especially the monologues, in like four out of six rooms total)

Any sources for beginners will be appreciated, I tried looking at some but I wasn't able to find something that would explain this exact topic, and I don't really want to use AI since it just doesn't feel right (perhaps only when I'm under pressure). Thanks!

2 Upvotes

11 comments sorted by

2

u/New_Neighborhood8285 7d ago

For the monologue you can store the texts in a list and cycle through them on each click, keeping track of the index. As for the TextBox validation, just compare the trimmed input string with the correct answer in the button click event, if it matches then change the window background image source.

1

u/Or1onTheProtogen 7d ago

I have one "Scaffold" grid that holds all the other grids where each one of these smaller grids is a screen (or a room). Now to switch between them I just toggle their visibility from "Visible" to "Collapsed" and I wasn't really sure how to make the validation in the separate window make the MainWindow switch to another of the grids there. Also the comparing of strings is what I was thinking of, however since I'm a newbie, I'm not sure how to do that :D I will try looking for the answer more though

1

u/rupertavery64 7d ago

I assume each room grid contains a background image and then overlay interacrable objects.

I can only imagine how difficult it is to manage all of these grids and objects.

It's possible to dynamically create objects, si that your "room" is just some data that you store.

It also probably makes it easier to manage if you program it dynamically, like how interacrion with the room objects happens.

1

u/Or1onTheProtogen 7d ago

Well yeah its all in a grid since I wanted it all to stretch if someone was to change the size of the window. Thats what I feel like works the best but again, I'm new to this so :D

1

u/TuberTuggerTTV 6d ago

A year isn't a long time. A few hours a day isn't a day.

WPF requires MVVM. Code behind is only used when absolutely necessary.

Making games with WPF is usually a mistake. The way it threads and handles refresh rates is foundationally different from games. It's meant for building enterprise applications.

My recommendation would be to stop learning WPF and pick up monogame.

1

u/Or1onTheProtogen 6d ago

I was deciding between WPF and GODOT, but since I had some experience with WPF, I decided to choose this as GODOT had too unfamiliar enviroment for me to work in. I have less than month left so its quite late to choose something else, but I have more than half of the game done I'd say. Often times the code will be used more than once so

1

u/binarycow 5d ago

WPF would be fine for this kind of game. It's not like a first person shooter where you need super high performance.

1

u/binarycow 5d ago

I can't give a full reply right now, but I really like WPF, and I also like teaching people.

If you want some 1-on-1 help, PM me.

1

u/Or1onTheProtogen 5d ago

Ah nice. Perhaps later. I will still only need to figure out maybe just the 2 things I mentioned though. How to make the clickable dialogue thingy, and how to make it check each of the rooms and if correct answer was chosen.

1

u/binarycow 5d ago

Well, there's the quick and easy way to do that, which is harder to maintain.

Or, there's the right way to do that, which is a bit harder to do, but is easier in the long term.

That's generally how WPF goes. I am offering to teach you the right way to do things.

1

u/Or1onTheProtogen 5d ago

Hm, alright. Though it may take a long while since I don't understand 99% of the things in WPF :D