I am using director and I would like to make it so if you press a button on one screen, that changes a variable to be used on another screen.
Example code that does not work because I am dumb:
Screen1.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | module(..., package.seeall) -- Main function - MUST return a display.newGroup() color = 0; function new() local localGroup = display.newGroup() --PreStuFZ -- Background local background = display.newImage("bg.png") localGroup:insert(background) -- --Menu local bt01 = display.newRect(100,150,100,50) local function bt01t ( event ) if event.phase == "ended" then director:changeScene("screen2","moveFromLeft") end end bt01:addEventListener("touch",bt01t) bt01.x = 160 bt01.y = 200 localGroup:insert(bt01) -- local bt02 = display.newRect(100,50,100,50) local function bt02t ( event ) if event.phase == "ended" then color = 256; end end bt02:addEventListener("touch",bt02t) bt02.x = 180 bt02.y = 270 localGroup:insert(bt02) -- MUST return a display.newGroup() return localGroup end |
local bt01 = display.newRect(100,150,100,50)
local function bt01t ( event )
if event.phase == "ended" then
_G["color"] = 0
director:changeScene("screen2","moveFromLeft")
end
end
I use this _G["color"] = 0 a lot.. but you should make it local on the next screen for speed.
I tried that and all I get is this error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | WARNING: display.setStatusBarMode() not supported in the simulator for the current device Runtime error error loading module 'screen1' from file 'E:\Users\Du\Desktop\App\VarsExam\screen1.lua': E:\Users\Du\Desktop\App\VarsExam\screen1.lua:29: '<name>' expected near '[' stack traceback: [C]: ? [C]: ? [C]: in function 'require' Runtime error: error loading module 'screen1' from file 'E:\Users\Du\Desktop\App\VarsExam\screen1.lua': E:\Users\Du\Desktop\App\VarsExam\screen1.lua:29: '<name>' expected near '[' stack traceback: [C]: ? [C]: ? [C]: in function 'require' E:\Users\DuSchiRuntime error E:\Users\Du\Desktop\App\VarsExam\director.lua:135: ERROR: table expected. If this is a function call, you might have used '.' instead of ':' stack traceback: [C]: ? [C]: in function 'insert' E:\Users\Du\Desktop\App\VarsExam\d |
The answer to your question is very simple. Just declare a variable in main.lua, like this:
1 | xyz = 123 |
This still seems to have no effect!
Main.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 | display.setStatusBar( display.HiddenStatusBar ) color = 0 local director = require("director") local mainGroup = display.newGroup() local function main() mainGroup:insert(director.directorView) director:changeScene("screen1") return true end main() |
Dude, you forgot to mention what you're expecting vs. what you're getting. That's a lot of code to try to guess at it.
Button pressed changes a value on one screen. That same value sticks if it switches to another screen.
Not specific enough, sorry, I'm tired and going to bed. I'm willing to help, but I'm not going to wade through 75 lines of code to fix your problem for you when you don't even bother to define what the problem is in specific terms. Have you tried reducing the problem to something very very simple (10-20 lines), like the example I posted?
lococo: "Unless you say "local", all variables in Lua are global."
that's not actually quite true
mainmenu.lua
1 2 3 4 5 6 | module(..., package.seeall) function doSomething() print("dosomething in mainmenu") xyz=345 _G["globalXYZ"]="DEF" end |
Yep, it looks like you're right. It seems that module(..., package.seeall)
is the culprit here, which is interesting. If you define the mainmenu.lua module like this, and leave all other code untouched, it works fine:
1 2 3 4 5 6 7 | local M = {} function M.doSomething() print("dosomething in mainmenu") xyz=345 _G["globalXYZ"]="DEF" end return M |
that's not actually quite true
huh, thanks for that example. That could be what's causing the tiny memory leak I wasted hours on yesterday before giving up.
lococo, in your example where you remove "module", it's essentially the same as if you put your mainmenu code inside your main.lua. that's all require is really doing there, that's why it works
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | local M = {} function M.doSomething() print("dosomething in mainmenu") xyz=345 _G["globalXYZ"]="DEF" end local mainmenu=M xyz = 123 _G["globalXYZ"]="ABC" print("xyz="..xyz) -- 123 print("globalxyz=".._G["globalXYZ"]) -- ABC mainmenu.doSomething() print("xyz="..xyz) -- still 123 print("globalxyz=".._G["globalXYZ"]) -- DEF |
One of the effects of the module function is to change the environment of the code that calls module. What that means is that any global assignments in the rest of that file go into the module table, rather than the thread environment. Using package.seeall with module allows global lookups to retrieve values from the thread global environment (which is how reference is to _G work inside a module declaration, actually), but assignments go into the module table.
This usually still means they're globally accessible. If you make a global assignment in a module "m" to the global "xyz", outside code can retrieve it as "m.xyz", because module typically creates the module table in a global variable.
Environments are one of the most confusing aspects of Lua but also among the most powerful.
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Quick question to do with collisions
CPM 2.2.2 little bug
when I playing a video,can I skip it by a touch ?
Flurry support in simulator
The Addictive Card Game, Kiki's Koi-Koi, is Now Available for iOS
How to create and use multi-sequence sprites?
how to check print statements
network.download problem ANSCA HELP!
RIP Steve Jobs - 1955 > 2011