Global value?

Hay everyone, I aim wondering how to make a global value. I may not be using the correct wording.

What I really mean is if I have a game that uses multiple .lua files, how do I access a value that is in the previous lua file.

For Examle

one.lua

kurt = display.newText(“kurt”)

two.lua

kurt = display.text

Well i tried that and it didn’t work, but I think it gives you an idea of what i’m looking to do.

–Thanks in advance-- [import]uid: 86879 topic_id: 15257 reply_id: 315257[/import]

here’s how I would do it…

scene1.lua
[lua]local function text()
local object = display.newText(“hello”, … … . )
return object
end[/lua]

scene2.lua
[lua]local scene1 = require(“scene1”)

scene1.text[/lua]
[import]uid: 24641 topic_id: 15257 reply_id: 56328[/import]

how do I load the text onto the new lua file?

And thank you so much [import]uid: 86879 topic_id: 15257 reply_id: 56336[/import]

you load the text by using the [lua] scene1.text [/lua] [import]uid: 24641 topic_id: 15257 reply_id: 56371[/import]

This is what I have, and it isn’t cooperating.
Scene 1

score1 = display.newText(“0”, 0, 0, “Slayer”, 10)
score1:setTextColor(255, 255, 255)
score1.x = 60
score1.y = 30
localGroup:insert(score1)

scene 2

local scene1 = require(“1”)
local bscore1 = display.newText(“0”, 0, 0, “Slayer”, 10)
bscore1:setTextColor(255, 255, 255)
bscore1.x = 54
bscore1.y = 469
localGroup:insert(bscore1)

bscore1.text = scene1.score1
[import]uid: 86879 topic_id: 15257 reply_id: 56392[/import]

scene1.lua

[lua]local M = {}

local function text()
t = display.newText(“hello”,100,100,nil,50)
t:setTextColor(255,255,25)
return t

end

M.text = text

return M[/lua]

main.lua
[lua]local scene1 = require(“scene1”)
–display the text
local t = scene1.text()[/lua] [import]uid: 24641 topic_id: 15257 reply_id: 56398[/import]

does it have to be a function? [import]uid: 86879 topic_id: 15257 reply_id: 56408[/import]

Doesn’t have to be a function, I have a lua file that just contains globals that will be accessed by everything else

Just declare and define then as normal, obvously don’t prefix it with “local”, then include that lua file in your usual script and call that variable name
[import]uid: 71799 topic_id: 15257 reply_id: 56412[/import]

can you give me an example lua file I could take a look at? Thanks [import]uid: 86879 topic_id: 15257 reply_id: 56417[/import]

This isn’t tested but this seems more likely to work :

[code]
Scene 1

score1 = display.newText(“0”, 0, 0, “Slayer”, 10)
score1:setTextColor(255, 255, 255)
score1.x = 60
score1.y = 30
localGroup:insert(score1)

scene 2

local scene1 = require(“1”)

local bscore1 = display.newText(“0”, 0, 0, “Slayer”, 10)
bscore1:setTextColor(255, 255, 255)
bscore1.x = 54
bscore1.y = 469
localGroup:insert(bscore1)

bscore1.text = scene1.score1.text – Note the .text
[/code] [import]uid: 84637 topic_id: 15257 reply_id: 56527[/import]

Here’s another strategy for globals that you may find useful, I just posted a project template blog that demonstrates it…

Project template with a global variables strategy

–croisened [import]uid: 48203 topic_id: 15257 reply_id: 57409[/import]