Page 1 of 1

Dropdown for int-setting with text description?

Posted: Mon Apr 29, 2024 4:22 pm
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?

Re: Dropdown for int-setting with text description?

Posted: Mon Apr 29, 2024 5:03 pm
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?

Re: Dropdown for int-setting with text description?

Posted: Tue Apr 30, 2024 5:28 am
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