Page 1 of 1

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

Posted: Sun Feb 11, 2018 4:25 pm
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

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

Posted: Sun Feb 11, 2018 4:58 pm
by Bilka

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

Posted: Sun Feb 11, 2018 10:56 pm
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.

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

Posted: Mon Feb 12, 2018 9:27 am
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)

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

Posted: Mon Feb 12, 2018 9:56 am
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").

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

Posted: Mon Feb 12, 2018 8:35 pm
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.

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

Posted: Tue Feb 13, 2018 12:01 am
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.