top of page

Determine if some number is odd or even

Let's say you have an array of items, and you want to sort them into columns based on whether their array index is even or odd, no matter how many there are.

I'm not a coder so answering this kind of question to me is like black magic, but the need arose and I did manage to figure out how to do it.

I looked on the first available google thread, which was StackOverflow http://stackoverflow.com/questions/160930/how-do-i-check-if-an-integer-is-even-or-odd

And found this if (x % 2 != 0) { /* x is odd */ } which is someone explaining about using modulo (the remainder operator) to figure it out.

UE4 has a modulo operator, so I tried this out:

This yields the following in the log :

If you are splitting a large number of items into two columns than the number of rows is going to be half the number of items (at least). So how do you tell it which row to go into?

For an array of 20 integers, starting at 0 you can divide the index for each by two, and if it is odd (using the modulo black magic) then you can coerce it into a float and use an operator called FCeil which rounds it up to the nearest number above (so 2.5 becomes 3 for instance).

Thusly, if you have two columns, then item0 and item1 go on row0, item2 and item3 go on row1, item4 and item5 go on row2, etc.

Here is the log result, with first row at the bottom :

Finally, here is an example of all this in practice : the inventory slots in the floating window arranged using the ideas discussed here.

Video :

Featured Posts
Recent Posts
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page