Form Handling - Basic Game, asking for Players name

Hi all,

I am relatively new.

I’ve decided to try my hand at building a point and click mystery adventure game.

My girls love playing these by other developers, one of whome I have spoken with and said he built his apps using Corona engine.

So…

My question…

I want to ask the player for their name. To then personalise the story naration.

I’ve successfully coded the ability to receive in the players name, store it to memory, and retreive it.

What I am struglling with…

  1. If I try to skip or save without entering a name. A runtime error occurs. I can not find how to manage this. Not all players will want to enter their name. At which point I will use a defult “friend”.

  2. Similar to above, but if theplayer clicks on the input field, but then still does not enter a name, again it causes a runtime error. I’ve tried everything but as I am knew I am probably missing a trick somewhere.

How do you handle input fields, when the field is no accessed and even if it is, but no data inputted.

Thanks

Angela

I would suggest looking through the sample apps that come with Corona. In the SampleCode/Interface folder there are two native text field sample apps.

You can also look at https://github.com/coronalabs-samples/business-app-sample

for a complete “Business App” style sample, which has example form input.

But you’re likely not catching the end/cancelled events correctly or trying to access a variable that’s nil and not testing for it. But we really can’t help you unless you post the error message (copy/paste from the console log) and show us the relevant code around where the error is occurring.

Rob

Those Runtime errors occur because because you are trying to reference a variable that is nil. The safest way to avoid such things is to give the variable a default value from the beginning.

I took ~5 minutes to scrape this together from Corona docs just to give you a general idea on how to approach something like this.

local widget = require( "widget" ) local playerName = "friend" local defaultField local function textListener( event ) if event.phase ~= "began" then print( "Current name: " .. event.target.text ) end end local function alertListener( event ) if event.action == "clicked" then local i = event.index if i == 1 then if defaultField.text:len() \> 0 then print( "Using custom name: " .. defaultField.text ) playerName = defaultField.text -- update playerName else print( "Using default name: " .. playerName ) end --\> Player has chosen to continue, so clear the UI and move on. elseif i == 2 then print( "Going back to change the name." ) end end end local function handleButtonEvent( event ) if event.phase == "ended" then if defaultField.text:len() \> 0 then local alert = native.showAlert( "Confirm", "Is your name " .. defaultField.text .. "?", { "Yes", "No, change it." }, alertListener ) else local alert = native.showAlert( "Confirm", "Do you want to continue without giving a name, " .. playerName .. "?", { "Yes", "No, change it." }, alertListener ) end end end local button = widget.newButton( { left = display.contentCenterX, top = display.contentCenterY + 40, label = "Continue", fontSize = 40, onEvent = handleButtonEvent } ) button.anchorX = 1 defaultField = native.newTextField( display.contentCenterX, display.contentCenterY, 200, 64 ) defaultField:addEventListener( "userInput", textListener )