1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

[TOTPP] Get Started With Lua Events

Discussion in 'Civ2 - General Discussions' started by Prof. Garfield, Sep 7, 2018.

Tags:
  1. Prof. Garfield

    Prof. Garfield Chieftain Supporter

    Joined:
    Mar 6, 2004
    Messages:
    2,074
    Location:
    Ontario
    @tootall_2012 @Knighttime

    Thanks for bringing this up. JPetroski explained that the limit was the reason he was using tables for his object naming, and a web search confirmed there was a limit, that the solution to exceeding this limit was to use the table method, and also that exceeding this limit probably meant that you should have multiple files anyway.

    Based on some testing I did today (boy am I glad I learned how to use Vim (well, GVim) and I could put in the code with a macro), there is a 200 local variable limit per function. This applies to the over all "module," since it is also a function. If you declare a local function inside another function, that function gets its own 200 local variables, and doesn't cut in to the allotment of the "outer" function (except that there is one local variable used for the function itself). I'll correct the lesson at some point.

    Disclaimer: much of what follows is an "educated" guess and could very well be wrong. But it is why I think the above statement is correct. This stuff isn't important to understand to use Lua.

    This makes sense with a piece of information that I learned in a CS course I took years ago. Programs have two "kinds" of memory they can use, "static" and "dynamic". When you call a function, it needs some place in memory to store the variables it will use, so it gets some memory from the operating system and uses it while the function is active. When the instance of the function is complete, the memory is made available for other uses. This is the "static" memory. In Lua terms, our local variables are (as far as I understand) the static memory.

    In a programming language like C, which is compiled, it is known how much static memory to allocate for each function, since the compiler can count the number of variable declarations during the compiling process. In Lua terms, if there are 7 uses of "local", then the function would ask for 7 "units" of static memory when the function is called. However, since Lua is not compiled (I think they do something kind of like compiling, but for our purposes I don't think it is compiling), but instead read line by line during execution, it doesn't know how much static memory to ask for. Therefore, I think the Lua creators simply said that 200 variables worth would be enough for most purposes.

    "Dynamic" memory allocation doesn't require knowing how much memory you will need when you write the program, and isn't freed up once any given function is completed. Tables (and global variables, which are entries in a table) are Lua's form dynamic memory, and Lua does "garbage collecting" to check when this dynamic memory is no longer used. In a language like C, this kind of memory must manually be deallocated (and causes "memory leaks" if not done properly).
     
  2. Prof. Garfield

    Prof. Garfield Chieftain Supporter

    Joined:
    Mar 6, 2004
    Messages:
    2,074
    Location:
    Ontario

Share This Page