Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to access the "newText" variable into external module?
Started by cornfield May 29 2018 12:10 AM

2 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

cornfield

[GLOBAL: userInfoPane.html]
cornfield
  • Observer

  • 6 posts
  • Corona SDK

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

 



[TOPIC: post.html]
#2

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 365 posts
  • Corona SDK

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)

2. 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:

 

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


Edited by ldurniat, 29 May 2018 - 02:35 AM.


[TOPIC: post.html]
#3

Develephant

[GLOBAL: userInfoPane.html]
Develephant
  • Corona Geek

  • 1,440 posts
  • Corona SDK

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




[topic_controls]
[/topic_controls]