How do you check for a mod being enabled in LUA?

Place to get help with not working mods / modding interface.
Post Reply
Vas
Long Handed Inserter
Long Handed Inserter
Posts: 91
Joined: Tue Apr 12, 2016 11:02 pm
Contact:

How do you check for a mod being enabled in LUA?

Post by Vas »

I've been trying to check for a mod, rather than an item in a mod, but unsuccessfully so far. Something posted somewhere lead me to believe this would be the right way, but it wasn't. Currently, I get "else" every time.

Code: Select all

	if game.active_mod["timed_technology"] then
		table.insert(kit,{name="lab",count=1})
	else
		table.insert(kit,{name="lab",count=1})
		table.insert(kit,{name="science-pack-1",count=50})
		table.insert(kit,{name="science-pack-2",count=50})
		table.insert(kit,{name="science-pack-3",count=50})
	end
You can get my mods by clicking here, and use discussions there or PMs here to suggest or report issues.
Want some blueprints made by me? Click here then!

Bilka
Factorio Staff
Factorio Staff
Posts: 3130
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: How do you check for a mod being enabled in LUA?

Post by Bilka »

I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Vas
Long Handed Inserter
Long Handed Inserter
Posts: 91
Joined: Tue Apr 12, 2016 11:02 pm
Contact:

Re: How do you check for a mod being enabled in LUA?

Post by Vas »

Bilka. I'm sorry, but this doesn't help, as I've seen these pages before.

I've found the second link you gave when looking for a way to test for mods, thats how I learnt about active_mods in the first place... The page is very vague on how active_mods works, and doesn't specify anything about how it can be used other than to just generate a list of mods. I don't need a list, I need to know how to check if a mod is enabled.

The first link, I already know about data lifecycle, which has no bearing on this at all.

Code: Select all

"dependencies": ["base >= 0.16.0", "? timed_technology"],

Code: Select all

elseif ADD_EASY == "Research Equipment" then
	if game.active_mods["timed_technology"] then
		table.insert(kit,{name="lab",count=1})
	else
		table.insert(kit,{name="lab",count=1})
		table.insert(kit,{name="science-pack-1",count=50})
		table.insert(kit,{name="science-pack-2",count=50})
		table.insert(kit,{name="science-pack-3",count=50})
	end
The goal here, is if timed_technology mod is enabled, you get one lab, but if its not, you get 1 lab and 50 of the first three science packs.
You can get my mods by clicking here, and use discussions there or PMs here to suggest or report issues.
Want some blueprints made by me? Click here then!

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

Re: How do you check for a mod being enabled in LUA?

Post by bobingabout »

What he's saying is. he's following the guide (if game.active_mods["timed_technology"] then looks correct to me from the guide, assuming "timed_technology" is actually the internal name of the mod he's looking for) but it's always failing the check even when the mentioned mod is installed and active, and he's asking if anyone knows why that might be happening.

Personally, it looks correct to me, so, I don't know what to suggest. (PS, I've not done a lot with the scripting side of things)
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

socket
Burner Inserter
Burner Inserter
Posts: 9
Joined: Sat Nov 19, 2016 8:39 pm
Contact:

Re: How do you check for a mod being enabled in LUA?

Post by socket »

Vas wrote: I've found the second link you gave when looking for a way to test for mods, thats how I learnt about active_mods in the first place... The page is very vague on how active_mods works, and doesn't specify anything about how it can be used other than to just generate a list of mods.
Quoting the page:
active_mods :: dictionary string → string [Read-only]

The active mods versions. The keys are mod names, the values are the versions.
That doesn't seem vague at all. In fact, it gives you all the information you need. It does assume that you know a bit about how Lua works. Let me try to explain.

"active_mods :: dictionary" indicates that "game.active_mods" is a dictionary. A dictionary (more often called a table in Lua world) is a data structure containing key-value pairs. It allows looking up a value by its key using square brackets (`table[key]`) and iterating over all pairs using `pairs` function (`for key, value in pairs(table) do ... end`).

"string → string " after "dictionary" means that both the keys and the values of the dictionary are strings.

The line "The keys are mod names, the values are the versions." specifies what the key-value pairs of this dictionary mean: in each key-value pair, key is a mod name and value is its version.

So yes, `if game.active_mods["timed_technology"] then ...` is the correct way to check if a mod named "timed_technology" is active. If it doesn't work, printing all keys in the dictionary should help figuring it out:

Code: Select all

for name in pairs(game.active_mods) do
   game.print(name)
end
My guess is that you have the 0.15 version of Timed Technology mod, which has a different name ("timed_technology_015").

Vas
Long Handed Inserter
Long Handed Inserter
Posts: 91
Joined: Tue Apr 12, 2016 11:02 pm
Contact:

Re: How do you check for a mod being enabled in LUA?

Post by Vas »

socket wrote: That doesn't seem vague at all. In fact, it gives you all the information you need. It does assume that you know a bit about how Lua works.

So yes, `if game.active_mods["timed_technology"] then ...` is the correct way to check if a mod named "timed_technology" is active. If it doesn't work, printing all keys in the dictionary should help figuring it out:

My guess is that you have the 0.15 version of Timed Technology mod, which has a different name ("timed_technology_015").
You're right, I'm barely a beginner in LUA, I know how to make an if else statement and copy/paste other people's code with slight alterations.

I could supply my entire LUA file here if it helps, but I did your code too, just to prove that I did the right name which everyone seems to be thinking I did wrong for some reason.

Image

So, I got the name right, and apparently my LUA is correct too, I have no idea why its just not working.
Attachments
control.lua
This is the file, with the debug bit in it to print out a list of mods too.
(11.35 KiB) Downloaded 98 times
You can get my mods by clicking here, and use discussions there or PMs here to suggest or report issues.
Want some blueprints made by me? Click here then!

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: How do you check for a mod being enabled in LUA?

Post by eradicator »

I looked at that control.lua and am puzzled how you could possibly get to the "else" part. You should be getting a "tried to index a nil value" error because you're trying to access "game" outside of an event where it shouldn't even exist.

Post Reply

Return to “Modding help”