Wrong function output

Hello, I have a weird issue where if I try to input code for a safe (which is 2515), the buttons seem output the number as x+1. It’s like that on every button except 3, 6, 9 and 0, they output correct values. Did I do something wrong?




Is it printing the incorrect values from the print block in the Add to Code function?

Use MAP variable, it is key (index) and corresponding value.
So you can search for name and get integer value out of it.
Or use just strings (or whatever) array, search for that whatever, index should be number you use.

Or redesign whole umg/device:

  • instead of coding all those buttons code just single one.
  • either as UMG or as actor or blueprintable actor component
  • place that actor or component on your safe model (attach to it)
  • set each button value to integer, use that value to convert number into ASCII for number, print that number on key
  • when you press button, ask correct component/actor what is its integer value.

other way (all code in single place):

  • all those keypads numbers are in 3x4 grid. So you can calculate each key number as x+y*4. To get x and y divide keypad by size of button.
  • when you get 0…11 number, you can make integer array that reads numbers and give you result of which key was pressed.

Anyway:

Try remembering code as string. When you get integer 0…9 from key. Change it to string (it will be single number string). Then compare keypad string with correct code.

Code creating as TEXT:

make sure Result Code is empty before you start creating it.

I have changed the code to be a string instead of an integer array, but it seems like there’s an issue when calling the function on button click. The code works fine, but wrong data is sent to them.


I have tried to change it to send -1 (Btn1 would send 0, Btn2 → 1, Btn3 → 2 etc.) This is what I got:

The printed values are those that the function gets, so they’re incorrect. On the video, you can see that when I click a button, a different value gets sent.

You need feedback on which button was pressed. Collision/pressing button may be wrong.

You should add arrow componen inside that safe actor. MAke arrow visible in game.
When you press a button move that arrow to either button on location where you pressed.

If your code works correctly then only one source of that mess is that you press wrong buttons or collision on them is wrong. So make sure you can see which one button was pressed. That may be done if each button had different material, and you changed material on button press.

Again much better setup for this safe would be if each button was separate blueprint actor.

How those buttons are made?
Are they 3d Widgets, meshes that you line trace, or collision volumes?

Write your buttons like this:

1 [2 3] = 2
4 [5 6] = 5
7 [8 9] = 8
-  0 -

As you can see right column repeats what is pressed in middle column, i am almost sure you have something wrong with detecting touch on 3-6-9 column (like 2-5-8) covers both middle and right one column.

I HAVE FOUND THE ISSUE!
I set the buttons to be visible:


and when I played the game, I had it played in a window that wasn’t 16:9 ratio, so the buttons got stretched. The incorrect values were from me thinking I was clicking the right button, but in reality the button was stretched and that’s how I got wrong values.
Now, I need help with making the buttons not stretched.

If someone else in the future stumbles on this ( or even you op).

Its extra work, but you probably want to change the code to use mapped Keyboard KeyCodes - so that using the keyboard to enter the value is possible and runs the same lookup as the UI button events.

0 is 48, 9 is 57.

Additionally. You really dont do one event per button.
You create a button that has the event, and you make the string it holds as well as the value it adds a parameter you configure in the UI.
Precisely so as to avoid having individual button calls that have to be custom coded everywhere.

With the generalized One Event setup, you just filter the value of the KeyCode out, so input value received - 48 - and add the result as a single character to a componded string.

This enables you to do stuff like partial keypads on the fly. Instead of having to code each one manually everytime.

Ps: for keyboard accessibility you have to extend onkeydown, or press, or up. Or all of them. And manage the code yourself. To catch the input only for the button thats configured/added to the widget.
Another reason where you do it once, right, and re-use the component by customizing parameters…

1 Like

this is umg widget?

Just wrap it in size and scale boxes.

And do custom widget for single button, each with exposed variable for its int value.
Then after button press update that resulting code in owner BP or in BP_HUD or BP_PlayerController etc. With such widgets ready you can place them all in grid widget, and have neatly spaced.

And if you replied to my first post how you made buttons you would have this solved yesterday.