string passed as an argument turns into a table. lua - lua

I have a function that whenever I pass a string into, it turns into a table and I don't know why. Any help is very appreciated. Here's my code:
function gotoSkip(para)
print(para)
print(type(para))
--scene:removeEventListener( "create", scene )
--composer.gotoScene(para)
end
function scene:create( event )
local sceneGroup = self.view
background = display.newImageRect(sceneGroup,"images/white.jpg",768,1500)
background.x = display.contentCenterX
background.y = display.contentCenterY
local skipButton = display.newText(sceneGroup, "Skip", 600, 1300, nativeSystemFont, 60)
skipButton:setFillColor(0,0,256)
skipButton:addEventListener("tap",gotoSkip, "StringExample")
end
Here is the result of the two print statements respectively:
table: 0D27FB88 (or some other memory address)
table

addEventListener function only gets 2 arguments, so you should remove that third argument.
skipButton:addEventListener("tap", gotoSkip)
If you wanna pass parameter to a function from an Event Listener, there are 2 ways;
1-) You can set a property for event.target;
function gotoSkip(event)
print( event.target.para)
print( type(event.target.para) )
end
local skipButton = display.newText("Skip", 400, 400, nativeSystemFont, 60)
skipButton.para= "StringExample"
skipButton:addEventListener("tap", gotoSkip)
2-) You can use a sub function;
function gotoSkip(para)
print( para )
print( type(para) )
end
local skipButton = display.newText("Skip", 400, 400, nativeSystemFont, 60)
function subFunc()
gotoSkip("StringExample")
end
skipButton:addEventListener("tap", subFunc)
You can remove its listener like this;
skipButton:removeEventListener("tap", subFunc)
Also nativeSystemFont isn't a valid constant unless you defined it. Correct one is native.systemFont.

Related

Corona pass parameter in gotoScene

I refer to corona website reference using these to pass parameters to another scene.
main.lua
local options =
{
effect = "slideLeft",
time = 800,
params = { var1 = "custom", myVar = "another" }
}
storyboard.gotoScene( "notificationPage", options )
then on my other scene
notificationPage.lua
function scene:enterScene( event )
local group = self.view
local params = event.params
print( params.var1 )
print( params.myVar )
end
it return error attempt to index local 'params' (a nil value). why is that ? and how do i do it correctly?
It should be placed in "createScene" instead of "enterScene".
And as you may read in Corona Docs "This library is planned for deprecation. If you are starting a new project, you should use the composer.* scene management library instead."
It's in the 'createScene' event handler only, try this code:
function oScene:createScene( oEvent )
local oGroup = self.view
local aParams = oEvent.params
if aParams then
print (aParams.var1)
print (aParams.myVar)
end
You should use scene:create function:
function scene:create( event )
local sceneGroup = self.view
local params = event.params
print( params.var1 )
print( params.myvar )
end
you can download scene template from coronalabs.com

Corona Sdk - Is there a way to call and group:insert() an object from another lua file?

As stated in the title I would like to not only call an object from an external Lua file, but I would also like to group:insert() this object into my Menu page with the properties given to it in the external lua file. Is this possible and/or efficient? I would just really like to make sure data isn't repeated through out my project.
EDIT
Here's my code so far:
The group:insert() function is throwing me an error stating it was expecting a table and that I might have been trying to call a function in which case i should use ":" instead of "."
This is menu.lua:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local widget = require "widget"
local m = require ("myData")
local menuFunction = require("menuFunction")
local menuSwipe
-- =======================
-- menuSwipe()
-- =======================
menuSwipe = function(self, event)
local phase = event.phase
local touchID = event.id
if(phase == "began") then
elseif(phase == "moved") then
elseif(phase == "ended" or phase == "cancelled") then
if(m.menuActivator > 0) then
menuDown(m.invisiBar, event)
else
--m.layerInfo = layers
transition.to( menuFunction.menuBar, { x = menuFunction.menuBar.x, y = 0, time = 200 } )
--transition.to( layers, { x = menuFunction.menuBar.x, y = h, time = 100 } )
m.invisiBar = display.newRect( 0,0,w,25,6)
m.invisiBar.alpha = 0
m.menuActivator = 1
end
end
end
-- ++++++++++++++++++++++
-- menuDown()
-- ++++++++++++++++++++++
function menuDown(self, event)
local phase = event.phase
local touchID = event.id
if(phase == "began") then
elseif(phase == "moved") then
elseif(phase == "ended" or phase == "cancelled") then
if(m.menuActivator == 1) then
transition.to( menuFunction.menuBar, { x = m.menuInfo.x, y = h*.964, time = 200 } )
--transition.to( group, { x = 0, y = 0, time = 10 } )
m.menuActivator = 0
end
end
end
function scene:createScene( event )
local group = self.view
group:insert( menuFunction.menuBar ) -- *** ERROR occurs here
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
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
This is menuFunction.lua:
local m = require("myData")
local menu = require ("menu")
local w = display.contentWidth
local h = display.contentHeight
local menuFunction = {}
--menuBar
menuFunction.menuBar = display.newImage( "images/menuBar1.png")
menuFunction.menuBar.x = w*(1/2)
menuFunction.menuBar.y = h*1.465
menuFunction.menuBar.height = h
menuFunction.menuBar:setReferencePoint(display.TopLeftReferencePoint)
menuFunction.menuBar.touch = menu.menuSwipe
menuFunction.menuBar:addEventListener("touch", menuFunction.menuBar)
return menuFunction
This is the exact error message:
ERROR: table expected. If this is a function call, you might have used '.' instead of ':'
message**
Does this happen every time this code is called, or does it by any chance work the first time and then crashes? In your case, code could work the first time you enter the scene, but the second time you do, it may crash [if you remove scenes in between].
When you do a 'require' of a file, its contents are executed and returned value is saved in the global packages table. When you require the same file again, the returned value is taken from the global packages table instead, the code is not executed again.
So if you by any chance require this file in one spot of your app, and then call :removeSelf() and nil the reference of the menuBar, the display object will be removed and its reference will cease to exist, and calling the require again, will not recreate the object. Fully removing a scene will also remove the display objects.
So what you wanted to achieve is very sensible [contrary to what #Schollii says], but your "module" should allow creation of multiple objects if you want to get rid of them during runtime.
I'm not going to correct your code, just a simple example of how you can achieve this:
-- menu.lua
local menuCreator = {}
menuCreator.newMenu = function(params)
local menu = display.newGroup()
-- create your menu here
return menu
end
return menuCreator
Now anytime you do:
local menuCreator = require("menu.lua")
you will be able to call:
local menu = menuCreator.newMenu(someParams)
and get yourself a nice new menu wherever you need.
If it's not shown all the time on screen, it may be better to create a new one whenever you need it, and then remove it from the memory.
There are several issues with this, and none of them seem related to your error but fixing them will either also fix the error or make the cause of the error more obvious. Please fix following and update:
Although Lua allows it, don't use circular includes, where A includes B which includes A. Instead have menu require menuFunction and then call a creation function in menuFuntion:
-- menuFunction.lua
local m = require("myData")
-- require("menu") -- BAD! :)
local w = display.contentWidth
local h = display.contentHeight
local menuBar = display.newImage( "images/menuBar1.png")
menuBar.x = w*(1/2)
menuBar.y = h*1.465
menuBar.height = h
menuBar:setReferencePoint(display.TopLeftReferencePoint)
local menuFunction = { menuBar = menuBar }
function createMenuBar(menuSwipe)
menuFunction.menuBar.touch = menuSwipe
menuFunction.menuBar:addEventListener("touch", menuFunction.menuBar)
return menuFunction
end
-- menu.lua
function createScene(event)
local mf = require('menuFunction')
mfFunction = mf.createMenuBar(menuSwipe)
group:insert(menuFunction.menuBar)
end
Secondly out of the four calls to group:insert() the first 3 refer to objects that are not shown in the code and don't see relevant to problem, they should be removed or if you think relevant, comment why their code now shown, or show their code.

listener for multiple text field

I'm trying to call a text field listener from multiple field like this page
http://docs.coronalabs.com/api/library/native/newTextField.html#listener-optional
When user start to write something in input field, handler function is called normally but closure that is located in handler is not called.
login.lua file like below:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- Forward declerations
local userNameField
-- TextField Listener
local function fieldHandler( getObj )
print( "This message is showing up :) " )
-- Use Lua closure in order to access the TextField object
return function( event )
print( "This message is not showing up :( There is something wrong here!!!" )
if ( "began" == event.phase ) then
-- This is the "keyboard has appeared" event
getObj().text = ""
getObj():setTextColor( 0, 0, 0, 255 )
elseif ( "ended" == event.phase ) then
-- This event is called when the user stops editing a field:
-- for example, when they touch a different field or keyboard focus goes away
print( "Text entered = " .. tostring( getObj().text ) ) -- display the text entered
elseif ( "submitted" == event.phase ) then
-- This event occurs when the user presses the "return" key
-- (if available) on the onscreen keyboard
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end -- "return function()"
end
local function userNameFieldHandler( event )
local myfunc = fieldHandler( function() return userNameField end ) -- passes the text field object
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
-- Create our Text Field
userNameField = native.newTextField( display.contentWidth * 0.1, display.contentHeight * 0.5, display.contentWidth * 0.8, display.contentHeight * 0.08)
userNameField:addEventListener( "userInput", userNameFieldHandler )
userNameField.font = native.newFont( native.systemFontBold, 22 )
userNameField.text = "User Name"
userNameField:setTextColor( 0, 0, 0, 12 )
end
Help please...
I don't know Corona, but your code is somewhat strange.
userNameFieldHandler doesn't do much, it just creates a handler calling fieldHandler and stores it in a local that is never used (myfunc). Are you sure you didn't mean this:
local function userNameFieldHandler( event )
local myfunc = fieldHandler(
function() return userNameField end ) -- passes the text field object
return myfunc --<<<<--- added return
end
and maybe when you add the event listener you meant this (note the added ()):
userNameField:addEventListener( "userInput", userNameFieldHandler() )

Lua: how do I add display objects required from external functions to display groups?

In a storyboard scene, I require a bunch of display objects from external functions. When I attempt to add these to the scene's display group, I get the error "table expected."
function scene:createScene(event)
local group=self.view
local shieldDisplay = shieldDisplay.new()
group:insert(shieldDisplay)
end
The external function looks like this:
function shieldDisplay.new()
shieldDisp = display.newText("Shield: "..tostring(Cshield), 1165, 20, native.systemFont, 30)
shieldDisp:setTextColor(9,205,235)
end
return shieldDisplay
What am I doing wrong?
The return object must be inside on the function that you're calling.
function shieldDisplay.new()
local shieldDisp = display.newText("Shield: "..tostring(Cshield), 1165, 20, native.systemFont, 30)
shieldDisp:setTextColor(9,205,235)
return shieldDisp
end
function scene:createScene(event)
local group=self.view
local shieldDisplay = shieldDisplay.new()
group:insert(shieldDisplay)
end
Try changing it to
function scene:createScene(event)
local group=self.view
local shieldDisplay = shieldDisplay.new
group:insert(shieldDisplay)
end

trigger to hide and show table

I am trying to trigger a hide and a show on my table local starTable = {}
local starTable = {} -- Set up star table
local function showStarTable()
-- some trigger to show the star table
end
timer.performWithDelay( 500, showStarTable, 1 )
local function hideStarTable()
-- some trigger to hide the star table
end
timer.performWithDelay( 1000, hideStarTable, 1 )
Is it possible to achieve this
To go along with the first answer here is an example:
local starTable = {}
local star1 = <display object>
starTable:insert(star1)
local function showStarTable()
starTable.alpha = 1
end
timer.performWithDelay( 500, showStarTable, 1 )
local function hideStarTable()
starTable.alpha = 0
end
timer.performWithDelay( 1000, hideStarTable, 1 )
Or of you wanted to stick with an actual table. and without seeing what actually is inserted into your starTable you could try:
local starTable = {}
local star = <display object>
starTable[1] = star
star = <display object>
starTable[2] = star
star = <display object>
starTable[3] = star
local function showStarTable()
for i=1, #starTable do
starTable[i].star.alpha = 1
end
end
timer.performWithDelay( 500, showStarTable, 1 )
local function hideStarTable()
for i=1, #starTable do
starTable[i].star.alpha = 0
end
end
timer.performWithDelay( 1000, hideStarTable, 1 )
However, the first option is better, if it works with your program.
Your code will execute the function showStarTable() after 1/2 second for 1 time. Then in another 1/2 second, it will execute hideStarTable() once.
Display objects like display.newImageRect() are tables, so if that's the table you are referring to, you can show/hide them by changing either the .alpha property of the object or it's visibility (.isVisible = true or .isVisible = false). However tables by themselves are just containers of information and a generic table isn't displayable. It could contain a single display object ore multiple.
It would be your responsibility in your show/hide functions to show/hide the contents of the table, if the table has displayable content.

Resources