Page 1 of 1

Format for prototype files within subfolders

Posted: Tue Jul 31, 2018 5:44 am
by Joeykyle88
This may be a stupid question but I'm creating a large mod and would like to be able to split up the items and entities into multiple prototype files "ex. projectiles.lua or gun.lua" is it possible to put those into subfolders, or do I need to put them into the prototypes folder, and if I is possible to put them into sub folders what format do I need to use in data.lua for them to work?

Re: Format for prototype files within subfolders

Posted: Tue Jul 31, 2018 7:07 am
by darkfrei

Code: Select all

require("folder.subfolder.subsubfolder.filename")

Code: Select all

require("prototypes.entities.all-my-belts.green-transport-belt")

Re: Format for prototype files within subfolders

Posted: Tue Jul 31, 2018 7:12 am
by Joeykyle88
Thanks

Re: Format for prototype files within subfolders

Posted: Tue Jul 31, 2018 10:27 pm
by Nexela
The name or location of the file doesn't matter at all (as long as it is somewhere in the mod folder).

I usually use 1 file for each related thing

I.e
prototypes/warhouse.lua would contain the entity, item(s), recipe(s) and insert the tech unlock.

and require("prototypes/warehouse") in data.lua

I specifically put everything in a prototypes/ directory(or subdirectory of) So my lua linter knows these are "data-stage" files

Re: Format for prototype files within subfolders

Posted: Wed Aug 08, 2018 2:38 pm
by eradicator
Also thanks to syntactic sugar and require being a function you can use:

Code: Select all

require "youdirectory/yourfile"
require "yourdirectory.yourfile"
without the brackets. Also slash and full stop are identical in meaning. Just use whichever you think looks nicer.

Re: Format for prototype files within subfolders

Posted: Thu Aug 09, 2018 12:31 am
by Nexela
eradicator wrote:Also thanks to syntactic sugar and require being a function you can use:

Code: Select all

require "youdirectory/yourfile"
require "yourdirectory.yourfile"
without the brackets. Also slash and full stop are identical in meaning. Just use whichever you think looks nicer.
This is not quite true, and can bite you.

The "string" passed to require is stored as the package key,

require("a/b") first looks for package.loaded["a/b"] and returns that result else requires the file
And since require("a/b") and require("a.b") are 2 different strings your file can get required twice and in some instances this not what you want. I suggest using the same "a/b" format that Factorio uses.

Re: Format for prototype files within subfolders

Posted: Thu Aug 09, 2018 9:52 am
by eradicator
Nexela wrote: I suggest using the same "a/b" format that Factorio uses.
As long as it's consistent within a mod i don't see any difference. Also the whole "requiring the same thing again doesn't actually load it a second time" feature of lua is probably something that 99% of modders don't even know.

In the context of factorio modding i'm not sure how useful requiring the same file twice is (or if it should be considered bad practice), or in what instances actually loading a package twice would cause any issues. Got good examples? (For me and the ones to come).

Re: Format for prototype files within subfolders

Posted: Thu Aug 09, 2018 10:29 am
by darkfrei
Requiring some funktion in data.lua, data-updates.lua and data-final-fixes.lua.

Re: Format for prototype files within subfolders

Posted: Thu Aug 09, 2018 11:35 pm
by Nexela
eradicator wrote:
Nexela wrote: I suggest using the same "a/b" format that Factorio uses.
As long as it's consistent within a mod i don't see any difference. Also the whole "requiring the same thing again doesn't actually load it a second time" feature of lua is probably something that 99% of modders don't even know.

In the context of factorio modding i'm not sure how useful requiring the same file twice is (or if it should be considered bad practice), or in what instances actually loading a package twice would cause any issues. Got good examples? (For me and the ones to come).
--------------------------------------------------------
lib file

Code: Select all

local SomeData = {blahblahblah}
log("First time required")
return SomeData
file A

Code: Select all

local SomeData = require("lib.file")
someData.A = true
file B

Code: Select all

local SomeData = require("lib/file")
print SomeData.A -- prints nil
file C

Code: Select all

local SomeData = require("lib.file")
print SomeData.a -- prints true
"First time required" will show up in the log only twice since the script is executed for / and . but not for the final . since it has already been required

Re: Format for prototype files within subfolders

Posted: Fri Aug 10, 2018 9:58 am
by eradicator
@Nexela:
I explicitly wanted good examples of where this would actually cause confusion, not the simplest possible "how does this work" example, but real case scenarios. Trying to read a variable from some module right after it has been required is something only someone who already knows the "second require gets the same thing" quirk would try. Besides you need to fix your file C example. lower-case "a" won't print true (at least not from previous assignments to upper-case "A").

@Darkfrei:
Yea. That's a very good example for requiring the same thing twice. I actually need to adjust my code to compensate for possible intermediate changes from hostile mods.

Re: Format for prototype files within subfolders

Posted: Sat Aug 11, 2018 4:00 am
by Nexela
eradicator wrote:@Nexela:
to read a variable from some module right after it has been required is something only someone who already knows the "second require gets the same thing" quirk would try.
That is example enough because this has bit me :P

re: typos and short examples
I am on a laptop typing sideways laying down, or on a phone using itty bitty keypad.