Solve a simple math question to open a door

Hey all you brilliant people.

Im sitting with a small game for an project, and I want to make a level where you progress by solving a simple math question (like 2+2=?) in order to remove the door so you can get into the next room.

I guess that I need to make a trigger box at the door, so when I press E the math question pops up (A widget I guess?), then you have to type 4 and press either a button like enter, and then the door will open. (The Math questions are supposed to be randomly generated, but since this is just a prototype ill just make 3-5 doors with different math questions.)

Anyone got an idea or any leads to how I can make this?

Sounds like you’ve got it covered already.
You could use a trace for doors/anything interactive instead of a trigger.
This would make creating and placing things easier in future while also ensuring they are actually in front of the player.

Tom Looman has a tutorial on the subject that you might find useful:
[Tutorial] Multiplayer supported UsableActor System in Blueprint](http://www.tomlooman.com/tutorial-multiplayer-supported-usableactor-system-in-blueprint/)

I dont have a problem with the interaction of doors. But I need help with the whole math thing in unreal.
Like, when you press E on the door, a math question pops up, you enter the answer, press enter. Then, if its correct, the door opens.
I cant seem to find anything that helps me with this.

I have made two intergers that generate a random number between 1 and 10, then ive put in a math expression that adds those two numbers together and it has a number.

Now, I need to make it so the players has to solve that equation the engine has made, and if the player enters the correct number. The door opens.
And when he enters the volume trigger, the equation should show on the screen and be able to click next to the equation mark to enter the result.
Anyone know how?

At first get used to widgets. Make a simple widget with a textbox (So the user can enter his solution himself) and a button to submit the answer. Then let the widget blueprint decide an action based on the value in the textbox.

Once you have the basic widget interaction implemented and made sure it is working properly you can work on a system to generate random math questions - this will take much more time. I’d start with a random number between 0 and 3 deciding the type (like 0 gets you a substraction question, 1 a division question, and so on). store that value in a variable like “Question type” or whatever you wanna call it. Now you do more functions to generate the 2 random numbers (Maybe between 1 and 9) so if random number#1 is 8 and random number#2 is 4 and type is 0 it would be “8 - 4 = ?”. Of course you may wanna do more logic behind it so if type is 0 (substraction) then random number#2 must be greater than random number#1 (Else the result would be negative, for kids you normally don’t do that, keep it easy.). for divisions you probably want random#1 to just be a multiple of random number#2 and so on and so on…

Hope this gets you to the right direction… :slight_smile: Otherwise keep asking us, we love to help.

Thanks that got me in a good direction!

Ive made it simple and just let it be an addition for now. So I have Value1 + Value2 = result

All that works now. But now I need it to check if the number typed in the result box is true or false, and do an action if it is correct.
Does it have to be a submit button, or can I just keybind it to be, lets say “enter” ?

Im not really sure where to start with how the engine knows what to do with what the player enters.

You can also use key events, but try to keep it simple and then work your way to more complicated solutions.

The textbox just holds the player input - the trigger to check if the input is correct should be (to keep it simple now) a button. Just throw a button on your widget canvas - and to make it look nicer throw a text onto the button. Change the text to “Solve”.

Now if you scroll down a bit on the button properties you can enable the “clicked” event. Do that and in the graph you can now define what happens at the clicked event.

Here is my blueprint from the button event.

Basically i just branch on condition.

I threw my textbox into the blueprint (GET), then use the “GetText” node to get the actual text from it, that goes to a textToString node, that goes to a StringToInt node, that goes to an “equals” (int) node where i just check if it equals to “2”. in the final version you wanna have a variable here that holds the solution to your random math equation. For the simple example a fixed one is good enough as a start. In the end we just branch to a print string to print “Correct” or “Wrong”.

For testing i just call the widget from a key pressed event in my level blueprint. For testing that is good enough.

Play a bit with that, then make it more complicated.

If you select a textbox, in the details panel there is an event called OnTextCommited which is fired when Enter is pressed or when this widget loses focus for any other reason - you can drive your logic from this point. This event will give you access to the value entered.

This will also grant you additional control regarding user input.

Interesting, i didn’t even know that event - but i must admit i never used textboxes before. :slight_smile:

THANK YOU! That works!

one last thing. When the answer is correct, I want an actor (which just has a cube mesh in it for now) to disappear (Also collision).
Do you recommend that I just make a set visibility or do you have a better idea?

If you don’t need that actor any more you can also just destroy it. There is a destroyActor node in blueprint.

Hmm, I seem to struggle with figuring out where it should be written.
Ive tried in the widget event graph, where the whole thing above is in, but I cant seem to target the actor?
Ive tried to make a variable that is set by the equation being true. Then put a branch in the actor, and set the Getter in there as the condition. As in if Condition is true, destroy self.

But for some reason the getter in the actor needs a target. And I dont know what target it wants.

If you don’t have a reference for your actor you can also just work around this. Instead of directly calling destroy actor you can use “GetallActorsofClass”, select your actorClass and link that to destoyActor. If you made a blueprint of that actor it should be a class of its own and selectable through that.

Thanks you so much Fronzelneekburm!

It works like a charm!

Now, ONE. LAST. THING. I PROMISE!

Im going to make this level where there will be multiple actors with this function. I just tried it out, but as expected, when I solve the equation it destroys all the doors. I need it to only destroy that specific door/actor im standing at.

I guess it has something to with casting and get location, got any idea?

Nevermind I figured it out. Instead of doing the above picture I did this in the actor bp:
90f8bae3ca179a29f1920a69910f7a993d0e0146.jpeg

So now it just destroys that specific actor.

Thank you so much for the help!