Saving value to global

Hi, im new to corona and im trying to implement a Picker Wheel so the user can select the number of players (number from 1 to 4). The picker wheel is already there and all i need to do is that when the user picks a value it is stored in a global variable. I did this following a tutorial that I found (http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/)

That is already done but now every time the user goes into the config Scene and the Picker Wheel gets shown, the index needs to be set to the value that he selected before. There is where I get this error: 

Attempt to index field ‘?’ (a nil value)

The way I try to set the new index is by a a simple “if” but I can’t manage to solve this issue.

Please help

-- Set the default value local wheel\_index = 2 if data.players == nil then print("There's nothing stored in the variable", wheel\_index) else wheel\_index = data.players print("Sets the index to the value stored") end

There shouldn’t be a problem if you created your globals file correctly.

data.lua:

[lua]

local M = {}

M.wheel_index = nil

return M

[/lua]

Any scene that uses global variables:

[lua]

local data = require(“data”)

local wheel_index = 2

if data.wheel_index == nil then

  print(“There’s nothing stored in the variable”, wheel_index)

else

    wheel_index = data.wheel_index

    print(“Sets the index to the value stored”)

end

[/lua]

By the way, I wouldn’t bother localising wheel_index to its own variable - you could just set M.wheel_index = 2 in the data.lua file, and update it directly as you go along, using data.wheel_index to set the position of the picker.

Thanks for your help, but I still can’t get it to work.

This is my code in the configuration menu. Im sorry for the spanish variables.

module(..., package.seeall) local widget = require( "widget" ) local datos = require( "datos\_configuracion" ) function new() -- Set the default value local wheel\_index = 2 if datos.wheel\_index == nil then print("There's nothing stored in the variable", datos.wheel\_index) else wheel\_index = datos.wheel\_index print("Sets the index to the value stored") end local w,h = display.contentWidth, display.contentHeight local aceptar = display.newText("Aceptar",0,0,nil,16) aceptar:setReferencePoint(display.CenterReferencePoint); aceptar.x = \_w-40; aceptar.y = \_h+20; local localGroup = display.newGroup() local number\_players = {} -- Populate the days table for i = 1, 4 do number\_players[i] = i end --print("la cantidad de jugdores que hay son," cantidad\_jugadores.getn(a)) -- Set up the Picker Wheel's columns local columnData = { { align = "center", width = 290, labels = number\_players, startIndex = wheel\_index } } -- Create a new Picker Wheel local pickerWheel = widget.newPickerWheel { top = display.contentHeight - 0, font = native.systemFontBold, columns = columnData, } function changeScene(e) if(e.phase == "ended") then director:changeScene(e.target.scene, "moveFromRight"); end end function Back(e) if(e.phase == "ended") then localGroup.remove(pickerWheel) director:changeScene(e.target.scene, "moveFromLeft"); display.remove( pickerWheel ) pickerWheel = nil end end function show\_picker() transition.to( pickerWheel, { time=300, x= 0, y=(h - 222)} ) transition.to(aceptar,{ time=300, x= w-40, y=(h - 240)}) end function ocultar\_picker() --guarda el valor del picker en la variable global datos local selectedRows = pickerWheel:getValues() for i=1,#selectedRows do datos.wheel\_index = selectedRows[i].value print ("se ha guardado el valor en datos: ", selectedRows[i].value) end transition.to( pickerWheel, { time=300, x= 0, y=h+222} ) transition.to( aceptar, { time=300, x= w-40, y=h+240} ) localGroup.remove(pickerWheel) end aceptar:addEventListener("tap", ocultar\_picker); local label\_jugadores = display.newText("Numero de Jugadores", 0, 0, native.systemFont, 16) label\_jugadores:setReferencePoint(display.CenterReferencePoint); label\_jugadores.x = \_w/2; label\_jugadores.y = \_h/2-90; label\_jugadores:addEventListener("tap", show\_picker); local creditos = display.newText("Créditos", 0, 0, native.systemFont, 16) creditos:setReferencePoint(display.CenterReferencePoint); creditos.x = \_w/2; creditos.y = \_h/2-40; creditos.scene = "configuracion"; creditos:addEventListener("tap", changeScene); --local aceptar = display.newText("Aceptar", 0, 0, native.systemFont, 16) local volver = display.newText("Atrás", 0, 0, native.systemFont, 16) volver:setReferencePoint(display.CenterReferencePoint); volver.x = \_w/2; volver.y = \_h/2-140; volver.scene = "menu"; volver:addEventListener("touch", Back); --- insert everything into the localGroup localGroup:insert(label\_jugadores) localGroup:insert(creditos) localGroup:insert(volver) localGroup:insert(aceptar) -- clean everything up clean = function () end -- do not remove lest the sky shalt fall upon thine head return localGroup end

There shouldn’t be a problem if you created your globals file correctly.

data.lua:

[lua]

local M = {}

M.wheel_index = nil

return M

[/lua]

Any scene that uses global variables:

[lua]

local data = require(“data”)

local wheel_index = 2

if data.wheel_index == nil then

  print(“There’s nothing stored in the variable”, wheel_index)

else

    wheel_index = data.wheel_index

    print(“Sets the index to the value stored”)

end

[/lua]

By the way, I wouldn’t bother localising wheel_index to its own variable - you could just set M.wheel_index = 2 in the data.lua file, and update it directly as you go along, using data.wheel_index to set the position of the picker.

Thanks for your help, but I still can’t get it to work.

This is my code in the configuration menu. Im sorry for the spanish variables.

module(..., package.seeall) local widget = require( "widget" ) local datos = require( "datos\_configuracion" ) function new() -- Set the default value local wheel\_index = 2 if datos.wheel\_index == nil then print("There's nothing stored in the variable", datos.wheel\_index) else wheel\_index = datos.wheel\_index print("Sets the index to the value stored") end local w,h = display.contentWidth, display.contentHeight local aceptar = display.newText("Aceptar",0,0,nil,16) aceptar:setReferencePoint(display.CenterReferencePoint); aceptar.x = \_w-40; aceptar.y = \_h+20; local localGroup = display.newGroup() local number\_players = {} -- Populate the days table for i = 1, 4 do number\_players[i] = i end --print("la cantidad de jugdores que hay son," cantidad\_jugadores.getn(a)) -- Set up the Picker Wheel's columns local columnData = { { align = "center", width = 290, labels = number\_players, startIndex = wheel\_index } } -- Create a new Picker Wheel local pickerWheel = widget.newPickerWheel { top = display.contentHeight - 0, font = native.systemFontBold, columns = columnData, } function changeScene(e) if(e.phase == "ended") then director:changeScene(e.target.scene, "moveFromRight"); end end function Back(e) if(e.phase == "ended") then localGroup.remove(pickerWheel) director:changeScene(e.target.scene, "moveFromLeft"); display.remove( pickerWheel ) pickerWheel = nil end end function show\_picker() transition.to( pickerWheel, { time=300, x= 0, y=(h - 222)} ) transition.to(aceptar,{ time=300, x= w-40, y=(h - 240)}) end function ocultar\_picker() --guarda el valor del picker en la variable global datos local selectedRows = pickerWheel:getValues() for i=1,#selectedRows do datos.wheel\_index = selectedRows[i].value print ("se ha guardado el valor en datos: ", selectedRows[i].value) end transition.to( pickerWheel, { time=300, x= 0, y=h+222} ) transition.to( aceptar, { time=300, x= w-40, y=h+240} ) localGroup.remove(pickerWheel) end aceptar:addEventListener("tap", ocultar\_picker); local label\_jugadores = display.newText("Numero de Jugadores", 0, 0, native.systemFont, 16) label\_jugadores:setReferencePoint(display.CenterReferencePoint); label\_jugadores.x = \_w/2; label\_jugadores.y = \_h/2-90; label\_jugadores:addEventListener("tap", show\_picker); local creditos = display.newText("Créditos", 0, 0, native.systemFont, 16) creditos:setReferencePoint(display.CenterReferencePoint); creditos.x = \_w/2; creditos.y = \_h/2-40; creditos.scene = "configuracion"; creditos:addEventListener("tap", changeScene); --local aceptar = display.newText("Aceptar", 0, 0, native.systemFont, 16) local volver = display.newText("Atrás", 0, 0, native.systemFont, 16) volver:setReferencePoint(display.CenterReferencePoint); volver.x = \_w/2; volver.y = \_h/2-140; volver.scene = "menu"; volver:addEventListener("touch", Back); --- insert everything into the localGroup localGroup:insert(label\_jugadores) localGroup:insert(creditos) localGroup:insert(volver) localGroup:insert(aceptar) -- clean everything up clean = function () end -- do not remove lest the sky shalt fall upon thine head return localGroup end