[Resolved] Sharing variable data through scenes

Hey all, my second ‘help’ thread in less than 24 hours, not good!

Ok, i have set a global variable in main.lua like so

[lua]_G.textToDisplay = 33[/lua]

I then have Director auto send me straight to a file called ‘Home’ which is my splash page. Clicking this image on the splash page takes me to Menu.lua

For testing purposes, i set menu.lua to show me the value of textToDisplay on the screen, and it does. It shows 33.

I then go to the next screen where i have a text input box. This screen is called ‘singlelockdate’

I think this is where it is all going wrong. I have the user input a number into the text box, and it appears under the text box. However if i go back a page using a home button i put in, then the value is not updated. The Menu screen will still show the original value of 33.

Here is the code for Menu, and singlelockdate screens.

Thanks for any guidance, i will get there in the end!

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


– Call in in the save file thing
require “saveit”
– Done!

local background = display.newImage (“blackbackground.png”)
localGroup:insert(background)
–> This sets the background

local homeButton = display.newImage(“Home.png”)
localGroup:insert(homeButton)

local redbutton = display.newImage (“redbutton.png”)
redbutton.x = 160
redbutton.y = 100
localGroup:insert(redbutton)

local textstuff = display.newText(textToDisplay,0,0,native.systemFont,16)
textstuff:setTextColor( 0, 0, 255, 255)
textstuff.x = 160
textstuff.y = 180
localGroup:insert(textstuff)

local bluebutton = display.newImage (“bluebutton.png”)
bluebutton.x = 160
bluebutton.y = 225
localGroup:insert(bluebutton)

local yellowbutton = display.newImage (“yellowbutton.png”)
yellowbutton.x = 160
yellowbutton.y = 350
localGroup:insert(yellowbutton)
–> This places our three buttons

local function pressRed (event)
if event.phase == “ended” then
director:changeScene (“newselflock”)
end
end

redbutton:addEventListener (“touch”, pressRed)

local function pressBlue (event)
if event.phase == “ended” then
director:changeScene (“singleLockDate”)
end
end

bluebutton:addEventListener (“touch”, pressBlue)

local function pressYellow (event)
if event.phase == “ended” then
director:changeScene (“yellow”)
end
end

yellowbutton:addEventListener (“touch”, pressYellow)

local function pressHome (event)
if event.phase == “ended” then
director:changeScene (“home”)
end
end

homeButton:addEventListener(“touch”, pressHome)
–> This adds the functions and listeners to each button



return localGroup
end
–> This is how we end every file except for director and main, as mentioned in my first comment[/lua]

And singlelockdate screen

[lua]module(…, package.seeall)
local widget = require “widget”
widget.setTheme(“theme_ios”)
function new()
local localGroup = display.newGroup()
–> This is how we start every single file or “screen” in our folder, except for main.lua
– and director.lua
–> director.lua is NEVER modified, while only one line in main.lua changes, described in that file


– Let’s add a background image
local background = display.newImage(“blackbackground.png”)
localGroup:insert(background)

– Now a home button so i can move around easier!
local homeButton = display.newImage(“Home.png”)
localGroup:insert(homeButton)
– Now we need to add some text input boxes, erm somehow!
local textstuff = display.newText(textToDisplay,0,0,native.systemFont,23)
textstuff.x = 150
textstuff.y = 150
localGroup:insert(textstuff)
local function test (event)
if event.phase == “submitted” then
textstuff.text = inputField.text
end
end

inputField = native.newTextField( 20, 30, 280, 30, test)
inputField.font = native.newFont( native.systemFontBold, 14 )
inputField.inputType = “number”
localGroup:insert(inputField)

– end above

– Add a function for when home is clicked, and a listener
local function pressHome (event)
if event.phase == “ended” then
textToDisplay.text = textstuff.text
director:changeScene (“menu”)
end
end

homeButton:addEventListener(“touch”, pressHome)
– Done with above


return localGroup
end
–> This is how we end every file except for director and main, as mentioned in my first comment[/lua] [import]uid: 120612 topic_id: 26651 reply_id: 326651[/import]

Thats because your not updating the value?

In your pressHome function add this line before the changeScene command:

_G.textToDisplay = textstuff.text [import]uid: 84637 topic_id: 26651 reply_id: 108066[/import]

Thank you for that, i figured it out about 1 hour ago and forgot to update. Such a simple thing, yet i could not pin point it :frowning: The debugger console was not much help :slight_smile:

Hopefully i won’t be back with any more help threads for a long time! Thanks for the solution :slight_smile: [import]uid: 120612 topic_id: 26651 reply_id: 108080[/import]