Page 2 of 2

Re: How to connect the 2 lua files?

Posted: Mon Jan 14, 2013 7:37 pm
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.

Re: How to connect the 2 lua files?

Posted: Mon Jan 14, 2013 7:46 pm
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.

Re: How to connect the 2 lua files?

Posted: Tue Jan 15, 2013 5:35 am
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?

Re: How to connect the 2 lua files?

Posted: Tue Jan 15, 2013 6:16 am
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

Re: How to connect the 2 lua files?

Posted: Wed Jan 16, 2013 6:46 am
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:

Re: How to connect the 2 lua files?

Posted: Wed Jan 16, 2013 1:54 pm
by bartbes
Interestingly enough, even in the old version he was doing it wrong. He (probably unknowingly) exploited the way require worked.