Dropdown for int-setting with text description?

Place to get help with not working mods / modding interface.
Post Reply
Merssedes
Fast Inserter
Fast Inserter
Posts: 144
Joined: Sun Oct 29, 2017 7:05 pm
Contact:

Dropdown for int-setting with text description?

Post by Merssedes »

Given the following:

Code: Select all

-- settings.lua
data:extend ({{
	name = "setting-name",
	setting_type = "startup",
	type = "string-setting",
	allowed_values = { "x", "y", "z" },
	default_value = "z"
	}})

-- in console:
/c game.player.print (settings.startup ["setting-name"])
Unknown key: "z"
means that I can't compare `settings.startup ["setting-name"]` by value with defined values to extract it's index.

So my question is: how to make dropdown setting with text representation, but integer values?

Merssedes
Fast Inserter
Fast Inserter
Posts: 144
Joined: Sun Oct 29, 2017 7:05 pm
Contact:

Re: Dropdown for int-setting with text description?

Post by Merssedes »

As fix for comparision:

Code: Select all

-- in console
/c game.player.print (settings.startup ["setting-name"].value)
z
Therefore my question transforms into: is there a more efficient way than `if ... elseif ... elseif ... end` to extract the index of value?

robot256
Filter Inserter
Filter Inserter
Posts: 701
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: Dropdown for int-setting with text description?

Post by robot256 »

Make a table with strings as keys and numbers as values, then index it with the string value of the setting to get the corresponding number.

Mapping = {one=1,["number two"]=2}

Mapping["one"] == 1

Post Reply

Return to “Modding help”