I have an array of 2 guns and an array index variable called gun_index that is incremented up or down by 1 when I press the arrow keys. However, if gun index gets higher than the array length, i need it to go back to 0, and the same if it gets too low. I believe this needs to be done with a wrapf function.

I wrote this:

gun_index = wrapf(gun_index ,0, gun_array.size() - 1)

this isn't working. If i press up arrow, the index equals 1 no matter how many times I press it. If I press down arrow, the index equals -1.

If I remove the -1 from my code, pressing the down arrow will cycle through the index correctly, but now I have the problem of pressing up being able to cause the index to reach a higher number than the array can take, thus getting an error.

I have no idea why the first line of code doesn't work and the second does, what is going on and how do i fix it?

Not sure that is the right function. You can do it like this:

gun_index = (gun_index + 1) % gun_array.size()

You can also use wrapi(). wrapf() is for floating point numbers and may cause rounding problems when return value is cast to an integer index.