How i can remove local objects?

Hi, im having some troubles im 2 hours trying to solve this but cant!!

I have some objects, and background, this is loggin page (main.lua), this should be: if password empty error (works) if password match with (user:profesor, pw:coromines) goes to composer(primera.lua) this doesnt works and if password doesnt match and its not empty shows user/pw incorrect this works too.

local background = display.newImageRect( "fondo6.png", 600, 350 ) background.x = display.contentCenterX background.y = display.contentCenterY display.setStatusBar( display.HiddenStatusBar ) local widget = require "widget" local composer = require "composer" local toast = require('plugin.toast') local scene = composer.newScene() local function iniciarboto( self, event ) if ( usuarisessio.text == "" or contrasenyasessio.text == "" ) then local alert = native.showAlert( "Els camps estan buits!", "Has d'omplir tots els camps.", { "D'acord" } ) return true elseif ( usuarisessio.text == "profesor" or contrasenyasessio.text == "coromines" ) then title:removeSelf() usuari:removeSelf() pw:removeSelf() iniciar:removeSelf() usuarisessio:removeSelf() contrasenyasessio:removeSelf() -- title = nil usuari = nil pw= nil iniciar= nil usuarisessio= nil contrasenyasessio= nil composer.gotoScene( "primera" ) return false else local alert = native.showAlert( "¡Usuari o contrasenya incorrecta!", "Torna a intentar.", { "D'acord" } ) return true end end ---- Objects ---- local title = display.newText( "Iniciar sessió", display.contentCenterX, 16, "Lifecraft.ttf", 32 ) title:setFillColor( 1, 0.88, 0.3 ) local usuari = display.newText( "Usuari", 200, 230, "Lifecraft.ttf", 24 ) usuari.x = 105 usuari.y = 90 local pw = display.newText( "Contrasenya", 200, 230, "Lifecraft.ttf", 24 ) pw.x = 130 pw.y = 160 local iniciar = display.newImageRect( "iniciaricono.png", 200, 40 ) iniciar.x = display.contentCenterX iniciar.y = 230 iniciar.tap = iniciarboto iniciar:addEventListener( "tap" ) local usuarisessio = native.newTextField( 320, 94, 180, 30 ) usuarisessio.placeholder = "Escriu l'usuari" local contrasenyasessio = native.newTextField( 320, 159, 180, 30 ) contrasenyasessio.placeholder = "Escriu la contrasenya" contrasenyasessio.isSecure = true

With this i get this error: 

71145084d5.png

PD: If you see any other error please comment for solve it.

PD2: return true and false are well used? I mean are not the other way around?

Thanks!

I’m not totally sure, but i found out that you MUST declare all objects before declaring a function that needs them, even if the function only works when the program is finishing, because it only searches in the code that’s BEFORE it is created,  try it like this:

[lua]

local background = display.newImageRect( “fondo6.png”, 600, 350 )
background.x = display.contentCenterX
background.y = display.contentCenterY

display.setStatusBar( display.HiddenStatusBar )

local widget = require “widget”
local composer = require “composer”
local toast = require(‘plugin.toast’)
local scene = composer.newScene()

---- Objects ----    
    
local title = display.newText( “Iniciar sessió”, display.contentCenterX, 16, “Lifecraft.ttf”, 32 )
title:setFillColor( 1, 0.88, 0.3 )

local usuari = display.newText( “Usuari”, 200, 230, “Lifecraft.ttf”, 24 )
usuari.x = 105
usuari.y = 90

local pw = display.newText( “Contrasenya”, 200, 230, “Lifecraft.ttf”, 24 )
pw.x = 130
pw.y = 160

local iniciar = display.newImageRect( “iniciaricono.png”, 200, 40 )
iniciar.x = display.contentCenterX
iniciar.y = 230
iniciar.tap = iniciarboto
iniciar:addEventListener( “tap” )

local usuarisessio = native.newTextField( 320, 94, 180, 30 )
usuarisessio.placeholder = “Escriu l’usuari”

local contrasenyasessio = native.newTextField( 320, 159, 180, 30 )
contrasenyasessio.placeholder = “Escriu la contrasenya”
contrasenyasessio.isSecure = true

local function iniciarboto( self, event )
if ( usuarisessio.text == “” or contrasenyasessio.text == “” ) then
    local alert = native.showAlert( “Els camps estan buits!”, “Has d’omplir tots els camps.”, { “D’acord” } )
    return true
    
elseif ( usuarisessio.text == “profesor” or contrasenyasessio.text == “coromines” ) then
    title:removeSelf()
    usuari:removeSelf()
    pw:removeSelf()    
    iniciar:removeSelf()
    usuarisessio:removeSelf()
    contrasenyasessio:removeSelf()
    –
    title = nil
    usuari = nil
    pw= nil
    iniciar= nil
    usuarisessio= nil
    contrasenyasessio= nil
    composer.gotoScene( “primera” )
    return false
    
else
    local alert = native.showAlert( “¡Usuari o contrasenya incorrecta!”, “Torna a intentar.”, { “D’acord” } )
    return true
        

end
end   

[/lua]

By doing that you should stop having that issue

@alangerardosanchez is exactly right. You’ve hit on an issue known as “scope”. If you’re coming to Lua from a language like C it can be a hard adjustment.

Languages like C are two-pass compilers. The first pass goes and collects all of the symbols (functions, variables, etc.) and assigns them memory addresses. The second pass compiles the code, replacing the variable names with the assigned memory addresses. By doing this, the order of when things are declared becomes less important.

Lua is a one-pass compiler. When it sees a variable or function that it doesn’t know about already, it assigns it a memory address in global memory which is initialized to nil.  Later on, it see’s your “local” definition, and generates a different memory address. The net effect is that you need to declare things before you use them. You don’t necessarily have to write the code for a function or assign values to variables when you declare them, you just need to declare them

We have a great tutorial on scope: https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

That will help you understand this.

Rob

thanks guys @alangerardosanchez and @Rob Miracle

I managed a way to fix it, thanks for your help!!

I’m not totally sure, but i found out that you MUST declare all objects before declaring a function that needs them, even if the function only works when the program is finishing, because it only searches in the code that’s BEFORE it is created,  try it like this:

[lua]

local background = display.newImageRect( “fondo6.png”, 600, 350 )
background.x = display.contentCenterX
background.y = display.contentCenterY

display.setStatusBar( display.HiddenStatusBar )

local widget = require “widget”
local composer = require “composer”
local toast = require(‘plugin.toast’)
local scene = composer.newScene()

---- Objects ----    
    
local title = display.newText( “Iniciar sessió”, display.contentCenterX, 16, “Lifecraft.ttf”, 32 )
title:setFillColor( 1, 0.88, 0.3 )

local usuari = display.newText( “Usuari”, 200, 230, “Lifecraft.ttf”, 24 )
usuari.x = 105
usuari.y = 90

local pw = display.newText( “Contrasenya”, 200, 230, “Lifecraft.ttf”, 24 )
pw.x = 130
pw.y = 160

local iniciar = display.newImageRect( “iniciaricono.png”, 200, 40 )
iniciar.x = display.contentCenterX
iniciar.y = 230
iniciar.tap = iniciarboto
iniciar:addEventListener( “tap” )

local usuarisessio = native.newTextField( 320, 94, 180, 30 )
usuarisessio.placeholder = “Escriu l’usuari”

local contrasenyasessio = native.newTextField( 320, 159, 180, 30 )
contrasenyasessio.placeholder = “Escriu la contrasenya”
contrasenyasessio.isSecure = true

local function iniciarboto( self, event )
if ( usuarisessio.text == “” or contrasenyasessio.text == “” ) then
    local alert = native.showAlert( “Els camps estan buits!”, “Has d’omplir tots els camps.”, { “D’acord” } )
    return true
    
elseif ( usuarisessio.text == “profesor” or contrasenyasessio.text == “coromines” ) then
    title:removeSelf()
    usuari:removeSelf()
    pw:removeSelf()    
    iniciar:removeSelf()
    usuarisessio:removeSelf()
    contrasenyasessio:removeSelf()
    –
    title = nil
    usuari = nil
    pw= nil
    iniciar= nil
    usuarisessio= nil
    contrasenyasessio= nil
    composer.gotoScene( “primera” )
    return false
    
else
    local alert = native.showAlert( “¡Usuari o contrasenya incorrecta!”, “Torna a intentar.”, { “D’acord” } )
    return true
        

end
end   

[/lua]

By doing that you should stop having that issue

@alangerardosanchez is exactly right. You’ve hit on an issue known as “scope”. If you’re coming to Lua from a language like C it can be a hard adjustment.

Languages like C are two-pass compilers. The first pass goes and collects all of the symbols (functions, variables, etc.) and assigns them memory addresses. The second pass compiles the code, replacing the variable names with the assigned memory addresses. By doing this, the order of when things are declared becomes less important.

Lua is a one-pass compiler. When it sees a variable or function that it doesn’t know about already, it assigns it a memory address in global memory which is initialized to nil.  Later on, it see’s your “local” definition, and generates a different memory address. The net effect is that you need to declare things before you use them. You don’t necessarily have to write the code for a function or assign values to variables when you declare them, you just need to declare them

We have a great tutorial on scope: https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

That will help you understand this.

Rob

thanks guys @alangerardosanchez and @Rob Miracle

I managed a way to fix it, thanks for your help!!