Page 1 of 1

Code not working

Posted: Thu Jun 02, 2016 1:02 pm
by TheSAguy
I have some code that is just not working for me and I can't figure out why.
It's code to detect if another mod is also active. I have two mods, Natural_Evolution_Enemies_5.3.2 & Natural_Evolution_Buildings_5.3.2. I want to update a recipe in Enemies, if both mods are running.
I'm using some of bob's functions to do the recipe updates and that part is working fine, it's just the mod detection part that's failing me.

So currently I have my check code look like this:

Code: Select all

--Detect N.E. Enemies
if data.raw["unit"]["small-biter-Mk2"] ~= nil and data.raw["unit"]["small-spitter-Mk2"] ~= nil then
	NEConfig.mod.NEEnemies=true
else 
	NEConfig.mod.NEEnemies=false
end
Then in my data-updates.lua, I want to run the following if NEEnemies is true:

Code: Select all

if NEConfig.mod.NEEnemies then

	---- Add Alien Toxin as a result if NE Enemies
	bobmods.lib.add_recipe_result ("NE_enhanced-alien-nutrientant", {type="fluid", name="NE_alien_toxin", amount=5})
	
	-- Add Alien Toxin as a ingriedient for Bio Ammo 

	bobmods.lib.remove_recipe_item ("Biological-bullet-magazine", "alien-artifact")
	bobmods.lib.remove_recipe_item ("Biological-bullet-magazine", "sulfuric-acid")
	bobmods.lib.add_new_recipe_item ("Biological-bullet-magazine", {type="fluid", name="NE_alien_toxin", amount=10})
	
end

It does not run... If I comment out the "if.. them" part, it runs. I've even tried replacing the above "if..then", check with:

Code: Select all

if data.raw["item"]["Biological-bullet-magazine"] ~= nil then
No luck.
Why!?!

It's already in the data-updates.lua, so both mods should have loaded the data.lua.
Biological-bullet-magazine is there, since it's updating it, so why is the check failing..

I currently can't add NE Enemies in the info.json as a optional load, since I'm doing something in NE Enemies regarding buildings.

This is very frustrating and help would be appreciated. It's probably something very simple that' just slipping my coding talents.
Thanks.

Re: Code not working

Posted: Thu Jun 02, 2016 1:39 pm
by ArderBlackard
The reason is that currently these two NE mods have conflicting access to the NEConfig.mod variable. Both of them have 'detectmod.lua' scripts and each of them starts with

Code: Select all

NEConfig.mod = {}
thus efficiently clearing all the info that have been written there by another mod. The Natural_Evolution_Buildings is loaded before the Natural_Evolution_Enemies so NEE wipes out the NEConfig.mod.NEEnemies variable set by NEB.
It's OK for initializing the NE mods themselves as they can use the filled NEConfig table during their own initialization, but it is not guaranteed that the data in it will be present while loading another mod.
As a workaround you may use the same approach which is used for NEEnemies detection in the Natural_Evolution_Buildings:

Code: Select all

if data.raw["unit"]["small-biter-Mk2"] ~= nil and data.raw["unit"]["small-spitter-Mk2"] ~= nil then
	NEConfig.mod.NEEnemies=true
else 
	NEConfig.mod.NEEnemies=false
end
UPD: Sorry, I didn't notice that I'm talking to the NE mods author :oops:

I've added some logging to both Buildings and Enemies 'detectmod.lua' scripts and received the next output:

Code: Select all

   0.469 Loading mod Natural_Evolution_Buildings 5.3.2 (data-updates.lua)
   0.470 Script detectmod.lua:5: NEConfig.mod is set to {}
   0.470 Script detectmod.lua:35: Detecting N.E. Enemies...
   0.470 Script detectmod.lua:48: NEConfig.mod.NEEnemies: false
   0.528 Loading mod Natural_Evolution_Enemies 5.3.2 (data-updates.lua)
   0.529 Script detectmod.lua:5: NEConfig.mod is set to {}
   0.529 Script detectmod.lua:47: NEConfig.mod.NEEnemies: nil
   0.592 Loading mod Natural_Evolution_Enemies 5.3.2 (data-final-fixes.lua)
   0.593 Script detectmod.lua:5: NEConfig.mod is set to {}
   0.593 Script detectmod.lua:47: NEConfig.mod.NEEnemies: nil


So the Buildings mod doesn't detect the Enemies mod correctly during the 'data-update' stage even if the latter is present. Hope this information may be of any use.

Re: Code not working

Posted: Thu Jun 02, 2016 2:22 pm
by bobingabout
I skimmed.

noticing the NEConfig.mod = {} issue, you should do what I do.


I have this at the start of data.lua in almost all of my mods.

Code: Select all

if not bobmods then bobmods = {}
if not bobmods.modname then bobmodst.modname = {}
so you should do something like this

Code: Select all

if not NEConfig then NEConfig = {}
if not NEConfig.mod then NEConfig.mod = {}
etc.

Re: Code not working

Posted: Thu Jun 02, 2016 3:24 pm
by steinio
Isn't

Code: Select all

neconfig.mod = neconfig.mod or {} 
the shorthand?

Re: Code not working

Posted: Thu Jun 02, 2016 4:33 pm
by TheSAguy
bobingabout wrote:I skimmed.

noticing the NEConfig.mod = {} issue, you should do what I do.


I have this at the start of data.lua in almost all of my mods.

Code: Select all

if not bobmods then bobmods = {}
if not bobmods.modname then bobmodst.modname = {}
so you should do something like this

Code: Select all

if not NEConfig then NEConfig = {}
if not NEConfig.mod then NEConfig.mod = {}
etc.
Thanks Bob and ArderBlackard.
Question, Is it only necessary to define the below in Data.lua or should I repeat it in data-updates.lua and data-final-fixes.lua if I use the detect mod scripts in those?

Code: Select all

if not NEConfig then NEConfig = {}
if not NEConfig.mod then NEConfig.mod = {}
etc.[/quote]

Re: Code not working

Posted: Thu Jun 02, 2016 5:05 pm
by ArderBlackard
I guess, data.lua should be enough as everything defined in it will be available later on in -updates and -final-fixes