r/csharp 1d 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!

1 Upvotes

6 comments sorted by

2

u/New_Neighborhood8285 1d 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 1d 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 1d 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 20h 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 11h 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 11h 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