Hi, I have a lot of variables that I need to access and change in real time:
In a tilemap I am keeping track of tiles content by their number starting from the top left.
As an example, a small tilemap (3x3) looks like (the numbers are tiles id):
1,2,3
4,5,6
7,8,9
In my game I'd like to use up to 20x20 tilemaps, which lead to tables with 400 possible indices (most of these empty, but who knows..? ).
Each tile with specific content is saved in a global table at index [tile id number], and accessed when needed. Empty tiles are not saved in this table.
When I need to check if there is something in a specific tile, I just check if mytiletable[tile id] exist and then read the informations I need (some entries: 10 to 20 for each tile).
I know that I should avoid global variables, and I'd like to avoid them but in my tests the global table is providing the fastest access to my data, and since it's global it can be easily accessed by everything.
I was wondering if there is a better way to do this or if I am already using the best alternative
Thank you
Comments
With regards to global variables.. I use them everywhere! I create many global classes and when I want to access one I just use something like this..
(for each row, for each column).
To get coordinates when I need them I just use some basic math (tileindex/mapwidth and tileindex/mapheight).
I also noticed that speed is not a problem using global variables, but they say (the internet..) to avoid global variables if possible, and I was wondering if there was a better way to handle this I think that global variables just increase ram usage (but I have no coding background, so I am asking).
https://www.lua.org/gems/sample.pdf
Likes: totebo, hgy29, pie
https://deluxepixel.com
I strongly suggest everyone to read it.
Likes: SinisterSoft
As I understood there is no better option for me here.. and it's better to have only one big table than many smaller tables.
I'd like to profile my game as suggested there though: did you try any lua profiler with gideros?
http://lua-users.org/wiki/ProfilingLuaCode
Likes: SinisterSoft
Also use macros for constants (also unique to our version of Lua) - it's better than wasting locals and because they will be evaluated at bytecode creation time rather than at run time then they should be faster too.
You should thank @N1cke for these enhancements.
https://deluxepixel.com
How do I switch between "old" bit lib to built-in bit manipulation? (assume that I use bitlib as a dogma, only to flip my tiles :P )
thank you
http://giderosmobile.com/forum/discussion/6622/lua-bitwise-operators-with-new-metamethods#Item_1
Likes: pie
https://deluxepixel.com
Forgive me for not trying[edit I tried but I couldn't make it work. I posted in main bit thread]
what about profiling? does any of the "standard" profilers available at lua-users is known to work "out of the box"?
Thank you
https://deluxepixel.com
ProFi works for me, and outputs a nice log
https://gist.github.com/perky/2838755