Hi there,
My god, I’m loving Corona so far! I’ve only been using it for 5 days (my girlfriend has seen or heard little from me the past few nights
but I’m already well under-way to my first game - a scrolling platform game of sorts. Loads of fun!
I’m having good results with smooth scrolling on an iPhone 3G so far, so I’m happy about that, but I do have some questions that I need help with, so all advise is more the welcome and much appreciated.
Mostly, I’m a bit in the dark on how to structure my code to get the best performance. I read everywhere to make things local as much as possible, but the things is that there are a lot of variables that I just want available throughout the whole application, and call nearly every frame for a whole lot of functions/objects. A good example of this is the Camera position, and the Tilemap-Array. Almost everything needs to know where the camera is at all time, so I can shut down a lot of routines and processing for things that are outside of the camera view. Likewise, I’m constantly checking the contents of an Array that holds the tiletypes (e.g. 1 = solid, 2 = air, 3 = cloud etc.) to calculate movement and collision for enemies, the Hero and more.
(Note: I’m new to Lua. I worked with Adobe Director for a long time, did some Flex work before so I’m used to scripting, but I’ve never really done OOP programming - which, I guess, makes Lua great for me!)
I have a couple of specific questions, right of the bat:
0) The big all-encompassing question is ‘how do I structure my data to be available FAST to a lot of functions’. One obvious way is to not use any modules and do everything in the main.lua file, and setting every local at the start of that file, but of course that’s impossible to manage.
- Which is fastest to read out my map of tiletype numbers, an Array or a Table? I’m using an array now, with the Array[#Array+1] fill method, because reputedly that’s faster than a table, but is that faster for writing or reading? Writing may be slow, I don’t care. But readout needs to be fast.
- Quick question: a local value is faster than a global to retrieve, I gather, and that’s why you would cache a global as a local first, for many functions or loops/branches. Am I right in assuming that if you ‘only need to use a variable once’ then a global is just fine, because caching it as a local first also needs to read the global value first?
- Why don’t my external modules see global values from other modules?
I thought that declaring a variable without the prefix ‘local’ would make the values global, and thus visible everywhere in my program, but apparently some functions (mostly in different modules) don’t see these variables. As a consequence I’m passing a lot of variables in between functions, and returning 'em as well, with seems like overkill to me.
O great master of Corona and Lua, please enlighten me 
Thanks a million!
Thomas (Belgium-based, by the way!) [import]uid: 70134 topic_id: 12933 reply_id: 312933[/import]