Format for prototype files within subfolders
-
- Burner Inserter
- Posts: 8
- Joined: Thu Jun 07, 2018 6:52 am
- Contact:
Format for prototype files within subfolders
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
Re: Format for prototype files within subfolders
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.
-
- Burner Inserter
- Posts: 8
- Joined: Thu Jun 07, 2018 6:52 am
- Contact:
Re: Format for prototype files within subfolders
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
Re: Format for prototype files within subfolders
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
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
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Format for prototype files within subfolders
Also thanks to syntactic sugar and require being a function you can use:
without the brackets. Also slash and full stop are identical in meaning. Just use whichever you think looks nicer.
Code: Select all
require "youdirectory/yourfile"
require "yourdirectory.yourfile"
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Format for prototype files within subfolders
This is not quite true, and can bite you.eradicator wrote:Also thanks to syntactic sugar and require being a function you can use:without the brackets. Also slash and full stop are identical in meaning. Just use whichever you think looks nicer.Code: Select all
require "youdirectory/yourfile" require "yourdirectory.yourfile"
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.
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Format for prototype files within subfolders
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.Nexela wrote: I suggest using the same "a/b" format that Factorio uses.
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Format for prototype files within subfolders
Requiring some funktion in data.lua, data-updates.lua and data-final-fixes.lua.
Re: Format for prototype files within subfolders
--------------------------------------------------------eradicator wrote: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.Nexela wrote: I suggest using the same "a/b" format that Factorio uses.
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
Code: Select all
local SomeData = require("lib.file")
someData.A = true
Code: Select all
local SomeData = require("lib/file")
print SomeData.A -- prints nil
Code: Select all
local SomeData = require("lib.file")
print SomeData.a -- prints true
- eradicator
- Smart Inserter
- Posts: 5206
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Format for prototype files within subfolders
@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.
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.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: Format for prototype files within subfolders
That is example enough because this has bit meeradicator 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.
re: typos and short examples
I am on a laptop typing sideways laying down, or on a phone using itty bitty keypad.