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
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.
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!
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
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
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.