multiple native.newTextFields - strange behaviour on remove

I’ve found a really strange problem when using multiple native text fields, which I cant find documented anywhere else.

Note - building for Android, but the problem is evident in the simulator.

I add my first native text field, and can remove it when I exit the scene, no problem at all.

However, if I add a second native.newTextField *anywhere* in my app (ie, in any scene) I get errors relating to the line where the first one is removed (unless I comment those lines out) saying:

attempt to index global ‘testtext1’ ( a nil value)

If i make these textfields “local” in the enterscene function, it seems that the exitscene function cant see them to remove them.

All i want to do, for several different scenes, is add a native text element on entering the scene, use it to capture some text, and then remove it when I exit to another scene. 

Can i only have 1 textfield in my app?? This seems very limiting if so!

(im using storyboard for scene management)
storyboard.gotoScene(… etc.
 

Code works fine if i only have one textfield, or no calls to removeself.

function scene:enterScene( event ) local group = self.view testtext1 = native.newTextField( namebg.x, namebg.y,namebg.width,namebg.height,regtextSearchListener testtext2 = native.newTextField( surnamebg.x, surnamebg.y, surnamebg.width,surnamebg.height,regtextSearchListener) end function scene:exitScene( event ) local group = self.view testtext1:removeSelf() testtext2:removeSelf() end

Does this happen if you name it different in each scene?
Is testtext1 set to local anywhere in your code? If not at top of code put only

local testtext1

I would put it after all requires

Hi, many thanks - that has helped a bit - in that one scene now behaves consistently.

One of my scenes with a text field in it now works correctly - I go into that scene and exit it, and the text field is added and removed. 

However… another scene that I’ve added a text field for (with a completely different name) in exactly the same way crashes as soon as I enter that scene, saying “attempt to index upvalue ‘textfield2’ (a nil value)” with the line number for  textfield2:removeSelf() in the exit scene function…

…where textfield2 is the name of the text field in my second scene. 

It doesnt seem to make any difference which of the two scenes i visit first either. One works fine, the other does not.

I tried moving the second textfield to some other scenes, same behaviour. 

I tried using a different name, same issue.

 

Can you post one of your scenes code

sure… This is the scene im testing my second textField in, and crashes with “attempt to index upvalue newtesttext (a nil value)” upon entering the scene. 

Line number is the line with “newtesttext:removeSelf()” on it.

The other scene using the same pattern but different local var name for the textfield is working great!

 

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local newtesttext function scene:createScene( event ) local group = self.view local backgroundimage = display.newImage('bg1.jpg',320\*xMultiplier,480\*yMultiplier, true ) backgroundimage.x = 160\*xMultiplier backgroundimage.y = 240\*yMultiplier local titletext = display.newText( "Titletext", 20, 0, 300, 300, native.systemFont, 30 ) titletext:setTextColor( 255 ) -- black titletext:setReferencePoint( display.CenterReferencePoint ) titletext.x = display.contentWidth \* 0.5 + 10 titletext.y = titletext.y + 20 local subtext = display.newText( "subtext", 20, 100, 170, 300, native.systemFont, 20 ) subtext:setTextColor(convHexColor("FF9C00",255)) group:insert(backgroundimage) group:insert(titletext) group:insert(subtext) end function scene:enterScene( event ) local group = self.view newtesttext = native.newTextField(100,100,100,50) end function scene:exitScene( event ) local group = self.view newtesttext:removeSelf() end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

note: if I comment out the removeSelf() line, it no longer crashes and the simulator shows me a grey placeholder for the textField. 

If i print() during the enter and exit scene functions, I do get output in the console with removeSelf()  commented out…

 

Really really weird thing just happened…

If I add another textField to a third scene in the same way, the same thing happens with that scene, but then scene 2’s textField *does* work as expected… and so does scene1’s textField…

All scenes work if I move the removeSelf on scene3’s textfield to the destroyScene function - but then it just stays on the display without being removed.

So, it seems to be one most recently added to my code (!) that causes the scene it’s in to crash.

 

Could you remove all the comments from code above. I’m on mobile and it makes it harder to read

Don’t see code to call gotoScene

Comments removed, gotoScene is elsewhere in the app (in another scene,not in this scene).

That may be where the problem is
Would need to see more code


edit - code replaced by better / fuller / more accurate version in post below


 

That’s more or less the size of it. There are plenty of other scenes that are working fine, and the changeScene() function has been working a treat for a week or so of development. It allows me to precisely define the transitions between one scene and another, and set a var to contain the current scene’s name.

 

Heres a much better, more simplified example that I’ve got running and which illustrates the problem:

main.lua

widget = require "widget" local system = require "system" local storyboard = require "storyboard" local globaldata = require( "globaldata" ) local mime = require("mime") local json = require("json") local rpfunc = require( "myfunctions" ) changeScene("welcome","") 

globaldata.lua

--my global space local M = {} transitionoptions = { effect = "slideLeft", time = 700 } transitionlist = {} transitionlist['welcome\_to\_login'] = {["effect"]="fade",["time"]=500} storeddata = {} storeddatasting = {} storeddata.apitoken = "" storeddata.leagues = {} netavailable = false searchterm = "" searchresults = "" selectedleague = "" defaultrowheight = 50 defaultpad = 10 registerdetalis = {} defaultalpha = 230 return M

login.lua

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local textlogin ------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view local loginbutton = widget.newButton { left = 100, top = 200, width = 150, height = 30, label = "to welcome", onEvent = function() changeScene("welcome",currentscene) end, } group:insert(loginbutton) end -------------------------------------------------------------------------------------- function scene:enterScene( event ) local group = self.view textlogin = native.newTextField(100,100,100,50) end -------------------------------------------------------------------------------------- function scene:exitScene( event ) local group = self.view textlogin:removeSelf() end -------------------------------------------------------------------------------------- function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) ----------------------------------------------------------------------------------------- return scene

welcome.lua

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local textwelcome ------------------------------------------------------------------------------------ function scene:createScene( event ) local group = self.view local startbutton = widget.newButton { left = 100, top = 200, width = 150, height = 30, label = "to login", onEvent = function() changeScene("login",currentscene) end, } group:insert(startbutton) end -------------------------------------------------------------------------------------- function scene:enterScene( event ) local group = self.view textwelcome= native.newTextField(100,100,100,50) end -------------------------------------------------------------------------------------- function scene:exitScene( event ) local group = self.view textwelcome:removeSelf() end -------------------------------------------------------------------------------------- function scene:destroyScene( event ) local group = self.view -- INSERT code here (e.g. remove listeners, remove widgets, save state variables, etc.) end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) ----------------------------------------------------------------------------------------- return scene

myfunctions.lua

local storyboard = require( "storyboard" ) local mime = require("mime") local json = require("json") local widget = require("widget") widget.setTheme( "widget\_theme\_ios") function changeScene(sceneto,scenefrom) --print("changing scene: " .. "from: " .. scenefrom .. " to: " .. sceneto) local testkey = scenefrom .. "\_to\_" .. sceneto local thistransition = {} if (transitionlist[testkey] == nil) then --print("no transition key found: " .. testkey) thistransition = transitionoptions else -- print("found a transition key: " .. testkey) thistransition["effect"] = transitionlist[testkey].effect thistransition["time"] = transitionlist[testkey].time end currentscene = sceneto storyboard.gotoScene(sceneto,thistransition); end

The problem does seem to be my changeScene function :/ 

If anyone can steer me towards why or what I’ve done wrong I’d really appreciate it!

Have you tried putting it in a function by itself and call the function

I’m not sure what you mean, sorry… put what in a function by itself? just storyboard.gotoScene(“scenename”)? 

That seems to work great (unless you hit the button lots of times quickly - not sure how to protect against that) but doesnt show what’s wrong with my way of changing scene, which I really want to use as it gives me a lot of control over transitions etc.

 

I tried this in the full app (rather than my separate simplified demo of the problem) and have no switched every call to my changeScene function to a storyboard.gotoScene(“scenename”) call… and the problem still happens. 

I think I’ve now found the problem.

It’s the scene transitions themselves. I’ve been clicking buttons and interacting with the app, before the transition has completed… so as a button for changing scene slides in, I’ve been clicking it just before the transition stopped - which means the next scene hasn’t fully finished loading at that point I guess. If i go through things much more slowly, and wait on each scene, things work fine.

thanks for helping me work through this!

Argh, i’m actually not correct. It seems to be random as to whether a scene will actually transition in correctly or not, depending on how long I wait before clicking on a button. Crazy.

uttlery stumped here.

Do you have any timers or transition.to going on before scene change
I’m at work right now working with some time sensitive stuff so can’t really stop and help at moment