Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
best option to store a ton of variables? — Gideros Forum

best option to store a ton of variables?

piepie Member
edited September 2016 in General questions
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

  • antixantix Member
    edited September 2016
    Wouldn't it be easier to store the tiles in like a table where you can access them by their x and y locations? I haven't tested this code but I would maybe consider something like this...
    MY_TILEMAP = nil
     
    local function newTileMap(rows, columns)
      local tileMap = {}
      for r = 1, rows do -- How many rows
        local row = {}
        for c = 1, columns do -- How many columns
          row[# row + 1] = 0 -- Create a new column in the current row
        end
        tileMap[#tileMap + 1] = row -- Store the new row
      end
      return tileMap
    end
     
    local function getCell(r, c) -- Get cell from row, column
      local tileMap = MY_TILEMAP
      return tileMap[r][c]
    end
     
    local function setCell(r, c, data) -- Set cell at row, column
      local tileMap = MY_TILEMAP
      tileMap[r][c] = data
    end
     
    MY_TILEMAP = newTileMap(5, 4)
    That should work okay. I use this method to make buffers for my random dungeon generator and it seems to be pretty fast, even with 10 or so layers. My layers are only 32x32 though.

    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..
    local tileMap = MY_TILEMAP
    I have done no testing for speed but all of my games run super fast using globals so it's probably not going to be an issue.
  • piepie Member
    edited September 2016
    Thanks, I think it's not that different in terms of table complexity, but when I need it, it seems easier to me to parse tiles inside a "one level table" than using a nested for loop:
    (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).

  • I have no idea why global variables are so shunned but I suppose as long as you are using them sparingly then everything should be fine.
  • It's the bytecode that is generated by your code that affects performance. You should read this:

    https://www.lua.org/gems/sample.pdf

    Likes: totebo, hgy29, pie

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+3 / -0 )Share on Facebook
  • This pdf is perfect @SinisterSoft. Thanks for sharing. So many good tricks for optimization. And one more time i am amazed with LUA.
    I strongly suggest everyone to read it.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • So as long as you make your global local before using it you should be good to go? Seems that way to me.
  • Thank you, why this pdf is not featured somewhere on gideros website? :)
    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

    +1 -1 (+1 / -0 )Share on Facebook
  • Another good optimisation not standard in Lua, but will work in our 'version' is to use built-in bit manipulation rather than use the bit library. The built-in way is about 8x faster.

    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. :)
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • I do thank n1cke and all the others, gideros is more powerful at each release :)

    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
  • Likes: pie

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • piepie Member
    edited September 2016
    ah it's that easy? Forgive me for not trying :D
    [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

  • I don't know about any lua profilers, sorry. :)
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • just to follow up on lua profiling:
    ProFi works for me, and outputs a nice log
    https://gist.github.com/perky/2838755
Sign In or Register to comment.