Page 1 of 1

Creating a 'Config' Mod

Posted: Tue Sep 27, 2016 9:42 pm
by TheSAguy
Hi,

I'm trying to create a standalone Config mod for my 3 Natural Evolution Mods. (Similar to what Bob has).

I got the everything working in data.lua and data-updates.lua, but my Control.lua does not seem to read the values from the Config mod.

In my Config Mod, I have this in the control.lua the following:

Code: Select all

Natural_Evolution.config.Single_Player_Only = true
In my Expansion mod, I have added the Config mod to the .json

Code: Select all

	"dependencies": ["base >= 0.14", "Natural_Evolution_Config", "? EvoGUI"],
Then in the control, I have the following:

Code: Select all

if not Natural_Evolution then Natural_Evolution = {} end
if not Natural_Evolution.config then Natural_Evolution.config = {} end
if not Natural_Evolution.config.expansion then Natural_Evolution.config.expansion = {} end


if  Natural_Evolution.config.Single_Player_Only then

	require ("libs/EvoGUI")
end
The above Single_Player_Only does not register as 'True'.
How does the Control.lua differ from the data.lua in terms of reading a value in anther mod?

Thanks.

Re: Creating a 'Config' Mod

Posted: Tue Sep 27, 2016 9:49 pm
by aubergine18
All data.lua, data-updates.lua and data-final-fixes.lua are all run in the same Lua environment (as if they were all part of the same mod). http://lua-api.factorio.com/latest/Data-Lifecycle.html

But each control.lua is loaded in separate Lua environment, so they can't see each other. Also, it seems that currently it's not possible to include scripts from other mods. viewtopic.php?f=28&t=32989

The only way to communicate between mods during control.lua is via remote interface. http://lua-api.factorio.com/latest/LuaRemote.html

Re: Creating a 'Config' Mod

Posted: Tue Sep 27, 2016 10:09 pm
by TheSAguy
Thanks aubergine18,
Does anyone have any examples I could look at?
Thanks.

Re: Creating a 'Config' Mod

Posted: Wed Sep 28, 2016 12:27 am
by Arch666Angel
TheSAguy wrote:Thanks aubergine18,
Does anyone have any examples I could look at?
Thanks.
Look into RSO.

I have trigger mods for my series of mods, maybe you want to look into those, they are more MP friendly but only feasible for a limited number of options

Re: Creating a 'Config' Mod

Posted: Wed Sep 28, 2016 4:56 pm
by TheSAguy
Arch666Angel wrote:
TheSAguy wrote:Thanks aubergine18,
Does anyone have any examples I could look at?
Thanks.
Look into RSO.

I have trigger mods for my series of mods, maybe you want to look into those, they are more MP friendly but only feasible for a limited number of options
Arch666Angel,
I looked at your mods and RSO and I could actually not identify how / where you cross referenced other mods...
It does not seem you use the "remote" function described by aubergine18.

Could you possibly help out with my example on the OP?

If this will make the mod unstable in MP I'd rather just leave the mod as is though.

Thanks.

Re: Creating a 'Config' Mod

Posted: Wed Sep 28, 2016 5:10 pm
by aubergine18
It might be better waiting for Factorio 0.15 which hopefully adds new settings system for mods: viewtopic.php?f=34&t=32890

Re: Creating a 'Config' Mod

Posted: Wed Sep 28, 2016 9:00 pm
by Nexela
In your config mod set up a remote interface (mostly pseudo code)

CONFIG
control.lua

Code: Select all

require("config") -- config file
interface = {}
function interface.config_A()
  blah blah bla
  return result
end 
function interface.config_B()
  meh meh meh
  return result
end
remote.add("CONFIG", interface)
MAIN MOD
optional dependency on config
control.lua

Code: Select all

script.on_init( function ()
  if remote.interfaces["CONFIG"] then
    myvalueA = remote.call("CONFIG", "config_A")
    myvalueB = remote.call("CONFIG", "config_B")
  else
    myvalueA = defaultvalue
    myvalueB = defaultvalue
  end
end)
[Edit] remote.call needs strings

Re: Creating a 'Config' Mod

Posted: Mon Oct 03, 2016 6:53 pm
by TheSAguy
I've finally had some time to give this a try. Think I have a synex error, but can't figure it out.
This is what I have:

Config MOD control.lua:

Code: Select all


require("config") 

interface = {}

function interface.config_A()
	local SP_Value
	SP_Value = Natural_Evolution.config.Single_Player_Only
	return SP_Value
end 

remote.add_interface("NE_CONFIG", interface)

In my Expansion mod, control I have:

Code: Select all


function On_Init()

  if remote.interfaces["NE_CONFIG"] then
	SP_Value = remote.call("NE_CONFIG", config_A)
  else
    SP_Value = false
  end

end
----------------------
script.on_init(On_Init)
----------------------
Error I'm getting:
Image

Natural_Evolution.config.Single_Player_Only in the config is a 'True' or 'False'

Re: Creating a 'Config' Mod

Posted: Mon Oct 03, 2016 7:39 pm
by Nexela
Needs to be passed a string for the function name, updated my post to reflect

SP_Value = remote.call("NE_CONFIG", "config_A")


Note:
Tables can also be returned so you could return all configs in one table with one call. however multiple returns are not supported so this below example would only give you the first return (pseudo example)
interface.test=return ok, no, no
a, b, c = remote.call("NE_CONFIG", "test") -- Only gives you "a"

Re: Creating a 'Config' Mod

Posted: Mon Oct 03, 2016 7:58 pm
by Nexela
To have your config vars always present you can also call remote.call outside of an event.
config.mod

Code: Select all

NE_CONFIG.Single_Player_only=true
NE_CONFIG.Some_config = Some_vale

interface = {}

function interface.configs()
   return NE_CONFIG
end

remote.add_interface("NE_CONFIG", interface)


expansion.mod (with (optional) dependency for config.mod)

Code: Select all

if remote.interfaces["NE_CONFIG"] then
   NE_CONFIG = remote.call("NE_CONFIG", "configs")
else
  NE_CONFIG.Single_Player_Only = true
end


function On_Init()
if NE_CONFIG.Single_Player_Only then  blah blah end
if NE_CONFIG.Some_Value then blah blah end
end
----------------------
script.on_init(On_Init)
----------------------

Re: Creating a 'Config' Mod

Posted: Mon Oct 03, 2016 9:18 pm
by TheSAguy
Thanks Nexela,
I'll give it a go tomorrow!