Calling object from other lua files

Hi guys,

So I’m trying to clean up my code by adding some functions to a separate lua file. This way, I’d not always have to copy and paste the code when I have to use it again in my levels.

Now, I was able to make functions work across the files, but how can I manage to transfer objects like rectangles, images etc from the separate file to the levels? So for example

I’ve got this code:

----------------------------------------------------------------------------------------- -- -- cityBackgroundPowerupSelection.lua -- ----------------------------------------------------------------------------------------- local M = {} local loadsave = require("loadsave") settings = loadsave.loadTable("settingsGame.json", system.DocumentsDirectory) local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 local centerX, centerY = display.contentCenterX, display.contentCenterY local centerX, centerY = display.contentCenterX, display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local originX = display.screenOriginX local originY = display.screenOriginY local maxWidth = display.screenOriginX + actualW local maxHeight = display.screenOriginY + actualH local background1 background1 = display.newRect(centerX, centerY, actualW, actualH) background1:setFillColor(41/255, 128/255, 185/255) background1.alpha = 0 background1 = M.background

I’d now like to add the background to my sceneGroup in my levels file. How could I do this?

Kind regards

Bram

I use lua to store all my info.
https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
Scroll all the way down to the bottom right before in summary.

Edit 2:

--main.lua local composer = require("composer") composer.gotoScene("mainMenu") --mainMenu.lua local background = require("backgroundMaker") background:setFillColor(1,0,0) --backgroundMaker.lua local background = display.newRect(50,50, 50,50) return background

Edit 1:

--Heres some code --main.lua local composer = require("composer") local pluginStorage = require("pluginStorage") local background = display.newRect(50,50, 50,50) pluginStorage.background = background composer.gotoScene("mainMenu") --mainMenu.lua local pluginStorage = require("pluginStorage") local background = pluginStorage.background background:setFillColor(1,0,0) --pluginStorage.lua local M = {} return M

Hi Scott,

I still don’t really get it. This the code that I have in the file from where I want to call my functions and from where I’m getting the images:

[lua] local M = {} function M.loadBackground() if settings.backgroundSelected == 1 then local background1 = display.newRect(centerX, centerY, actualW, actualH) background1:setFillColor(41/255, 128/255, 185/255) background1 = M.background end if settings.backgroundSelected == 2 then local background2Gradient = { type = "gradient", color1 = {26/255, 88/255, 127/255}, color2 = {49/ 255,210/255 ,133/255} } local background2 = display.newRect(centerX, centerY, actualW, actualH) background2.fill = background2Gradient background2 = M.background end if settings.backgroundSelected == 3 then local background3Gradient = { type = "gradient", color1 = {213/255, 51/255, 105/255}, color2 = {203/255, 173/255, 109/255} } local background3 = display.newRect(centerX, centerY, actualW, actualH) background3.fill = background3Gradient background3 = M.background end end return M [lua]

Now I’m able to use this functions in my level.lua files. But the background appears in front of my game objects! That’s because the background is never added to a group, or at least that is what I think is the reason.

But I’m unable to add my background variable to my sceneGroup in my levels.lua files.

This is what I tried:

[lua] local cityBackgroundPowerupSelection = require("cityBackgroundPowerupSelection") cityBackgroundPowerupSelection.loadBackground() cityBackgroundPowerupSelection.loadCity() background = cityBackgroundPowerupSelection.background sceneGroup:insert(background) [lua]

This is not working :confused:

This is the error: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’

How could I fix this? :slight_smile:

Kind regards 

Bram

Try flipping background3 = M.background
To
M.background = background3

I feel stupid now.

That fixed it! Thanks for the incredibly fast replies!

I use lua to store all my info.
https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
Scroll all the way down to the bottom right before in summary.

Edit 2:

--main.lua local composer = require("composer") composer.gotoScene("mainMenu") --mainMenu.lua local background = require("backgroundMaker") background:setFillColor(1,0,0) --backgroundMaker.lua local background = display.newRect(50,50, 50,50) return background

Edit 1:

--Heres some code --main.lua local composer = require("composer") local pluginStorage = require("pluginStorage") local background = display.newRect(50,50, 50,50) pluginStorage.background = background composer.gotoScene("mainMenu") --mainMenu.lua local pluginStorage = require("pluginStorage") local background = pluginStorage.background background:setFillColor(1,0,0) --pluginStorage.lua local M = {} return M

Hi Scott,

I still don’t really get it. This the code that I have in the file from where I want to call my functions and from where I’m getting the images:

[lua] local M = {} function M.loadBackground() if settings.backgroundSelected == 1 then local background1 = display.newRect(centerX, centerY, actualW, actualH) background1:setFillColor(41/255, 128/255, 185/255) background1 = M.background end if settings.backgroundSelected == 2 then local background2Gradient = { type = "gradient", color1 = {26/255, 88/255, 127/255}, color2 = {49/ 255,210/255 ,133/255} } local background2 = display.newRect(centerX, centerY, actualW, actualH) background2.fill = background2Gradient background2 = M.background end if settings.backgroundSelected == 3 then local background3Gradient = { type = "gradient", color1 = {213/255, 51/255, 105/255}, color2 = {203/255, 173/255, 109/255} } local background3 = display.newRect(centerX, centerY, actualW, actualH) background3.fill = background3Gradient background3 = M.background end end return M [lua]

Now I’m able to use this functions in my level.lua files. But the background appears in front of my game objects! That’s because the background is never added to a group, or at least that is what I think is the reason.

But I’m unable to add my background variable to my sceneGroup in my levels.lua files.

This is what I tried:

[lua] local cityBackgroundPowerupSelection = require("cityBackgroundPowerupSelection") cityBackgroundPowerupSelection.loadBackground() cityBackgroundPowerupSelection.loadCity() background = cityBackgroundPowerupSelection.background sceneGroup:insert(background) [lua]

This is not working :confused:

This is the error: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’

How could I fix this? :slight_smile:

Kind regards 

Bram

Try flipping background3 = M.background
To
M.background = background3

I feel stupid now.

That fixed it! Thanks for the incredibly fast replies!