How to get the get the value of a variable within a table

I’m making a table to show a towers upgrades in a tower defense game, and there is a singular “Upgrades” table with the values inside of it being the tower names and their upgrades/levels. The table is shown below:

module.Upgrades = {
	Swordsman = { -- tower
		{4, 0.7, 6, 100}, -- level 1
		{7, 0.65, 6.5, 250},
		{12, 0.6, 6.5, 400},
		{25, 0.5, 6.5, 1000}, -- level 4

	  }
        }

How do I get the variable in the table for the tower by using the tower’s name?

Ive tried using table.find(), however it returns an error because the variables arent strings.

upgradeModule.Upgrades[table.find(upgradeModule.Upgrades, tower.Name, 1)][tower.Level.Value][1]

What I want to achieve without directly addressing the tower within the table:

upgradeModule.Upgrades.Swordsman[tower.Level.Value][1]

Any help is appreciated. Many Thanks.

Upgrades.Swordsman[1] -- 1 for level 1, 2 for level 2, etc...

This should give you the level 1 values, for example:

print(Upgrades.Swordsman[1])

Will print:

{
[1] = 4,
[2] = 0.7,
[3] = 6,
[4] = 100
}  -  Server - Script:11

Hope this is what you needed and that this helps!

…unless you meant that you want to use the name of an object, then you can do something like this:

local TowerName = Swordsman.Name -- the object's name should be the name of the tower OR you can store the name somewhere else

local TowerUpgrades = Upgrades[TowerName]

local Level1Upgrade = TowerUpgrades[1]
local Level2Upgrade = TowerUpgrades[2]
-- etc...

of course, you can use any names you want and edit this however you want but these are just the basics, Hope this helps!
And if you have any more questions feel free to ask and I’ll try my best to respond!

Tables are, by default, indexed using numbers. As such, the code you mentioned is essentially this:

module.Upgrades = {
	[1] = {
		[1] = {4, 0.7, 6, 100},
		[2] = {7, 0.65, 6.5, 250},
		[3] = {12, 0.6, 6.5, 400},
		[4] = {25, 0.5, 6.5, 1000},
    }
}

We can, in fact, use any type that is hashable as keys for this dictionary. In our case, we want to address the values in the table with strings, which is possible:

module.Upgrades = {
	["Swordsman"] = {
		{4, 0.7, 6, 100},
		{7, 0.65, 6.5, 250},
		{12, 0.6, 6.5, 400},
		{25, 0.5, 6.5, 1000},
    }
}

I overcomplicated a little, but it’s basically the formal definition.

Edit: Code above is the definition of the table. In order to index the table, we must use the brackets syntax:

print(module.Upgrades["Swordsman"])
-- {
--		{4, 0.7, 6, 100},
--		{7, 0.65, 6.5, 250},
--		{12, 0.6, 6.5, 400},
-- 		{25, 0.5, 6.5, 1000},
-- }

-- assignment is done the same way
module.Upgrades["Swordsman"] = "R0BL0XH4CK3R"
1 Like

I changed the table to this:

["Swordsman"] = {
		{4, 0.7, 6, 100},
		{7, 0.65, 6.5, 250},
		{12, 0.6, 6.5, 400},
		{25, 0.5, 6.5, 1000},

	}

When I try to print this:

print(table.find(upgradeModule.Upgrades, tower.Name, 1)) -- tower.Name = "Swordsman"

It just prints “nil”, so the table.find function cant find anything going by that name in the table for some reason.

EDIT:
Instead of using that I just used upgradeModule.Upgrades[Tower.Name] and it worked fine. Thanks a ton for the help!

2 Likes

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