Having an issue with myData.lua?

Hi guys,

I’m trying to make a scene where text is inputted into a text field then a button send is clicked which sends the inputted text to myData.lua which can then be displayed on the screen with a display.newText, I can see that myData.lua is working fine as I have a value entered which displays on the screen, the issue is that when something is entered into the text box and the button send is clicked nothing happens. Any Ideas?

[lua]

–my global space

local myData = {}

myData.var1 = “textbox text”

return myData

[/lua]

[lua]

local composer = require( “composer” )

local scene = composer.newScene()

local widget = require( “widget” )

local myData = require( “myData” )

function scene:create( event )

local sceneGroup = self.view

–input textbox

local textBox

textBox = native.newTextField( display.contentCenterX, 95, display.contentWidth-50, 30 )

textBox.placeholder = myData.var1

–text for Var1

local textVar1 = display.newText(myData.var1, 

185, 170, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 20 )

textVar1:setFillColor( 0, 0, 0 )

end

–btnSend

local btnSend = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnSend.png”,

   onEvent = btnSend

}

btnSend.x = 235

btnSend.y = 445

–button function

local function btnSend( event )

    myData.var1 = textBox.text

    textBox.placeholder = myData.var1

end

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

end

function scene:destroy( event )

local sceneGroup = self.view

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

[/lua]

Thanks again,

Matt.

line 33 - rename that function to ‘onBtnSend’  (it should not be the same name as the button widget, which you named btnSend)

line 26 - onEvent = btnSend (rename it to match the new name of the function ‘onBtnSend’)

**  move the function (now named ‘onBtnSend’) to line 19, so it is above the definition of the button (btnSend).

OR, ‘forward declare’ the function somewhere above and outside the scene:create function or at the very top of the scene:create function

as such:

– forward declaration

local onBtnSend

* then remove ‘local’ from in front of the function definition of ‘onBtnSend’, since when pre-declaring it you already localized it. Then you do not have to move that function to line 19 as I noted earlier.

Good luck

I tried renaming my button function to onBtnSend and also changed the onEvent to the same, then moved my function above my button definition. What this did is made my button disappear from the screen it no longer appears. So I then tried the other option but forward declaring my function outside the scene:create function and nothing then appeared on the scene, so then tried putting it at the top of the scene:create function and only the button appeared but when clicked on I got an error message. This is how I made it look for the first option I think this is what you meant.

[lua]

local composer = require( “composer” )

local scene = composer.newScene()

local widget = require( “widget” )

local myData = require( “myData” )

function scene:create( event )

local sceneGroup = self.view

–input textbox

local textBox

textBox = native.newTextField( display.contentCenterX, 95, display.contentWidth-50, 30 )

textBox.placeholder = myData.var1

–text for Var1

local textVar1 = display.newText(myData.var1, 

185, 170, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 20 )

textVar1:setFillColor( 0, 0, 0 )

end

–button function

local function onBtnSend( event )

    myData.var1 = textBox.text

    textBox.placeholder = myData.var1

–btnSend

local btnSend = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnNext.png”,

   onEvent = onBtnSend

}

btnSend.x = 235

btnSend.y = 445

end

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

end

function scene:destroy( event )

local sceneGroup = self.view

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

[/lua]

Thanks again,

Matt.

‘end’ on line 38 … move it to line 24

that is why the button is not visible, you had the creation of the button inside the function that never gets called, because the button that calls the function never gets created.

In that function you should enclose the code in this 

if event.phase == “ended” then

end

– add this at the end of the function to prevent touch executing on anything under the button

return true

The button has appeared now thank you, however when I click the button I get an error message saying 

Runtime error

c:\users\matt\documents\corona projects\database\home.lua:24: attempt to index global ‘textBox’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\database\home.lua:24: in function ‘_onEvent’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221>

which I though might be down to the simulator not supporting the textField input so I ran it on my device and got the same error. Any ideas?

Thanks again, you’re a life saver.

Matt.

The textField is part of the ‘native’ library. Those objects are controlled by the OS, not the corona engine…  (so I guess) it is just nor going to recognize that textBox object.  I’ve only used the textField item in one of my apps, and here is how I used it :

&nbsp; &nbsp; local function compInput( e) if e.phase == "ended" then -- do something with textBox's text if e.target.text ~= nil then mySongTable.comp = string.upper(e.target.text) else mySongTable.comp = " " end elseif e.phase == "submitted" then -- this event is dispatched when the user presses the "return" key to finish editing elseif e.phase == "editing" then local txt = e.target.text &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(string.len(txt)\>18)then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt=string.sub(txt, 1, 18) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.target.text=txt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end e.target.text = string.upper(e.target.text) mySongTable.comp = string.upper(e.target.text) end end &nbsp; compTF = native.newTextField( 150, 150, 280, 30 ) compTF.size = 24 compTF.inputType = "string" compTF.text = "" compTF.isEditable = true compTF:addEventListener( "userInput", compInput ) &nbsp;

formatted a little better here:

&nbsp; local function compInput( e)&nbsp; if e.phase == "ended" then -- do something with textBox's text if e.target.text ~= nil then mySongTable.comp = string.upper(e.target.text) else&nbsp; mySongTable.comp = " " end&nbsp; elseif e.phase == "submitted" then&nbsp; -- this event is dispatched when the user presses the "return" key to finish editing elseif e.phase == "editing" then local txt = e.target.text &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(string.len(txt)\>18)then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt=string.sub(txt, 1, 18) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.target.text=txt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end&nbsp; e.target.text = string.upper(e.target.text)&nbsp; mySongTable.comp = string.upper(e.target.text)&nbsp; end&nbsp; end &nbsp; compTF = native.newTextField( 150, 150, 280, 30 ) compTF.size = 24 compTF.inputType = "string" compTF.text = "" compTF.isEditable = true compTF:addEventListener( "userInput", compInput ) &nbsp;

check out this link :  http://docs.coronalabs.com/api/library/native/newTextField.html

Your level of lua seams a bit heavy for me i think, I can’t seam to understand why it is so complex for such a simple task. All I wanted to do is make a button change text on screen using textbox input. I feel like i’m missing one little thing here to make it work, in myData.lua it is working as it appears on screen when a value is pre-entered so it is working on the “get” side of things its just the “set” that is the issue here. What do you advise I do here?

I tried this but i got an error when my button is clicked saying there is a nil value.

[lua]

–button function

function onBtnSend( event )

textBox.text = myData.var1

print( myData.var1 )

    return true

end

[/lua]

So i then removed the (dot)text and i didn’t get an error but nothing happened.

[lua]

–button function

function onBtnSend( event )

textBox = myData.var1

print( myData.var1 )

    return true

end

[/lua]

Any thoughts here?

Thanks again,

Matt.

Sorry Matt,

It was code specific to another app I did, and there was extra code there to control the length the text and other user input, etc…  

Here is a code sample (based on your example code) that works.

* be sure to use the 'if e.phase == “ended” check in the button event function. If you do not, that function will execute that code twice.

In the case of what you are doing here that probably won’t cause issues, but not doing that event.phase check can often lead to hard to find bugs,

&nbsp; local myData = {} myData.var1 = "textbox text" return myData &nbsp;

&nbsp; local composer = require( "composer" ) local scene = composer.newScene() local widget = require( "widget" ) local myData = require( "myData" ) &nbsp; function scene:create( event ) local sceneGroup = self.view &nbsp; local textBox = native.newTextField( &nbsp;150, 150, 180, 30 ) &nbsp; --button function local function onBtnSend( event ) if event.phase == "ended" then -- if you want to assign the text from the textbox to the myData.var1 -- myData.var1 = textBox.text &nbsp; -- if you want to assign the myData.var1 to the textBox textBox.text = myData.var1 print( myData.var1 ) end return true &nbsp; &nbsp; end &nbsp; --btnSend local btnSend = widget.newButton { &nbsp; width = 150, &nbsp; height = 40, &nbsp; defaultFile = "btnSend.png", &nbsp; onEvent = onBtnSend }&nbsp; btnSend.x = 235 btnSend.y = 445 end &nbsp; function scene:show( event ) local sceneGroup = self.view end &nbsp; function scene:hide( event ) local sceneGroup = self.view end &nbsp; function scene:destroy( event ) local sceneGroup = self.view end &nbsp; --------------------------------------------------------------------------------- &nbsp; scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) &nbsp; ----------------------------------------------------------------------------------------- &nbsp; return scene &nbsp;

here are some ‘sample code’ links to check out:

https://coronalabs.com/resources/tutorials/sample-code/

https://coronalabs.com/samples/

Thanks a lot you are really helping me out here, I have had a look at the code you have posted how do I get it to work when I click the my button it changes myData.var1 to whatever was entered in the textbox, as at the moment it sets the text in my textbox to myData.var1.

Thanks again,

Matt. 

Matt,

remove the ‘–’ from this line in the function

– myData.var1 = textBox.text

and ‘remark’ (–) the line 

textBox.text = myData.var1

I had posted a ‘note’ above each of those lines, so you could see how to do either thing. Just remark out whichever one you don’t want to do, and un-remark the one you want.

Hi again,

I did think this was the issue however, I tried it with sending the textbox what is in myData.var1 and it worked fine. However, when I changed it over to sending the textbox text to myData.var1 it didn’t seam like anything was changing I not sure if it is due to there not being a console on my device to see if it prints but I have put a newText to show me what is in myData.var1 but it doesn’t change it for some reason would it do it automatically or do I need to tell it to update when myData.var1 is changed not sure really. What do you think?

Thanks again,

Matt.

Matt,

If you run your code in your Corona Simulator,  any print statement you type in your code such as      print(myData.var1)    will show in the console(terminal window) on your computer.  If you do not have the console window showing on your computer screen when running your app in corona simulator then there is something wrong in your corona setup.

If you are testing on your device, and your device is connected to your computer, you should be able to see any print statements show in the console of Xcode. I assume you are using a mac for that, on PC I am not sure how you would access a console display.  But, in any case, your idea of using a text object to see on your device the change in the myData.var1 is a good way to test that, and it should work.

  1. The myData.var1 should update as soon as you press your ‘btnSend’ button.  But, be sure you have something typed into that textField Box.

  2. You should see that print statement execute immediately in your ‘terminal/console’ window.

  3. The text object your created to see the change to myData.var1, will not automatically update.  You have to assign the myData.var1 to the text member of that text object you created.  In your ‘onBtnSend’ function you will have to assign the myData.var1 to the text object your created like this :

textObject.text = myData.var1

** do this line of code right after the    print(myData.var1)

It does work, I tested it on my corona simulator.

Good luck

Bob

Hi Bob,

That worked perfectly thanks for clearing that up for me.

Matt.

line 33 - rename that function to ‘onBtnSend’  (it should not be the same name as the button widget, which you named btnSend)

line 26 - onEvent = btnSend (rename it to match the new name of the function ‘onBtnSend’)

**  move the function (now named ‘onBtnSend’) to line 19, so it is above the definition of the button (btnSend).

OR, ‘forward declare’ the function somewhere above and outside the scene:create function or at the very top of the scene:create function

as such:

– forward declaration

local onBtnSend

* then remove ‘local’ from in front of the function definition of ‘onBtnSend’, since when pre-declaring it you already localized it. Then you do not have to move that function to line 19 as I noted earlier.

Good luck

I tried renaming my button function to onBtnSend and also changed the onEvent to the same, then moved my function above my button definition. What this did is made my button disappear from the screen it no longer appears. So I then tried the other option but forward declaring my function outside the scene:create function and nothing then appeared on the scene, so then tried putting it at the top of the scene:create function and only the button appeared but when clicked on I got an error message. This is how I made it look for the first option I think this is what you meant.

[lua]

local composer = require( “composer” )

local scene = composer.newScene()

local widget = require( “widget” )

local myData = require( “myData” )

function scene:create( event )

local sceneGroup = self.view

–input textbox

local textBox

textBox = native.newTextField( display.contentCenterX, 95, display.contentWidth-50, 30 )

textBox.placeholder = myData.var1

–text for Var1

local textVar1 = display.newText(myData.var1, 

185, 170, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 20 )

textVar1:setFillColor( 0, 0, 0 )

end

–button function

local function onBtnSend( event )

    myData.var1 = textBox.text

    textBox.placeholder = myData.var1

–btnSend

local btnSend = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnNext.png”,

   onEvent = onBtnSend

}

btnSend.x = 235

btnSend.y = 445

end

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

end

function scene:destroy( event )

local sceneGroup = self.view

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

[/lua]

Thanks again,

Matt.

‘end’ on line 38 … move it to line 24

that is why the button is not visible, you had the creation of the button inside the function that never gets called, because the button that calls the function never gets created.

In that function you should enclose the code in this 

if event.phase == “ended” then

end

– add this at the end of the function to prevent touch executing on anything under the button

return true

The button has appeared now thank you, however when I click the button I get an error message saying 

Runtime error

c:\users\matt\documents\corona projects\database\home.lua:24: attempt to index global ‘textBox’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\database\home.lua:24: in function ‘_onEvent’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221>

which I though might be down to the simulator not supporting the textField input so I ran it on my device and got the same error. Any ideas?

Thanks again, you’re a life saver.

Matt.

The textField is part of the ‘native’ library. Those objects are controlled by the OS, not the corona engine…  (so I guess) it is just nor going to recognize that textBox object.  I’ve only used the textField item in one of my apps, and here is how I used it :

&nbsp; &nbsp; local function compInput( e) if e.phase == "ended" then -- do something with textBox's text if e.target.text ~= nil then mySongTable.comp = string.upper(e.target.text) else mySongTable.comp = " " end elseif e.phase == "submitted" then -- this event is dispatched when the user presses the "return" key to finish editing elseif e.phase == "editing" then local txt = e.target.text &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(string.len(txt)\>18)then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt=string.sub(txt, 1, 18) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.target.text=txt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end e.target.text = string.upper(e.target.text) mySongTable.comp = string.upper(e.target.text) end end &nbsp; compTF = native.newTextField( 150, 150, 280, 30 ) compTF.size = 24 compTF.inputType = "string" compTF.text = "" compTF.isEditable = true compTF:addEventListener( "userInput", compInput ) &nbsp;

formatted a little better here:

&nbsp; local function compInput( e)&nbsp; if e.phase == "ended" then -- do something with textBox's text if e.target.text ~= nil then mySongTable.comp = string.upper(e.target.text) else&nbsp; mySongTable.comp = " " end&nbsp; elseif e.phase == "submitted" then&nbsp; -- this event is dispatched when the user presses the "return" key to finish editing elseif e.phase == "editing" then local txt = e.target.text &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(string.len(txt)\>18)then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt=string.sub(txt, 1, 18) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.target.text=txt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end&nbsp; e.target.text = string.upper(e.target.text)&nbsp; mySongTable.comp = string.upper(e.target.text)&nbsp; end&nbsp; end &nbsp; compTF = native.newTextField( 150, 150, 280, 30 ) compTF.size = 24 compTF.inputType = "string" compTF.text = "" compTF.isEditable = true compTF:addEventListener( "userInput", compInput ) &nbsp;

check out this link :  http://docs.coronalabs.com/api/library/native/newTextField.html