Page 1 of 1

Help With Lua Require

Posted: Fri Nov 30, 2018 6:33 pm
by salbris
Hey,

I'm trying to figure out how Factorio and Angels Refining uses Lua Require but outside of Factorio I can't seem to get the same behaviour.

I opened a Stackoverflow question: https://stackoverflow.com/questions/535 ... quire-path

The line is question is the first line in this file:
https://github.com/Arch666Angel/ModsCur ... ctions.lua
Which originally included here: https://github.com/Arch666Angel/ModsCur ... ta.lua#L21

I'm trying to figure out how to configure Lua in my external tool to ensure that the require in override-functions.lua uses the correct path (relative to that file). Instead it's searching for paths relative to data.lua and a bunch of other hardcoded paths I have configured.

Any help would be appreciated.

Thanks.

Re: Help With Lua Require

Posted: Fri Nov 30, 2018 7:20 pm
by DaveMcW

Re: Help With Lua Require

Posted: Fri Dec 07, 2018 6:52 pm
by salbris
Thanks Dave but I think the problem is deeper than that. Sounds like I'll need to completely override the require function and make it dynamically find files in the current directory. I haven't found a simpler solution which leads me to believe the Factorio devs must have done something similar to support this.

Is there really no documentation about what supports in the Mod lua for require?

Re: Help With Lua Require

Posted: Fri Dec 07, 2018 7:22 pm
by DaveMcW
Here is what I used for my tech tree.

I run it in the factorio root directory in Lua 5.2.

Code: Select all

package.path = package.path .. ';data/core/lualib/?.lua;data/base/?.lua'
defines = {
  ["difficulty_settings"] = {
    ["recipe_difficulty"] = {["normal"]=1},
    ["technology_difficulty"] = {["normal"]=1},
  },
  ["direction"] = {
    ["north"] = 0,
    ["east"] = 2,
    ["south"] = 4,
    ["west"] = 6,
  },
}
require "dataloader"
require "data.base.data"
for k, r in pairs(data.raw.recipe) do ...

Re: Help With Lua Require

Posted: Mon Dec 10, 2018 9:03 am
by bobingabout
Yeah, the require() function needs to contain the full path from mod root.

I know you can require from the core mod too, such as you used to require("util"), but I don't know how that factors into it.

Re: Help With Lua Require

Posted: Wed Dec 12, 2018 5:47 am
by salbris
Thanks for code snippet Dave. And Thanks Bob :)

Actually if you look in Angels refining they use a relative path in prototypes/override-functions.

Anyways I found a custom require that works. I updated my StackOverflow answer if anyone is curious!