External files

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
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

External files

Post by xpali2 »

Hi all,

I am new to coding with an engine and I'm currently trying to create a simple game with multiple files. I realise that love.filesystem and require statements should be used to do this, but I cannot for the life of me figure it out. Can somebody end my confusion? How do I link different files together, for example one script that creates a world and one that creates a player and lets him walk around? What are some of the things I can do using love.filesystem?

Thanks,

Xpali2
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: External files

Post by ivan »

xpali2 wrote: Thu Apr 05, 2018 4:53 pmI realise that love.filesystem and require statements should be used to do this,
require is used to load Lua scripts.
love.filesystem works with any kind of file, plain text or whatever.
xpali2 wrote: Thu Apr 05, 2018 4:53 pmHow do I link different files together, for example one script that creates a world and one that creates a player and lets him walk around?
There is no "linking" as in the way compilers link files.
Everything in Lua is done dynamically, so scripts are loaded and executed as soon as you call "require(somefile)".
But yea, tutorials are a good way to start.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: External files

Post by zorg »

require needs a .lua file, and it loads and runs the contents of the file you give it, and either returns true if it was successful, or whatever it found after a return statement in a file. If you create local variables, require won't "comb" those together from multiple files, but you can always return those directly if you want. If you just define variables without the local keyword, those will be visible outside that file, but that is kinda considered bad practice due to loss of locality, and needing to keep in mind what was defined where and not passed explicitly.

kikito had a good writeup on module authoring that somewhat teaches you how require works: http://kiki.to/blog/2014/03/30/a-guide- ... a-modules/

Also, you can use love.filesystem.load if you don't want require to put stuff into the table called package.loaded
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

Re: External files

Post by xpali2 »

Thanks for the replies,

Wait so require is actually messy? Is there a better way to do this?
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: External files

Post by zorg »

depends on what you mean by "messy". i prefer love.filesystem.load myself.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

Re: External files

Post by xpali2 »

Well I would define messy as unoptimised and inefficent. Anyways, I guess I will have to look at both require and love.filesystem.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: External files

Post by zorg »

...But no one said they were unoptimized and inefficient?

I only said that it does some stuff behind the scenes, i.e. does package.loaded[<modulename>] = <the return value of the require call> but that's it.

lua is an interpreted language, can't skirt too much around that; the fact that Löve uses luaJIT to make things way faster doesn't modify the inner workings of the language too much; you still need to parse, and then execute any chunk you load into your program (which require does, and love.filesystem.load does partially, you'll need to run the chunk yourself just like a function with ()-s)... well, you don't HAVE to, but you usually do, if you want to access variables and functions in that scope.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

Re: External files

Post by xpali2 »

I see. Well I think I know what to do now.
Post Reply

Who is online

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