Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
global variables seen as nil — Gideros Forum

global variables seen as nil

PACPAC Member
edited February 2016 in General questions
Hi everyone. I'm having a problem with some global variables being seen with nil values.
This is only happening on my android phone. It doesn't happen when running on Gideros Player with Windows 7.

I have all my Global variables declared at the very top of 'main.lua'.
Ex:
puznumber=0
puzmark =0

Errors from Gideros and Eclipse:

Sample #1:
load_Puzzle = function( )
local ps=string.format("%03d", puznumber)
--
--
end

Gideros ERROR:
main.lua:604: bad argument #2 to 'format' (number expected, got nil)


Sample #2:
local function save_State()
local file=io.open("|D|cpsettings.txt","w+")
file:write(puzmark, "\n")
--
--
end

Eclipse ERROR::
E/AndroidRuntime(19373): com.giderosmobile.android.player.LuaException: ......... attempt to
index global 'puzmark' (a nil value)

Any insight would be greatly appreciated.

Comments

  • Hi @PAC I can guess that some function is trying to access the variables BEFORE main.lua is processed (main.lua is usually the last file in loading order).

    You can try placing them in another lua file, and make sure it is loaded first (right click on file in project tree to check for loading order, here you can also check for file dependencies)
    If this is the reason for the errors, on desktop it may be working due to faster reading/loading times. :)
  • pie, these were great suggestions, I really thought you nailed it, but after moving these variables to the top of the file order I'm still having the same problem.

    I've read more than once about the pitfalls of using Globals and have used as few as possible. I'll do some more studying on lua.
    MANY THANKS for the suggestions!
  • You're welcome,
    Try zerobrane studio. It has a nice watch window and debugging features that may be of help. Do a search on this forum, there are also some tutorials around :)
  • ar2rsawseenar2rsawseen Maintainer
    edited February 2016
    init.lua is the file that is loaded first

    Then we have file in alphabetical order (while resolving code dependencies provided)
    and then main.lua is loaded last

    So global values better to put in init.lua

    :)

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • I always forget init.lua, its so handy!
  • Thanks again for the input guys. Here was the problem. I exit the game with

    if gamestate==666 then
    save_State()
    collectgarbage()
    collectgarbage()
    collectgarbage()
    collectgarbage()
    puznumber=nil -- these were the variables I had trouble with
    puzmark=nil

    application:exit()
    end

    Since gamestate never got cleared it was reading save_State() twice before application:exit() finalized. :(|) I thought application:exit() would have stopped all.

    Is application:exit() a good way to close a program? Seems like a lot of apps use 'swipe down' instead.
Sign In or Register to comment.