How to connect the 2 lua files?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: How to connect the 2 lua files?

Post by master both »

Code: Select all

function love.load(require player.)
I have a question: is it necessary to be in the love.load argument?
i just put it at the start of main.lua before love.load.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: How to connect the 2 lua files?

Post by Boolsheet »

plz_m0ve wrote:

Code: Select all

function love.load(require player.lua)
This is invalid Lua. It expects parameter names in the function definitions, it doesn't evaluate expressions.

require is just a regular function. You can call it whenever and the module gets loaded. There's no rule where you have to put it, it's mainly your design decision. However, you have to be aware that if you put it before love.load, it will get executed first. It will break if something in the other file relies on a global that is set in love.load.

Code: Select all

function love.load()
    require("player")
end
Lua has some syntax sugar that allows you to drop the parentheses if the only argument is a string literal or table constructor.
Lua manual wrote:A call of the form f{fields} is syntactic sugar for f({fields}); that is, the argument list is a single new table. A call of the form f'string' (or f"string" or f[[string]]) is syntactic sugar for f('string'); that is, the argument list is a single literal string.
Shallow indentations.
plz_m0ve
Prole
Posts: 16
Joined: Fri Jan 04, 2013 4:07 pm

Re: How to connect the 2 lua files?

Post by plz_m0ve »

Boolsheet wrote:
plz_m0ve wrote:

Code: Select all

function love.load(require player.lua)
This is invalid Lua. It expects parameter names in the function definitions, it doesn't evaluate expressions.

require is just a regular function. You can call it whenever and the module gets loaded. There's no rule where you have to put it, it's mainly your design decision. However, you have to be aware that if you put it before love.load, it will get executed first. It will break if something in the other file relies on a global that is set in love.load.

Code: Select all

function love.load()
    require("player")
end
Lua has some syntax sugar that allows you to drop the parentheses if the only argument is a string literal or table constructor.
Lua manual wrote:A call of the form f{fields} is syntactic sugar for f({fields}); that is, the argument list is a single new table. A call of the form f'string' (or f"string" or f[[string]]) is syntactic sugar for f('string'); that is, the argument list is a single literal string.
When i do what you do with the love.load

Code: Select all

function love.load(require player.lua) 
then it says

Code: Select all

')' expected near "player.lua"
When i try to add it, it says it again? Someone know why?
LÖVE IS IN THE AIR
- OBEY
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: How to connect the 2 lua files?

Post by scutheotaku »

plz_m0ve wrote:
Boolsheet wrote:
plz_m0ve wrote:

Code: Select all

function love.load(require player.lua)
This is invalid Lua. It expects parameter names in the function definitions, it doesn't evaluate expressions.

require is just a regular function. You can call it whenever and the module gets loaded. There's no rule where you have to put it, it's mainly your design decision. However, you have to be aware that if you put it before love.load, it will get executed first. It will break if something in the other file relies on a global that is set in love.load.

Code: Select all

function love.load()
    require("player")
end
Lua has some syntax sugar that allows you to drop the parentheses if the only argument is a string literal or table constructor.
Lua manual wrote:A call of the form f{fields} is syntactic sugar for f({fields}); that is, the argument list is a single new table. A call of the form f'string' (or f"string" or f[[string]]) is syntactic sugar for f('string'); that is, the argument list is a single literal string.
When i do what you do with the love.load

Code: Select all

function love.load(require player.lua) 
then it says

Code: Select all

')' expected near "player.lua"
When i try to add it, it says it again? Someone know why?
I'm not exactly sure what you're doing wrong, but calling a require function usually looks like this:

Code: Select all

    require "player"
Or, to modify your code where you were putting it inside the love.load callback function:

Code: Select all

function love.load()
     require "player"
end
Also, you don't need to put your "require"'s in love.load; it's standard practice to place them at the beginning of the file (though this isn't a rule), e.g.

Code: Select all

require "player"

function love.load()

end

function love.update()
	
end

function love.draw()

end
EDIT:
Also, while you may not quite understand this yet, it's worth mentioning that including another Lua file in a Lua file with require doesn't "blend" them together. What I mean is that it's not like, with the code above, your code in player.lua will just be placed in main.lua at runtime. Instead, it will work sort of like a function, where it is a separate component from the rest of main.lua and its components. In general, this slight difference won't really matter though. But then why is it important? Because of scope. Since, using the example from before, player.lua is not actually a part of main.lua, any local variables (for example) defined in player.lua will not be able to be accessed by main.lua. Again, this may not make sense to you yet, but as you learn Lua and programming I think you'll understand my poor explanation :D (though I don't mean to be condescending, as you very well may get this).

EDIT AGAIN:
As a side note: since you're new to both LOVE and Lua, it may be helpful for you to go through one of the LOVE video tutorial series that teach you the basics of both.

Here are two good ones:
http://sockmunkee.com/videos/
http://www.youtube.com/playlist?list=PL924F20B05A624D91
nyrebellion
Prole
Posts: 3
Joined: Sun Jan 13, 2013 5:42 pm

Re: How to connect the 2 lua files?

Post by nyrebellion »

When making main.lua recognize player.lua I felt lost for a bit because I was learning from a tutorial.

I think the example I was learning from was based on the old version of LOVE, not sure...I'm completely new to this.

I tried going to main.lua and typing : require "player.lua" because I was copying every single thing he was doing and when he ran the game it worked. I couldn't get mine to work so I came on here and require "player" was all I needed.

I'm glad I wasn't the only person asking this question. I felt completely useless when it didn't work.

Thanks! :awesome:
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How to connect the 2 lua files?

Post by bartbes »

Interestingly enough, even in the old version he was doing it wrong. He (probably unknowingly) exploited the way require worked.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests