Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Newb question about text input and variables
Started by pinballgamer Jul 10 2018 12:55 PM

4 replies to this topic
text input variables

Best Answer pinballgamer , 10 July 2018 - 07:45 PM

I found the solution. 

I used the cabinet file from- https://github.com/Wavalab/corona-cabinet

 

I've included here for anyone who wants to see what I did

 

 

Main.lua

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------


-- Your code here


local composer = require( "composer" )


display.setStatusBar(display.HiddenStatusBar)


local options = {
   effect = "fade",
   time = 500,
}


composer.gotoScene( "scene1", options )

scene1.lua

 

local composer = require( "composer" )
--local globalData = require( "globalData" )


local cabinet = require("cabinet")


local scene = composer.newScene()










-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------








-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------


-- create()
function scene:create( event )


    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen


-- TextField Listener
local function fieldHandler( event )
  
    if ( "began" == event.phase ) then


    elseif ( "editing" == event.phase ) then
         
                   
myTextString = event.target.text 
    elseif ( "ended" == event.phase ) then


     
           
        native.setKeyboardFocus( nil )
    end
end




inputTextField = native.newTextField(150,100, 200, 50) 
inputTextField:addEventListener("userInput", fieldHandler)


timer.performWithDelay(50, function()native.setKeyboardFocus( inputTextField )end, 1)






cabinet.set("playerName",myTextString)








 local function onNextTouch( event )
if ("ended" == event.phase ) then
composer.gotoScene("scene2")
end
end






 local nextButton = display.newText( "Next", 150, 400, native.systemFont, 75 )
nextButton:setFillColor(25,25,4)




nextButton:addEventListener( "touch", onNextTouch )


sceneGroup:insert(inputTextField)
sceneGroup:insert(nextButton)












end






-- show()
function scene:show( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)


    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen


    end
end














-- hide()
function scene:hide( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)


    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen


    end
end




-- destroy()
function scene:destroy( event )


    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view


end




-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------


return scene

scene2.lua

 

local composer = require( "composer" )


--local globalData = require( "globalData" )
local cabinet = require("cabinet")


local scene = composer.newScene()






-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------








-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------


-- create()
function scene:create( event )


    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen


  cabinet.get("playerName")


 --local currentPlayer = globalData.playerName


 local nametext = display.newText("player " ..myTextString, 100,300,75,100)
nametext:setFillColor(25,25,4)








end








-- show()
function scene:show( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)


    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen


    end
end




-- hide()
function scene:hide( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)


    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen


    end
end




-- destroy()
function scene:destroy( event )


    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view


end




-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------


return scene

and last but not least... cabinet.lua

 

--[[
  Cabinet v1.1 | MIT | github.com/wavalab/corona-cabinet
  Save/retrieve Tables, Numbers, Strings & Booleans via persistent JSON file.
--]]


local j = require "json"


local cabinet = {}


local function rj(fname, dir)
  local t, f = {}, io.open(system.pathForFile(fname, dir), "r")
  if not f then return end
  t = j.decode(f:read("*a"))
  io.close(f)
  return t
end


local function wj(t, fname, dir)
  local f = io.open(system.pathForFile(fname, dir), "w")
  if not f then return end
  f:write(j.encode(t))
  io.close(f)
end


local f = "cabinet.json"
local d = system.DocumentsDirectory
local t = rj(f, d) or {}


function cabinet.set(k, v)
  t[k] = v
  wj(t, f, d)
end


function cabinet.get(k)
  return t[k]
end


function cabinet.remove(k)
  t[k] = nil
  wj(t, f, d)
end


function cabinet.clear()
  t = {}
  wj(t, f, d)
end


return cabinet

you will also need a blank caninet.json file. 

 

i hope this is of some help to someone. It was a strugle, but I made it. best way to learn! 

thanks to those who provided input for me.

 

Thanks,\

Mike

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

pinballgamer

[GLOBAL: userInfoPane.html]
pinballgamer
  • Observer

  • 4 posts
  • Corona SDK

Hi all,

 

Getting heavy into corona and loving it. Working on a simple project and i'be been having a heck of a time. Been through many documents, forums and tutorials and no luck making this work. should be simple.

 

I'm using composer to go from scene to scene

 

main goes to scene1

 

scene one has a text field for player to enter name.

goes to scene 2

 

scene 2 displays players name.

 

Cant get it to work. 

 

any help on a way to structure this simply would be awesome. biggest issue is that i cant get it to display a variable as test on the screen. cant tell if the firld is working because i cant get it to bloody work.

 

Many thanks

Mike



[TOPIC: post.html]
#2

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 325 posts
  • Corona SDK

Hi pinballgamer,

 

Try start  from Getting Started. This is good start point for beginners. It uses text object and composer for simple game project.

 

Good luck :)

 

 

ldurniat


Edited by ldurniat, 10 July 2018 - 11:50 PM.


[TOPIC: post.html]
#3

pinballgamer

[GLOBAL: userInfoPane.html]
pinballgamer
  • Observer

  • 4 posts
  • Corona SDK

Thanks ldurniat,

 

I appreciate the input. Been through this a bunch. Got all of this going except passing a text field into a variable and then displaying it.

 

When I do it as a straight variable

 

I'll include my short screens

 

***edited. I had the wrong scene2. now its not a copy of 1***

 

I have a globalData.lua file to store stuff.

-- Pseudo-global space
local M = {}


return M
 
 
then i have my main.lua
 
 
local composer = require( "composer" )


display.setStatusBar(display.HiddenStatusBar)


local options = {
   effect = "fade",
   time = 500,
}


composer.gotoScene( "scene1", options )

 

 

next is scene1.lua this has the input field

 

local composer = require( "composer" )
local globalData = require( "globalData" )


local scene = composer.newScene()










-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------








-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------


-- create()
function scene:create( event )


    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen


end




-- show()
function scene:show( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)


    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen


    end
end


myTextString = ""


-- TextField Listener
local function fieldHandler( event )
  
    if ( "began" == event.phase ) then


    elseif ( "editing" == event.phase ) then
         
        myTextString = event.target.text            


    elseif ( "ended" == event.phase ) then
     
        myTextString = event.target.text     
        native.setKeyboardFocus( nil )
    end
end




inputTextField = native.newTextField(150,100, 200, 50) 
inputTextField:addEventListener("userInput", fieldHandler)


timer.performWithDelay(50, function()native.setKeyboardFocus( inputTextField )end, 1)




globalData.playerName = myTextString




 local function onNextTouch( event )
if ("ended" == event.phase ) then
composer.gotoScene("scene2")
end
end






 local nextButton = display.newText( "Next", 150, 400, native.systemFont, 75 )
nextButton:setFillColor(25,25,4)


nextButton:addEventListener( "touch", onNextTouch )






-- hide()
function scene:hide( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)


    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen


    end
end




-- destroy()
function scene:destroy( event )


    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view


end




-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------


return scene
 
and finally scene2.lua that should display the name
 

local composer = require( "composer" )


local globalData = require( "globalData" )


local scene = composer.newScene()






-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------








-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------


-- create()
function scene:create( event )


    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen


end




 local currentPlayer = globalData.playerName


 local nametext = display.newText("player " ..currentPlayer, 100,300,75,100)
nametext:setFillColor(25,25,4)








-- show()
function scene:show( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)


    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen


    end
end




-- hide()
function scene:hide( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)


    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen


    end
end




-- destroy()
function scene:destroy( event )


    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view


end




-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------


return scene

 

 

I think i've got it all right, but it wont pass the variable. It leaves the field where it should display it by the word player as empty.

 

ideas?

 

Thanks.



[TOPIC: post.html]
#4

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 188 posts
  • Corona SDK

Do you read the composer.setVariable()?
In the other scene use composer.getVariable(), also have a params to pass thru scenes... I'm a newbie too so I just want to give you my 2 cents of what I learned. Cheers!

[TOPIC: post.html]
#5

pinballgamer

[GLOBAL: userInfoPane.html]
pinballgamer
  • Observer

  • 4 posts
  • Corona SDK

  Best Answer

I found the solution. 

I used the cabinet file from- https://github.com/Wavalab/corona-cabinet

 

I've included here for anyone who wants to see what I did

 

 

Main.lua

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------


-- Your code here


local composer = require( "composer" )


display.setStatusBar(display.HiddenStatusBar)


local options = {
   effect = "fade",
   time = 500,
}


composer.gotoScene( "scene1", options )

scene1.lua

 

local composer = require( "composer" )
--local globalData = require( "globalData" )


local cabinet = require("cabinet")


local scene = composer.newScene()










-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------








-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------


-- create()
function scene:create( event )


    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen


-- TextField Listener
local function fieldHandler( event )
  
    if ( "began" == event.phase ) then


    elseif ( "editing" == event.phase ) then
         
                   
myTextString = event.target.text 
    elseif ( "ended" == event.phase ) then


     
           
        native.setKeyboardFocus( nil )
    end
end




inputTextField = native.newTextField(150,100, 200, 50) 
inputTextField:addEventListener("userInput", fieldHandler)


timer.performWithDelay(50, function()native.setKeyboardFocus( inputTextField )end, 1)






cabinet.set("playerName",myTextString)








 local function onNextTouch( event )
if ("ended" == event.phase ) then
composer.gotoScene("scene2")
end
end






 local nextButton = display.newText( "Next", 150, 400, native.systemFont, 75 )
nextButton:setFillColor(25,25,4)




nextButton:addEventListener( "touch", onNextTouch )


sceneGroup:insert(inputTextField)
sceneGroup:insert(nextButton)












end






-- show()
function scene:show( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)


    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen


    end
end














-- hide()
function scene:hide( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)


    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen


    end
end




-- destroy()
function scene:destroy( event )


    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view


end




-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------


return scene

scene2.lua

 

local composer = require( "composer" )


--local globalData = require( "globalData" )
local cabinet = require("cabinet")


local scene = composer.newScene()






-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------








-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------


-- create()
function scene:create( event )


    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen


  cabinet.get("playerName")


 --local currentPlayer = globalData.playerName


 local nametext = display.newText("player " ..myTextString, 100,300,75,100)
nametext:setFillColor(25,25,4)








end








-- show()
function scene:show( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)


    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen


    end
end




-- hide()
function scene:hide( event )


    local sceneGroup = self.view
    local phase = event.phase


    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)


    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen


    end
end




-- destroy()
function scene:destroy( event )


    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view


end




-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------


return scene

and last but not least... cabinet.lua

 

--[[
  Cabinet v1.1 | MIT | github.com/wavalab/corona-cabinet
  Save/retrieve Tables, Numbers, Strings & Booleans via persistent JSON file.
--]]


local j = require "json"


local cabinet = {}


local function rj(fname, dir)
  local t, f = {}, io.open(system.pathForFile(fname, dir), "r")
  if not f then return end
  t = j.decode(f:read("*a"))
  io.close(f)
  return t
end


local function wj(t, fname, dir)
  local f = io.open(system.pathForFile(fname, dir), "w")
  if not f then return end
  f:write(j.encode(t))
  io.close(f)
end


local f = "cabinet.json"
local d = system.DocumentsDirectory
local t = rj(f, d) or {}


function cabinet.set(k, v)
  t[k] = v
  wj(t, f, d)
end


function cabinet.get(k)
  return t[k]
end


function cabinet.remove(k)
  t[k] = nil
  wj(t, f, d)
end


function cabinet.clear()
  t = {}
  wj(t, f, d)
end


return cabinet

you will also need a blank caninet.json file. 

 

i hope this is of some help to someone. It was a strugle, but I made it. best way to learn! 

thanks to those who provided input for me.

 

Thanks,\

Mike




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: text input, variables