Page 1 of 1
How to separate concerns?
Posted: Sun Sep 17, 2017 11:03 am
by cfazzini
Lately, I've been following this tutorial on Youtube. Everything was created in a single file called main.lua
There were things like player and enemy that seemed like they could have been moved out of main.lua.
I come from Ruby, and I like the fact that I can encapsulate logic into classes. Is there a pattern in love2d/Lua I should be looking at?
Re: How to separate concerns?
Posted: Sun Sep 17, 2017 12:12 pm
by grump
This is a good starting point.
Re: How to separate concerns?
Posted: Sun Sep 17, 2017 3:28 pm
by erasio
Short answer: Yes. You usually do though the tutorial probably didn't for simplicitys sake.
You can load any lua file or lua binding dlls with the "require" keyword.
If you just wanna include some code you can assume it's just appended to your main.lua file. Actual modules are written slightly differently. You can take a look at the libraries on the wiki and check out how they did it.
Re: How to separate concerns?
Posted: Fri Sep 29, 2017 2:28 pm
by hamberge
It's nice to have one class-like table per file and to load that table into another file via require. Each required file can return its created table. One nice thing in lua is that if you require the same file in multiple different files, it returns a reference to the same table.
Re: How to separate concerns?
Posted: Tue Oct 17, 2017 4:01 pm
by TC1061
erasio wrote: ↑Sun Sep 17, 2017 3:28 pm
You can load any lua file or lua binding dlls with the "require" keyword.
Don't lie!!! "require" isn't keyword! It's function!
Re: How to separate concerns?
Posted: Tue Oct 17, 2017 7:08 pm
by zorg
TC1061 wrote: ↑Tue Oct 17, 2017 4:01 pm
erasio wrote: ↑Sun Sep 17, 2017 3:28 pm
You can load any lua file or lua binding dlls with the "require" keyword.
Don't lie!!! "require" isn't keyword! It's function!
It's also a keyword. See the manual:
https://www.lua.org/manual/5.1/manual.html#2.1
The following keywords are reserved and cannot be used as names:
and break do else elseif
end false for function if
in local nil not or
repeat return then true until while
Re: How to separate concerns?
Posted: Wed Oct 18, 2017 12:06 am
by Azhukar
zorg wrote: ↑Tue Oct 17, 2017 7:08 pmIt's also a keyword. See the manual:
https://www.lua.org/manual/5.1/manual.html#2.1
The following keywords are reserved and cannot be used as names:
and break do else elseif
end false for function if
in local nil not or
repeat return then true until while
'require' is not a reserved keyword.
Re: How to separate concerns?
Posted: Wed Oct 18, 2017 2:21 am
by zorg
Azhukar wrote: ↑Wed Oct 18, 2017 12:06 am
'require' is not a reserved keyword.
Whoops, that's what i get for not reading what i copy :p