How to transfer datas in LUA ?

Hi guys, 

I’m working on a game, and I have a simple question I think, but I really don’t know how to do it !

I have this : 

 btn\_back = widget.newButton { defaultFile = "images/boutons/b\_back.png", width = 200, height = 200, left = 150, top = 80, onRelease = onbtn\_back } btn\_newyork = widget.newButton { defaultFile = "images/boutons/b\_newyork.png", width = 200, height = 200, left = 350, top = 250, --onRelease = onbtn\_startlevel1 }

And this :

local function onbtn\_startlevel1() end local function onbtn\_startlevel2() end

I want to go in another file who’s name is Game.lua, where I will put my game logic but I really don’t know how to do it … 

Normally i put a string and i send it by a object to my function but i don’t know how to do it in LUA.

Can someone help me ? 

(i hope, you will understand me ^^)

You need to include your Game.lua file with

local Game = include “Game”

in Game.lua at the end when you have defined all your functions you need to return a table with functions you want to able to access.

For example

local function foo(param)

end

local M = {}

M.foo = foo

return M

then in your button handlers you would call

Game.foo(“my parameter”)

Sorry to be late to answer, i was in class today.

I’m not really sure to understand what you said, 

look at this :

bg_menulvl.lua :

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local widget = require "widget" local game = include "game" local function onbtn\_back() storyboard.gotoScene( "bg\_home", "fade", 500 ) return true end local function onbtn\_startlevel1() end local function onbtn\_startlevel2() end local function onbtn\_startlevel3() end local function onbtn\_startlevel4() end local function onbtn\_startlevel5() end local function onbtn\_startlevel6() end local function onbtn\_startlevelarcade() storyboard.gotoScene( "bg\_arcade", "fade", 500 ) end local function onbtn\_startlevelchallenge() storyboard.gotoScene( "bg\_challenge", "fade", 500 ) end function scene:createScene( event ) local group = self.view local background = display.newImageRect( "images/backgrounds/bg\_menulvl.jpg", display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_back = widget.newButton { defaultFile = "images/boutons/b\_back.png", width = 200, height = 200, left = 150, top = 80, onRelease = onbtn\_back } btn\_newyork = widget.newButton { defaultFile = "images/boutons/b\_newyork.png", width = 200, height = 200, left = 350, top = 250, --onRelease = onbtn\_startlevel1 } btn\_paris = widget.newButton { defaultFile = "images/boutons/b\_paris.png", width = 200, height = 200, left = 800, top = 180, --onRelease = onbtn\_startlevel2 } btn\_lecaire = widget.newButton { defaultFile = "images/boutons/b\_lecaire.png", width = 200, height = 200, left = 890, top = 400, --onRelease = onbtn\_startlevel3 } btn\_tokyo = widget.newButton { defaultFile = "images/boutons/b\_tokyo.png", width = 200, height = 200, left = 1550, top = 300, --onRelease = onbtn\_startlevel4 } btn\_moscou = widget.newButton { defaultFile = "images/boutons/b\_moscou.png", width = 200, height = 200, left = 1100, top = 170, --onRelease = onbtn\_startlevel5 } btn\_rio = widget.newButton { defaultFile = "images/boutons/b\_rio.png", width = 200, height = 200, left = 500, top = 650, --onRelease = onbtn\_startlevel6 } btn\_arcade = widget.newButton { defaultFile = "images/boutons/b\_arcade.png", width = 360, height = 128, left = 800, top = 600, onRelease = onbtn\_startlevelarcade } btn\_challenge = widget.newButton { defaultFile = "images/boutons/b\_challenge.png", width = 481, height = 128, left = 740, top = 750, onRelease = onbtn\_startlevelchallenge } group:insert( background ) group:insert( btn\_back ) group:insert( btn\_newyork ) group:insert( btn\_paris ) group:insert( btn\_lecaire ) group:insert( btn\_tokyo ) group:insert( btn\_moscou ) group:insert( btn\_rio ) group:insert( btn\_arcade ) group:insert( btn\_challenge ) end scene:addEventListener( "createScene", scene ) return scene

And

game.lua

local game = {} local widget = require "widget" local function onbtn\_pause() local Game Game = game.new("images/backgrounds/bg\_pause.jpg") Game:pauseGame() end local function onbtn\_home() storyboard.gotoScene( "bg\_home", "fade", 500 ) end local function onbtn\_map() storyboard.gotoScene( "bg\_menulvl", "fade", 500 ) end local function onbtn\_score() storyboard.gotoScene( "bg\_score", "fade", 500 ) end function game.new(levelN) local group = self.view function initGame() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_pause = widget.newButton { defaultFile = "images/boutons/b\_pause.png", width = 150, height = 150, left = 1700, top = 40, onRelease = onbtn\_pause } -- Game logic to play end function initPause() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_son = widget.newButton { defaultFile = "images/boutons/b\_son\_on.png", width = 200, height = 200, left = 150, top = 80, --onRelease = onbtn\_faq } btn\_fx = widget.newButton { defaultFile = "images/boutons/b\_fx\_on.png", width = 200, height = 200, left = 150, top = 300, --onRelease = onbtn\_faq } btn\_reprendre = widget.newButton { defaultFile = "images/boutons/b\_play.png", width = 200, height = 200, left = 1650, top = 80, onRelease = onbtn\_reprendre } btn\_recommencer = widget.newButton { defaultFile = "images/boutons/b\_replay.png", width = 200, height = 200, left = 1650, top = 300, onRelease = onbtn\_recommencer } btn\_home = widget.newButton { defaultFile = "images/boutons/b\_home.png", width = 200, height = 200, left = 150, top = 800, onRelease = onbtn\_home } btn\_map = widget.newButton { defaultFile = "images/boutons/b\_map.png", width = 200, height = 200, left = 400, top = 800, onRelease = onbtn\_map } btn\_score = widget.newButton { defaultFile = "images/boutons/b\_score.png", width = 200, height = 200, left = 1650, top = 820, onRelease = onbtn\_score } end function game:startGame() initGame() end function game:pauseGame() initPause() end group:insert( background ) return game end return game

When i’m on my file bg_menulvl.lua, I want to click on one of my 6 functions, with a special backgrounds for each of them, and after when i press the buttons pause, I want to go on the storyboard,

but when i try your technique, i fail, and don’t know why =/

Please post the error you are getting or explain what goes wrong.

One problem I see in function game.new(levelN) is “group = self.view” this will call game.view which I don’t see you setting anywhere.

in local function onbtn_startlevel1() you would have to call local Game = game.new(“something.png”) Game:initGame(). But this will not change scene it will just paint over your existing scene.

This looks strange to me though. Why don’t you make your game.lua a storyboard scene? Then the pause menu would be another storyboard scene that you show as an overlay (look at storyboard.showOverlay). The way you are doing it now it will just show what ever you create in initGame() over the current scene. Is that what you want? Also doing it like this you would have to remove the game menu yourself somewhere. Also I think you don’t need another background in initPause() or are you doing that to hide the game behind?

If you keep it like this I would suggest that in your new function you create a new object not set references to the module variable game as you are overwriting it’s functions every time you call new.

If you don’t change game.lua to storyboard then in game.new do this:

function game.new(levelN) local group = display.newGroup() function initGame() ... end function initPause() ... end function group:startGame() initGame() end function group:pauseGame() initPause() end group:insert( background ) return group end

If you get stuck again please post the exact description of the problem you are seeing and any errors from the terminal window.

Good luck

HUm i alaways forget to check the console =/

I will try and i will ask you if i have problem (but i think it’s work soon ! :slight_smile: )

thank you !

You need to include your Game.lua file with

local Game = include “Game”

in Game.lua at the end when you have defined all your functions you need to return a table with functions you want to able to access.

For example

local function foo(param)

end

local M = {}

M.foo = foo

return M

then in your button handlers you would call

Game.foo(“my parameter”)

Sorry to be late to answer, i was in class today.

I’m not really sure to understand what you said, 

look at this :

bg_menulvl.lua :

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local widget = require "widget" local game = include "game" local function onbtn\_back() storyboard.gotoScene( "bg\_home", "fade", 500 ) return true end local function onbtn\_startlevel1() end local function onbtn\_startlevel2() end local function onbtn\_startlevel3() end local function onbtn\_startlevel4() end local function onbtn\_startlevel5() end local function onbtn\_startlevel6() end local function onbtn\_startlevelarcade() storyboard.gotoScene( "bg\_arcade", "fade", 500 ) end local function onbtn\_startlevelchallenge() storyboard.gotoScene( "bg\_challenge", "fade", 500 ) end function scene:createScene( event ) local group = self.view local background = display.newImageRect( "images/backgrounds/bg\_menulvl.jpg", display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_back = widget.newButton { defaultFile = "images/boutons/b\_back.png", width = 200, height = 200, left = 150, top = 80, onRelease = onbtn\_back } btn\_newyork = widget.newButton { defaultFile = "images/boutons/b\_newyork.png", width = 200, height = 200, left = 350, top = 250, --onRelease = onbtn\_startlevel1 } btn\_paris = widget.newButton { defaultFile = "images/boutons/b\_paris.png", width = 200, height = 200, left = 800, top = 180, --onRelease = onbtn\_startlevel2 } btn\_lecaire = widget.newButton { defaultFile = "images/boutons/b\_lecaire.png", width = 200, height = 200, left = 890, top = 400, --onRelease = onbtn\_startlevel3 } btn\_tokyo = widget.newButton { defaultFile = "images/boutons/b\_tokyo.png", width = 200, height = 200, left = 1550, top = 300, --onRelease = onbtn\_startlevel4 } btn\_moscou = widget.newButton { defaultFile = "images/boutons/b\_moscou.png", width = 200, height = 200, left = 1100, top = 170, --onRelease = onbtn\_startlevel5 } btn\_rio = widget.newButton { defaultFile = "images/boutons/b\_rio.png", width = 200, height = 200, left = 500, top = 650, --onRelease = onbtn\_startlevel6 } btn\_arcade = widget.newButton { defaultFile = "images/boutons/b\_arcade.png", width = 360, height = 128, left = 800, top = 600, onRelease = onbtn\_startlevelarcade } btn\_challenge = widget.newButton { defaultFile = "images/boutons/b\_challenge.png", width = 481, height = 128, left = 740, top = 750, onRelease = onbtn\_startlevelchallenge } group:insert( background ) group:insert( btn\_back ) group:insert( btn\_newyork ) group:insert( btn\_paris ) group:insert( btn\_lecaire ) group:insert( btn\_tokyo ) group:insert( btn\_moscou ) group:insert( btn\_rio ) group:insert( btn\_arcade ) group:insert( btn\_challenge ) end scene:addEventListener( "createScene", scene ) return scene

And

game.lua

local game = {} local widget = require "widget" local function onbtn\_pause() local Game Game = game.new("images/backgrounds/bg\_pause.jpg") Game:pauseGame() end local function onbtn\_home() storyboard.gotoScene( "bg\_home", "fade", 500 ) end local function onbtn\_map() storyboard.gotoScene( "bg\_menulvl", "fade", 500 ) end local function onbtn\_score() storyboard.gotoScene( "bg\_score", "fade", 500 ) end function game.new(levelN) local group = self.view function initGame() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_pause = widget.newButton { defaultFile = "images/boutons/b\_pause.png", width = 150, height = 150, left = 1700, top = 40, onRelease = onbtn\_pause } -- Game logic to play end function initPause() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_son = widget.newButton { defaultFile = "images/boutons/b\_son\_on.png", width = 200, height = 200, left = 150, top = 80, --onRelease = onbtn\_faq } btn\_fx = widget.newButton { defaultFile = "images/boutons/b\_fx\_on.png", width = 200, height = 200, left = 150, top = 300, --onRelease = onbtn\_faq } btn\_reprendre = widget.newButton { defaultFile = "images/boutons/b\_play.png", width = 200, height = 200, left = 1650, top = 80, onRelease = onbtn\_reprendre } btn\_recommencer = widget.newButton { defaultFile = "images/boutons/b\_replay.png", width = 200, height = 200, left = 1650, top = 300, onRelease = onbtn\_recommencer } btn\_home = widget.newButton { defaultFile = "images/boutons/b\_home.png", width = 200, height = 200, left = 150, top = 800, onRelease = onbtn\_home } btn\_map = widget.newButton { defaultFile = "images/boutons/b\_map.png", width = 200, height = 200, left = 400, top = 800, onRelease = onbtn\_map } btn\_score = widget.newButton { defaultFile = "images/boutons/b\_score.png", width = 200, height = 200, left = 1650, top = 820, onRelease = onbtn\_score } end function game:startGame() initGame() end function game:pauseGame() initPause() end group:insert( background ) return game end return game

When i’m on my file bg_menulvl.lua, I want to click on one of my 6 functions, with a special backgrounds for each of them, and after when i press the buttons pause, I want to go on the storyboard,

but when i try your technique, i fail, and don’t know why =/

Please post the error you are getting or explain what goes wrong.

One problem I see in function game.new(levelN) is “group = self.view” this will call game.view which I don’t see you setting anywhere.

in local function onbtn_startlevel1() you would have to call local Game = game.new(“something.png”) Game:initGame(). But this will not change scene it will just paint over your existing scene.

This looks strange to me though. Why don’t you make your game.lua a storyboard scene? Then the pause menu would be another storyboard scene that you show as an overlay (look at storyboard.showOverlay). The way you are doing it now it will just show what ever you create in initGame() over the current scene. Is that what you want? Also doing it like this you would have to remove the game menu yourself somewhere. Also I think you don’t need another background in initPause() or are you doing that to hide the game behind?

If you keep it like this I would suggest that in your new function you create a new object not set references to the module variable game as you are overwriting it’s functions every time you call new.

If you don’t change game.lua to storyboard then in game.new do this:

function game.new(levelN) local group = display.newGroup() function initGame() ... end function initPause() ... end function group:startGame() initGame() end function group:pauseGame() initPause() end group:insert( background ) return group end

If you get stuck again please post the exact description of the problem you are seeing and any errors from the terminal window.

Good luck

HUm i alaways forget to check the console =/

I will try and i will ask you if i have problem (but i think it’s work soon ! :slight_smile: )

thank you !