Code not working

Place to get help with not working mods / modding interface.
Post Reply
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Code not working

Post 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.
Attachments
Natural_Evolution_Enemies_5.3.2.zip
Natural_Evolution_Enemies_5.3.2
(233.61 KiB) Downloaded 88 times
Natural_Evolution_Buildings_5.3.2.zip
Natural_Evolution_Buildings_5.3.2
(1.39 MiB) Downloaded 80 times

User avatar
ArderBlackard
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Thu May 05, 2016 12:41 pm
Contact:

Re: Code not working

Post 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.
Gib dich hin bis du Glück bist

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Code not working

Post 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.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2636
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: Code not working

Post by steinio »

Isn't

Code: Select all

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

Transport Belt Repair Man

View unread Posts

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Code not working

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

User avatar
ArderBlackard
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Thu May 05, 2016 12:41 pm
Contact:

Re: Code not working

Post by ArderBlackard »

I guess, data.lua should be enough as everything defined in it will be available later on in -updates and -final-fixes
Gib dich hin bis du Glück bist

Post Reply

Return to “Modding help”