How to set text to a variable

Hi,
I want to give users the ability to enter their name. I am using the textfield option with the following example code

local name  
local function textListener( event )  
 if event.phase == "editing" then  
 name=nil  
 print( event.text )  
 name=event.text  
 end  
 if event.phase == "submitted" then  
 print("This is the name:"..name)  
 end  
end  
  
-- Create our Text Field  
defaultField = native.newTextField( 10, 30, 180, 30 )  
defaultField:addEventListener( "userInput", textListener )  

Is this the correct way?
Is there a better way to set the event.text to a variable.
If I don’t do it in the editing phase I get a nil value in the submitted. If I just use print (event.text)
Why I set it to nil first? That way I know it will be clear when I set the text to the variable. Cause i the editing phase it keeps updating as I type.
My option doesn’t seem the way to do this.

Thanks [import]uid: 100901 topic_id: 36960 reply_id: 336960[/import]

Not really aware of the details of how that listener works, but…

  1. Name is already nil when you declare it, so you can drop the first line inside the editing phase.
  2. You should use “elseif” instead of “end” on line 7. Currently it’s checking both event types which doesn’t make sense so it’s only called once per event anyway.
  3. You’re not changing event.text to anything here; it’s quite the opposite. You’re setting the name variable to event.text. So I’m not sure how anything is changing… [import]uid: 41884 topic_id: 36960 reply_id: 145188[/import]

Well let me explain how the listener works.

When you start typing each letter you type is being printed as you type.
So the first time the name variable is indeed nil.
after you started typing it is set to the new word as it appears on screen.
p
pa
pat
pate
pater
Is the output.
event.text is being changed as you type the word.
So I am changing event.text.
The event Listener is being called continuously durin the typing phase.
So not sure if elseif is handy here. I want it to happen if the submitted phase is started. not if editing is not being done. It could be “ended” for instance.

What I am doing is working, just not sure if this is the optimal way. [import]uid: 100901 topic_id: 36960 reply_id: 145191[/import]

Why do you even need the event listener?
Why not just have the Text Box and a button that says Submit?
When the Submit button is pressed get the value in the text box.
This solves for the user pressing the backspace as well, seems much simpler? [import]uid: 202464 topic_id: 36960 reply_id: 145200[/import]

How would I get the text from the textfield?
That is what I use the listener for. [import]uid: 100901 topic_id: 36960 reply_id: 145204[/import]

Found had it.
Had to go to google to search for a tutorial that was hosted on this website.
I really do not like the corona search function.
there is a .text property That I missed reading the docs for textfield. [import]uid: 100901 topic_id: 36960 reply_id: 145228[/import]

Not really aware of the details of how that listener works, but…

  1. Name is already nil when you declare it, so you can drop the first line inside the editing phase.
  2. You should use “elseif” instead of “end” on line 7. Currently it’s checking both event types which doesn’t make sense so it’s only called once per event anyway.
  3. You’re not changing event.text to anything here; it’s quite the opposite. You’re setting the name variable to event.text. So I’m not sure how anything is changing… [import]uid: 41884 topic_id: 36960 reply_id: 145188[/import]

Well let me explain how the listener works.

When you start typing each letter you type is being printed as you type.
So the first time the name variable is indeed nil.
after you started typing it is set to the new word as it appears on screen.
p
pa
pat
pate
pater
Is the output.
event.text is being changed as you type the word.
So I am changing event.text.
The event Listener is being called continuously durin the typing phase.
So not sure if elseif is handy here. I want it to happen if the submitted phase is started. not if editing is not being done. It could be “ended” for instance.

What I am doing is working, just not sure if this is the optimal way. [import]uid: 100901 topic_id: 36960 reply_id: 145191[/import]

Why do you even need the event listener?
Why not just have the Text Box and a button that says Submit?
When the Submit button is pressed get the value in the text box.
This solves for the user pressing the backspace as well, seems much simpler? [import]uid: 202464 topic_id: 36960 reply_id: 145200[/import]

How would I get the text from the textfield?
That is what I use the listener for. [import]uid: 100901 topic_id: 36960 reply_id: 145204[/import]

Found had it.
Had to go to google to search for a tutorial that was hosted on this website.
I really do not like the corona search function.
there is a .text property That I missed reading the docs for textfield. [import]uid: 100901 topic_id: 36960 reply_id: 145228[/import]

Not really aware of the details of how that listener works, but…

  1. Name is already nil when you declare it, so you can drop the first line inside the editing phase.
  2. You should use “elseif” instead of “end” on line 7. Currently it’s checking both event types which doesn’t make sense so it’s only called once per event anyway.
  3. You’re not changing event.text to anything here; it’s quite the opposite. You’re setting the name variable to event.text. So I’m not sure how anything is changing… [import]uid: 41884 topic_id: 36960 reply_id: 145188[/import]

Well let me explain how the listener works.

When you start typing each letter you type is being printed as you type.
So the first time the name variable is indeed nil.
after you started typing it is set to the new word as it appears on screen.
p
pa
pat
pate
pater
Is the output.
event.text is being changed as you type the word.
So I am changing event.text.
The event Listener is being called continuously durin the typing phase.
So not sure if elseif is handy here. I want it to happen if the submitted phase is started. not if editing is not being done. It could be “ended” for instance.

What I am doing is working, just not sure if this is the optimal way. [import]uid: 100901 topic_id: 36960 reply_id: 145191[/import]

Why do you even need the event listener?
Why not just have the Text Box and a button that says Submit?
When the Submit button is pressed get the value in the text box.
This solves for the user pressing the backspace as well, seems much simpler? [import]uid: 202464 topic_id: 36960 reply_id: 145200[/import]

How would I get the text from the textfield?
That is what I use the listener for. [import]uid: 100901 topic_id: 36960 reply_id: 145204[/import]

Found had it.
Had to go to google to search for a tutorial that was hosted on this website.
I really do not like the corona search function.
there is a .text property That I missed reading the docs for textfield. [import]uid: 100901 topic_id: 36960 reply_id: 145228[/import]

Not really aware of the details of how that listener works, but…

  1. Name is already nil when you declare it, so you can drop the first line inside the editing phase.
  2. You should use “elseif” instead of “end” on line 7. Currently it’s checking both event types which doesn’t make sense so it’s only called once per event anyway.
  3. You’re not changing event.text to anything here; it’s quite the opposite. You’re setting the name variable to event.text. So I’m not sure how anything is changing… [import]uid: 41884 topic_id: 36960 reply_id: 145188[/import]

Well let me explain how the listener works.

When you start typing each letter you type is being printed as you type.
So the first time the name variable is indeed nil.
after you started typing it is set to the new word as it appears on screen.
p
pa
pat
pate
pater
Is the output.
event.text is being changed as you type the word.
So I am changing event.text.
The event Listener is being called continuously durin the typing phase.
So not sure if elseif is handy here. I want it to happen if the submitted phase is started. not if editing is not being done. It could be “ended” for instance.

What I am doing is working, just not sure if this is the optimal way. [import]uid: 100901 topic_id: 36960 reply_id: 145191[/import]

Why do you even need the event listener?
Why not just have the Text Box and a button that says Submit?
When the Submit button is pressed get the value in the text box.
This solves for the user pressing the backspace as well, seems much simpler? [import]uid: 202464 topic_id: 36960 reply_id: 145200[/import]

How would I get the text from the textfield?
That is what I use the listener for. [import]uid: 100901 topic_id: 36960 reply_id: 145204[/import]