Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"})
like image 631
Kilik Sky Avatar asked Oct 18 '22 19:10

Kilik Sky


1 Answers

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"
  }    
like image 158
greatwolf Avatar answered Oct 21 '22 03:10

greatwolf