How to get value by position table?

How would I get a value in my table by it’s position like this, but it doesn’t work:

upgradeModule.Upgrades.NpcUpgrade.Npcs[1].Name

Might be a good idea to elaborate here, maybe show the table?

local upgrades = {}

upgrades.Upgrades = {
	["MoneyMultiplierUpgrade"] = {["Price"] = 0.5,["Exponent"] = 1.3},
	["NpcUpgrade"] = {
		["Npcs"] = {
			["Rig"] = {["Name"] = "Rig",["Price"] = 15,["Multiplier"] = 3},
			["Bacon"] = {["Name"] = "Bacon",["Price"] = 215,["Multiplier"] = 15},
		},
	}
}

return upgrades

upgradeModule.Upgrades.NpcUpgrade.Npcs[1].Name

You have named indices so referencing them by an integer value won’t work. And you are missing a level in the call. Npcs contains two dictionaries with string indices (Rig and Bacon). There is no index with the value of 1 to reference.

But I’m trying to refer to the position 1, the rig. I could not find how to pick a value from a table by it’s position.

Dictionaries are unordered so referencing by position will not always return the same data. If you must use this system an easier way would be to index them using integers, i.e. setup a bunch of constants to secure the index,i.e.

local RIG = 1;
local BACON = 2;

then use those integers with natural arrays.

...
		["Npcs"] = {
			[RIG] = {["Name"] = "Rig",["Price"] = 15,["Multiplier"] = 3},
			[BACON] = {["Name"] = "Bacon",["Price"] = 215,["Multiplier"] = 15},
...

The code is pseudo and untested but I think you will get the idea.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.