Kilik Sky
Kilik Sky

Reputation: 161

How to pass variable from another lua file?

How to pass variable from another lua file? Im trying to pass the text variable title to another b.lua as a text.

a.lua

local options = {
    title = "Easy - Addition", 
    backScene = "scenes.operationMenu", 
}

b.lua

   local score_label_2 = display.newText({parent=uiGroup, text=title, font=native.systemFontBold, fontSize=128, align="center"})

Upvotes: 7

Views: 14550

Answers (2)

Burns
Burns

Reputation: 146

You can import the file a.lua into a variable, then use it as an ordinary table.

in b.lua

local a = require("a.lua")
print(a.options.title)

Upvotes: -1

greatwolf
greatwolf

Reputation: 20838

There are a couple ways to do this but the most straightforward is to treat 'a.lua' like a module and import it into 'b.lua' via require

For example in

-- a.lua
local options =
{
  title = "Easy - Addition",
  backScene = "scenes.operationMenu",
}

return options

and from

-- b.lua
local options = require 'a'
local score_label_2 = display.newText
  {
    parent = uiGroup,
    text = options.title,
    font = native.systemFontBold,
    fontSize = 128,
    align = "center"
  }    

Upvotes: 4

Related Questions