How to access the "newText" variable into external module?

Hi

I want to access a variable “display.newText” into a module

tools.lua

local module1 = require ("module") local text = display.newText ("text", 500, 500, native.Systemfont, 50)

module.lua

local M = {} - How to access text variables from the tool.lua here? return M

What should I do ?.

Can you give me a guide for this?

thank you

I’m coming up with two solution:

  1. Simple solution (not recommended).

Make text variable global so it can be accessible from any file.

\_G.text = display.newText ("text", 500, 500, native.Systemfont, 50)
  1. Use new module common.lua which groups functions or  variables in one place used by others modules.

    – common.lua local M = {} M.text = display.newText (“text”, 500, 500, native.systemFont, 50) return M

    – module.lua local common = require ‘common’ local M = {} - How to access text variables from the tool.lua here? – common.text is reference to Text object defined in common.lua  print( common.text.text ) return M

Read more:

When one should use local and global functions and variables in corona sdk? (https://stackoverflow.com)

Note: You should use native.systemFont instead of native.Systemfont.

Hi,

Another option:

module.lua

local M = {} function M:init(textField) self.textField = textField end function M:someFunction() self.textField.text = "Here is some text" end return M

tools.lua

local module1 = require("module") local text = display.newText ("text", 500, 500, native.systemFont, 50) --pass a reference to the textField module1:init(text)

-dev

I’m coming up with two solution:

  1. Simple solution (not recommended).

Make text variable global so it can be accessible from any file.

\_G.text = display.newText ("text", 500, 500, native.Systemfont, 50)
  1. Use new module common.lua which groups functions or  variables in one place used by others modules.

    – common.lua local M = {} M.text = display.newText (“text”, 500, 500, native.systemFont, 50) return M

    – module.lua local common = require ‘common’ local M = {} - How to access text variables from the tool.lua here? – common.text is reference to Text object defined in common.lua  print( common.text.text ) return M

Read more:

When one should use local and global functions and variables in corona sdk? (https://stackoverflow.com)

Note: You should use native.systemFont instead of native.Systemfont.

Hi,

Another option:

module.lua

local M = {} function M:init(textField) self.textField = textField end function M:someFunction() self.textField.text = "Here is some text" end return M

tools.lua

local module1 = require("module") local text = display.newText ("text", 500, 500, native.systemFont, 50) --pass a reference to the textField module1:init(text)

-dev