Get subtable from module table?

I have several tables for different text styles, like:

local p = { -- Paragraph x = \_W, width = display.contentWidth - margin\*2, font = "brandon\_reg", fontSize = 14, text = "" }

I don’t wanna declare these tables at the top of every scene, so I’ve put them in a module (is this a good idea btw?)

local M = { -- Paragraph p = { x = \_W, width = display.contentWidth - margin\*2, font = "brandon\_reg", fontSize = 14, text = "" }, } return M

Then in my scene I need to get the table back - what’s the syntax for this?

Really I’m looking to get the subtable like (this doesn’t work)

local myData = require ("myData") local p = myData.p

Then later in the scene I can

local text = display.newText (p) 

Thanks for any help! I really appreciate it :slight_smile:

** UPDATED TO FIX SYNTAX ERROR **

local M = {} M.p = { ["x"] = \_W, ["width"] = display.contentWidth - margin\*2, ["font"] = "brandon\_reg", ["fontSize"] = 14, ["text"] = "" } return M

** UPDATED TO FIX SYNTAX ERROR **

Or if you want to maintain the structure you have then you’ll need to use the correct Lua Table index sytax:

local M = { -- Paragraph ["p"] = { ["x"] = \_W, ["width"] = display.contentWidth - margin\*2, ["font"] = "brandon\_reg", ["fontSize"] = 14, ["text"] = "" }, } return M

Thanks roaminggamer, I played with the syntax again, but the problem is that I can’t use the table for display.newText().

I’m able to print out the values like so:

-- myData.lua local M = {} M.p = { x = \_W, width = display.contentWidth - margin\*2, font = "brandon\_reg", fontSize = 14, text = "" } return M

-- scene.lua local myData = require ("myData") local p = myData.p print (p.fontSize)

and 14 is printed out as specified.

However, 

display.newText (p)

returns a ‘nil’ error? Am I being a bit dumb here? whatevs, thanks for your help so far!

** ANSWER UPDATED** SYNTAX NOT TESTED SO THERE MAY BE ERRORS BUT THIS IS CLOSE **

Here is an original example from here:

https://docs.coronalabs.com/daily/api/library/display/newText.html#multi-line-text-with-alternate-alignment

Packaged the way you’re trying to do it:

mytext.lua

local m = { ["options1"] = { ["text"] = "The quick brown fox jumped over the lazy dog.", ["x"] = 90, ["width"] = 120, ["font"] = native.systemFont, ["fontSize"] = 18 }, ["options2"] = { ["text"] = "The quick brown fox jumped over the lazy dog, then jumped back again.", ["x"] = 230, ["width"] = 120, ["font"] = native.systemFont, ["fontSize"] = 18 }, } return m

The usage:

local stuff = require "mytext" display.newText( stuff.options1 )

By the way, in the future, grab a table dumper and inspect the structure of the table so you understand what you’re creating better by seeing it in the console:

  • table.dump() - First level print
  • table.print_r() - Recursive print of whole table (doesn’t handle looped tables)

http://github.com/roaminggamer/SSKCorona/blob/master/ssk/extensions/table.lua

requires that you first require this file (has dependency on string extentions):

http://github.com/roaminggamer/SSKCorona/blob/master/ssk/extensions/string.lua

I’m giving this advice because the primary issue is you’re creating a table but getting some of the syntax wrong and without dumping it to the console you’ve got no way to know you’ve made a mistake.  Dumping and recursive printing of a table are both invaluable debug options that I use every day.  

Example:

local bob = { loves = { "sue", "pringles", "money" }, age = 20 } table.dump( bob ) table.print\_r( bob )

 Produces this easy to read output:

Table Dump: ----- age (string) == 20 (number) loves (string) == table: 038E5358 (table) ----- table: 038E5628 { [loves] =\> table: 038E5628 { [1] =\> "sue" [2] =\> "pringles" [3] =\> "money" } [age] =\> 20 }

Worked great roaminggamer, thank for your help! :slight_smile:

** UPDATED TO FIX SYNTAX ERROR **

local M = {} M.p = { ["x"] = \_W, ["width"] = display.contentWidth - margin\*2, ["font"] = "brandon\_reg", ["fontSize"] = 14, ["text"] = "" } return M

** UPDATED TO FIX SYNTAX ERROR **

Or if you want to maintain the structure you have then you’ll need to use the correct Lua Table index sytax:

local M = { -- Paragraph ["p"] = { ["x"] = \_W, ["width"] = display.contentWidth - margin\*2, ["font"] = "brandon\_reg", ["fontSize"] = 14, ["text"] = "" }, } return M

Thanks roaminggamer, I played with the syntax again, but the problem is that I can’t use the table for display.newText().

I’m able to print out the values like so:

-- myData.lua local M = {} M.p = { x = \_W, width = display.contentWidth - margin\*2, font = "brandon\_reg", fontSize = 14, text = "" } return M

-- scene.lua local myData = require ("myData") local p = myData.p print (p.fontSize)

and 14 is printed out as specified.

However, 

display.newText (p)

returns a ‘nil’ error? Am I being a bit dumb here? whatevs, thanks for your help so far!

** ANSWER UPDATED** SYNTAX NOT TESTED SO THERE MAY BE ERRORS BUT THIS IS CLOSE **

Here is an original example from here:

https://docs.coronalabs.com/daily/api/library/display/newText.html#multi-line-text-with-alternate-alignment

Packaged the way you’re trying to do it:

mytext.lua

local m = { ["options1"] = { ["text"] = "The quick brown fox jumped over the lazy dog.", ["x"] = 90, ["width"] = 120, ["font"] = native.systemFont, ["fontSize"] = 18 }, ["options2"] = { ["text"] = "The quick brown fox jumped over the lazy dog, then jumped back again.", ["x"] = 230, ["width"] = 120, ["font"] = native.systemFont, ["fontSize"] = 18 }, } return m

The usage:

local stuff = require "mytext" display.newText( stuff.options1 )

By the way, in the future, grab a table dumper and inspect the structure of the table so you understand what you’re creating better by seeing it in the console:

  • table.dump() - First level print
  • table.print_r() - Recursive print of whole table (doesn’t handle looped tables)

http://github.com/roaminggamer/SSKCorona/blob/master/ssk/extensions/table.lua

requires that you first require this file (has dependency on string extentions):

http://github.com/roaminggamer/SSKCorona/blob/master/ssk/extensions/string.lua

I’m giving this advice because the primary issue is you’re creating a table but getting some of the syntax wrong and without dumping it to the console you’ve got no way to know you’ve made a mistake.  Dumping and recursive printing of a table are both invaluable debug options that I use every day.  

Example:

local bob = { loves = { "sue", "pringles", "money" }, age = 20 } table.dump( bob ) table.print\_r( bob )

 Produces this easy to read output:

Table Dump: ----- age (string) == 20 (number) loves (string) == table: 038E5358 (table) ----- table: 038E5628 { [loves] =\> table: 038E5628 { [1] =\> "sue" [2] =\> "pringles" [3] =\> "money" } [age] =\> 20 }

Worked great roaminggamer, thank for your help! :slight_smile: