Does lua have something similar to python's import x as y?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Dola
Prole
Posts: 14
Joined: Sat Aug 11, 2012 2:45 am
Location: Brasil

Does lua have something similar to python's import x as y?

Post by Dola »

I have been separating my game code into several files, one for each part of it (mainMenu.lua,HUD.lua,arcadeMode.lua) and importing them with require "file" but doing things this way , as the code grows things start to get confusing because of the huge amount of functions i have created.

In python i could do something like

Code: Select all

import mainMenu.py as M
inside this file i would have a function named "draw" and to draw the menu i could write

Code: Select all

M.draw()
I know i could make each file a table with a lot of functions inside and require it normally but is there a more elegant way of doing this?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Does lua have something similar to python's import x as

Post by ivan »

Dola wrote:I know i could make each file a table with a lot of functions inside and require it normally but is there a more elegant way of doing this?
Yep, you can use the "module" keyword:

Code: Select all

local base = _G
module("api")

function foo()
  math.random(1, 100)
  -- error, "math" is undefined
end
function bar()
  base.math.random(1, 100)
  -- the proper way to access "math"
end
There, both foo and bar are now accessed via "api.foo()" or "api.bar()".
However, using the module keyword makes globals inaccessible so you it's good to have a "base" reference.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Does lua have something similar to python's import x as

Post by Nixola »

Isn't the module function deprecated?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Does lua have something similar to python's import x as

Post by Robin »

Nixola wrote:Isn't the module function deprecated?
It is.

Please, do not use module().

Here's what you can do:
In somemodule.lua:

Code: Select all

local m = {}

function m.draw()
   ....
end

...

return m
In main.lua:

Code: Select all

local foo = require 'somemodule'

foo.draw()
Help us help you: attach a .love.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Does lua have something similar to python's import x as

Post by T-Bone »

Yes, require is the way to go. The problem there is that it assumes that the module you are importing is well written.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Does lua have something similar to python's import x as

Post by Robin »

T-Bone wrote:The problem there is that it assumes that the module you are importing is well written.
That is unfortunate, yes. However, if you write the module yourself, you can make it well written. If it's a 3rd party library, you should probably use one that's well written anyway.
Help us help you: attach a .love.
User avatar
Dola
Prole
Posts: 14
Joined: Sat Aug 11, 2012 2:45 am
Location: Brasil

Re: Does lua have something similar to python's import x as

Post by Dola »

All right, thanks guys!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests