Format for prototype files within subfolders

Place to get help with not working mods / modding interface.
Joeykyle88
Burner Inserter
Burner Inserter
Posts: 8
Joined: Thu Jun 07, 2018 6:52 am
Contact:

Format for prototype files within subfolders

Post 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?
I'm a modder, and I make mods, they usually don't work for a while but sometimes they do work, occasionally they work well enough to put on the Factorio mods website

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2904
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Format for prototype files within subfolders

Post by darkfrei »

Code: Select all

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

Code: Select all

require("prototypes.entities.all-my-belts.green-transport-belt")
Last edited by darkfrei on Tue Jul 31, 2018 7:14 am, edited 1 time in total.

Joeykyle88
Burner Inserter
Burner Inserter
Posts: 8
Joined: Thu Jun 07, 2018 6:52 am
Contact:

Re: Format for prototype files within subfolders

Post by Joeykyle88 »

Thanks
I'm a modder, and I make mods, they usually don't work for a while but sometimes they do work, occasionally they work well enough to put on the Factorio mods website

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Format for prototype files within subfolders

Post 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

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Format for prototype files within subfolders

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Format for prototype files within subfolders

Post 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.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Format for prototype files within subfolders

Post 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).
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2904
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Format for prototype files within subfolders

Post by darkfrei »

Requiring some funktion in data.lua, data-updates.lua and data-final-fixes.lua.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Format for prototype files within subfolders

Post 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

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Format for prototype files within subfolders

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Format for prototype files within subfolders

Post 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.

Post Reply

Return to “Modding help”