Trying to get a string to appear from a library

I created a Library  myData.lua with this info I got from codebase, I changed  testFunction1and inserted a string

[lua]

local M = {}

local testFunction1 = function()

  star1=display.newText( “go back”, w/2, h/6, “marker felt”, 75 )

end

– assign a reference to the above local function

M.testFunction1 = testFunction1

local testFunction2 = function()

    print( “Test 2” )

end

M.testFunction2 = testFunction2

– Finally, return the table to be used locally elsewhere

return M

[/lua]

then in a separate file level1.lua I try to call the string testFunction1like so:

[lua]

local myData = require( “myData” )

local options1 = 

{

    text = myData.testFunction1(),

    x = w/2,

    y= h/6,

    width = 800,     --required for multi-line and alignment

    font = “marker felt”,

    fontSize = 75,

    align = “center”,

}

local myText1 = display.newText( options1 )

myText1:setFillColor( 0, 0, 0 )

group:insert( myText1 )

[/lua]

I keep getting errors, i can’t put the function in the text because it wants a string, if i put

text = “myData.testFunction1()”,it prints exactly that - myData.testFunction1()

Hi,

Your ‘testFunction1’ is not returning a string. Actually, it is not returning anything.

Return a string from ‘testFunction1’ and your code will work.

I thought I was returning a string with

  1. local testFunction1 = function()
  2.   star1=display.newText( “go back”, w/2, h/6, “marker felt”, 75 )
  3. end

How do I go about returning a string from  ‘testFunction1’?

return star1

Thank you, now Im getting error

" bad argument #-1 to ‘newText’ (string expected, got table)"

In refrence to ‘text = myData.testFunction1(),’

It wants something in quotes “”  But if I put it in quotes it just says “myData.testFunction1()” instead of “go back”.  How would I get it to reference the library function?

display.newText returns a ‘DisplayObject’, not a string.

so if you do ‘return star1’, you are returning a display object to the calling function.

If you want a string then you should return a string.

In your case use: return “go back”

or:

local testFunction1 = function() local myString = "go back" return myString end

Got it Thank you!!!

Your welcome. Glad i could help.

Hi,

Your ‘testFunction1’ is not returning a string. Actually, it is not returning anything.

Return a string from ‘testFunction1’ and your code will work.

I thought I was returning a string with

  1. local testFunction1 = function()
  2.   star1=display.newText( “go back”, w/2, h/6, “marker felt”, 75 )
  3. end

How do I go about returning a string from  ‘testFunction1’?

return star1

Thank you, now Im getting error

" bad argument #-1 to ‘newText’ (string expected, got table)"

In refrence to ‘text = myData.testFunction1(),’

It wants something in quotes “”  But if I put it in quotes it just says “myData.testFunction1()” instead of “go back”.  How would I get it to reference the library function?

display.newText returns a ‘DisplayObject’, not a string.

so if you do ‘return star1’, you are returning a display object to the calling function.

If you want a string then you should return a string.

In your case use: return “go back”

or:

local testFunction1 = function() local myString = "go back" return myString end

Got it Thank you!!!

Your welcome. Glad i could help.