Log In  

function wait()
  repeat
    flip()
  until btnp(4)
end

function count()
  cls()
  for i=1,3 do
    print(i)
  end
end

i=6

count()

wait()

print("")
print(i)
print("")

wait()

for i=1,3 do
  print(i)
end

wait()

print("")
print(i)
print("done!")

repeat
  flip()
until forever

By looking at this code you would think the end result would be 4 each time, both in the function and in the main program as most programming languages would return that value. But this is not the case with PICO-8. Instead the result will be what you defined globally, in this case 6.

So it's important to remember that the index variable in ANY FOR/END statements, either directly in your main code or in a function - will ALWAYS be local to the loop.

So you need NEVER define LOCAL variables in a function that will only be used in FOR/END statements. Good to know.

Here is the cart for examination:

Cart #mifehifiwi-0 | 2019-08-22 | Code ▽ | Embed ▽ | No License

P#66867 2019-08-22 20:57 ( Edited 2019-08-22 21:03)


[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-16 08:39:01 | 0.009s | Q:11