question about "require"

In my main.lua I have the first line as “require othergame”
where othergame.lua is a standalone game.

When I press the “play game” button all I get is:
x:123 y:123 {or some 3 digit numbers}
nil

Then Corona exits immediately.

It is going to function:
local function showxy(event)
if(event.phase==“began”)then
print("\tx: “…event.x…” y: "…event.y)
end
end

but not to:
local function startothergame(event)
if(event.phase==“began” and playgameup.alpha==1 and othergame.gameActive==false)then
if(bInGame==true)then return end
bInGame = true
othergame.gameActive = true
playgameup.alpha = 0
local function launch()
if(othergame.started==true)then
othergame.bMenu = true
othergame.gameActive = true
else
audio.fade({ channel=1, time=1, volume=100 } )
audio.fade({ channel=2, time=1, volume=0 } )
othergame.runGame()
othergame.gameActive = true
end
end
timer.performWithDelay(300,launch)
end
end – function startothergame(event)

This code worked before with one game. I modified it to use othergame.
Thanks in advance for your comments. [import]uid: 51895 topic_id: 31162 reply_id: 331162[/import]

Hi iconosys_dev,

“require” is used for implementing classes.

Such as:

local widget = require("widgets")  
  
--Or  
  
local storyboard = require("storyboard")  

-Landon [import]uid: 111492 topic_id: 31162 reply_id: 124696[/import]

Here is how Require works:

  1. When you call “require (“x”)” It first looks into a table in your global table called ‘packages.loaded[x]’, if that is not nil, the require call will return whatever is there.
  2. If that is nil, it’ll look for a file called x.lua, if it finds it, it’ll load it. (This gets more technical on device, where all lua files are one single file called ‘resources.car’, but it effectively looks in that folder for the lua object ‘x’.
  3. It then runs the script it finds, as if the whole file were encased in a function
  4. It takes whatever was returned from that file, and puts it in packages.loaded[x]

This means your lua file is only processed once, no matter how many times you call require on it. This means that if you a file that does say:

return math.random(0, 100)  

It will ALWAYS return the same number after the first require (but could be different between App Starts).

You can ‘unrequire’ a file by removing it from packages.loaded, but that can be sort of hacky. Ideally, if you want a chunk of processing to occur multiple times it should be within a function within the file, maybe a .new() function that returns a game object rather than simply require() returning the game object. I’d need to see the file itself to get a better idea of why you would be getting the crash.

It also means that you can do some neat tricks depending on what exactly you return from your file. You can use metatables to make the requires return unique objects, or return partial objects (with private variables as local) to create encapsulation. [import]uid: 134101 topic_id: 31162 reply_id: 124700[/import]

Hi iconosys_dev,

“require” is used for implementing classes.

Such as:

local widget = require("widgets")  
  
--Or  
  
local storyboard = require("storyboard")  

-Landon [import]uid: 111492 topic_id: 31162 reply_id: 124696[/import]

Here is how Require works:

  1. When you call “require (“x”)” It first looks into a table in your global table called ‘packages.loaded[x]’, if that is not nil, the require call will return whatever is there.
  2. If that is nil, it’ll look for a file called x.lua, if it finds it, it’ll load it. (This gets more technical on device, where all lua files are one single file called ‘resources.car’, but it effectively looks in that folder for the lua object ‘x’.
  3. It then runs the script it finds, as if the whole file were encased in a function
  4. It takes whatever was returned from that file, and puts it in packages.loaded[x]

This means your lua file is only processed once, no matter how many times you call require on it. This means that if you a file that does say:

return math.random(0, 100)  

It will ALWAYS return the same number after the first require (but could be different between App Starts).

You can ‘unrequire’ a file by removing it from packages.loaded, but that can be sort of hacky. Ideally, if you want a chunk of processing to occur multiple times it should be within a function within the file, maybe a .new() function that returns a game object rather than simply require() returning the game object. I’d need to see the file itself to get a better idea of why you would be getting the crash.

It also means that you can do some neat tricks depending on what exactly you return from your file. You can use metatables to make the requires return unique objects, or return partial objects (with private variables as local) to create encapsulation. [import]uid: 134101 topic_id: 31162 reply_id: 124700[/import]