Log In  


Hi

Could someone kindly tell me how to for loop through each char of a string? I'm unsure how this is done with pico 8.



given string s,

for i=1,#s do
  local c = sub(s, i, i)
  -- do stuff with c
end

I put "local" here because it's typically faster that way, but it is optional.


kimiyoribaka, many thanks!!


By faster, do you mean 'local' executes faster as code?


Yes, though I'm basing that claim on lua in general more than Pico-8. From what I've read, local variables in general tend to be faster to access. My guess is it's because underlying code has to wade through all the variables and functions available in the scope being used, and local scope has less to wade through.


Very good to know.
I will definitely keep that in mind. Thank you!




[Please log in to post a comment]