hello i am hoping the answer is pretty straight forward and im to tired to realize it right now, but i have a table with 5 values and i would like to do something like
table={1,2,3,4,5}
if btnp(X) then table[i+1]
but also have it wrap back to 1 if the table index reaches the length of the table.
essentially a (for I=1,#table do) but only progresses if I press a button
so, do you mean that you’re storing a value from the table somewhere, and on button press you want to store the next value?
table={1,2,3,4,5} thing_idx=1 def _update() if btnp(X) then thing_idx=thing_idx + 1 % #table end end |
% is modulo, or the division remainder operator
# is the length operator for tables and strings
so the code increments the index by 1, but wraps around depending on the length of the table :)
@merwok,
That's basically the same approach I use, but you need to be sure to add the 1 after running the value through the modulo instead of before, or your value will loop back to 0.
table={1,2,3,4,5} index=1 function _update() cls() if btnp(❎) then index=index%#table+1 end ?table[index],1,1 end |
thank you for the responses! For clarity
ipipe={5,6}
lpipe={7,8,9,10}
tpipe={11,12,13,14}
function mk_pipe()
pipe={}
pipe.x=8
pipe.y=0
pipe.spr=ipipe[1]
end
function rotate_pipe()
if (btnp(❎)) then
--- here i want to be able to increment any pipe tables index by one up
to #pipetable before wraping back to index one
no need to append as these values will not change.--
@merwok
JadeLombax pointed out the first issue I was going to mention about your code. However, I'll also add that your code wouldn't have wrapped anyway due to operator precedence. % gets the same order as * and / .
@Razzputan
If you have an further need to post code, I suggest using the code formatting. It's a ``` before and after your code.
[Please log in to post a comment]