Having an issue with myData.lua?

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,

  local myData = {} myData.var1 = "textbox text" return myData  

  local composer = require( "composer" ) local scene = composer.newScene() local widget = require( "widget" ) local myData = require( "myData" )   function scene:create( event ) local sceneGroup = self.view   local textBox = native.newTextField(  150, 150, 180, 30 )   --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   -- if you want to assign the myData.var1 to the textBox textBox.text = myData.var1 print( myData.var1 ) end return true     end   --btnSend local btnSend = widget.newButton {   width = 150,   height = 40,   defaultFile = "btnSend.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   ---------------------------------------------------------------------------------   scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene )   -----------------------------------------------------------------------------------------   return scene  

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.