How to pull values from a table to use them in a function?

I am trying to give certain enemies statistics, so I can use them later in a table. How would I do this?

local metalbear = {atk = 9999, atkSpeed = 150, def = 9999, movementSpeed = 300, range = 50, longRange = false, isSingleTarget = true}

 So I can use this in a function:

function enemy:new(atkBuff, defBuff, spawnTime, spawnPattern) end --Would I use something like: metalbear.atk or metalbear[1] 

Nevermind, I believe this is the solution:

metalbear.atk

I read it in a lua tutorial.

https://coronalabs.com/learn-lua/

If you’re allocating this to a new object, make sure you add zero to numbers and “” to strings.

[lua]

local atk = metalbear.atk – creates reference to metalbear. Changing atk also changes metalbear.atk.

local atk = metalbear.atk + 0  – creates a new variable atk which can be changed independently.

[/lua]

Thank you. Also, I am going to have many enemies in the table, How can I reference them all with the atk variable, would it be like:

local enemies = { metalbear = {atk = 9999} ursamajor = {atk = 7000} } function enemy:new() enemies[i].atk? end

Many ways to do it, but I’d probably have something like this:

[lua]

local enemies = {

   {name = “metalbear” , atk = 9999},

   {name = “ursamajor”, atk = 7000},

}

function enemy:new()

   local enemyType = math.random(1, #enemies)

   local e = enemies[enemyType]

   print ("This enemy is “…e.name…” with an atk of "…e.atk)

end

[/lua]

If you wanted to find an enemy by its name, you could have a helper function that takes in a name of an enemy, and returns its ID in the enemies table.

[lua]

local getEnemyByName = function (name)

 local id = 1 – default to 1 so you never return a ‘nil’ enemy

  for a = 1, #enemies, 1 do

   if enemies[a].name == name then

     id = a

     break

   end

 end

return id

end

[/lua]

Thanks! This will be extremely helpful for looping through units and making functions for all of them:

for i = 1, #enemies do print ("This enemy is "..enemies[i].name.." with an atk of "..enemies[i].atk) end

I do not think this is correct because primitive values (numbers, strings, booleans, etc.) are copied by value, not by reference. Unless you are referring to a technique I’m not seeing in the posts above? Eg:

local metalbear = {atk = 9999, atkSpeed = 150, def = 9999, movementSpeed = 300, range = 50, longRange = false, isSingleTarget = true} print(metalbear.atk) local atk = metalbear.atk atk = atk + 1 print(metalbear.atk) local atk = metalbear.atk + 0 atk = atk + 1 print(metalbear.atk)

Changing the value of ‘atk’ will not change the value of ‘metalbear.atk’

Am I missing something?

Thanks for your help @horacebury, this will come in handy. I was worried that buffing the enemy meant it would change the attack power permanently.

Nevermind, I believe this is the solution:

metalbear.atk

I read it in a lua tutorial.

https://coronalabs.com/learn-lua/

If you’re allocating this to a new object, make sure you add zero to numbers and “” to strings.

[lua]

local atk = metalbear.atk – creates reference to metalbear. Changing atk also changes metalbear.atk.

local atk = metalbear.atk + 0  – creates a new variable atk which can be changed independently.

[/lua]

Thank you. Also, I am going to have many enemies in the table, How can I reference them all with the atk variable, would it be like:

local enemies = { metalbear = {atk = 9999} ursamajor = {atk = 7000} } function enemy:new() enemies[i].atk? end

Many ways to do it, but I’d probably have something like this:

[lua]

local enemies = {

   {name = “metalbear” , atk = 9999},

   {name = “ursamajor”, atk = 7000},

}

function enemy:new()

   local enemyType = math.random(1, #enemies)

   local e = enemies[enemyType]

   print ("This enemy is “…e.name…” with an atk of "…e.atk)

end

[/lua]

If you wanted to find an enemy by its name, you could have a helper function that takes in a name of an enemy, and returns its ID in the enemies table.

[lua]

local getEnemyByName = function (name)

 local id = 1 – default to 1 so you never return a ‘nil’ enemy

  for a = 1, #enemies, 1 do

   if enemies[a].name == name then

     id = a

     break

   end

 end

return id

end

[/lua]

Thanks! This will be extremely helpful for looping through units and making functions for all of them:

for i = 1, #enemies do print ("This enemy is "..enemies[i].name.." with an atk of "..enemies[i].atk) end

I do not think this is correct because primitive values (numbers, strings, booleans, etc.) are copied by value, not by reference. Unless you are referring to a technique I’m not seeing in the posts above? Eg:

local metalbear = {atk = 9999, atkSpeed = 150, def = 9999, movementSpeed = 300, range = 50, longRange = false, isSingleTarget = true} print(metalbear.atk) local atk = metalbear.atk atk = atk + 1 print(metalbear.atk) local atk = metalbear.atk + 0 atk = atk + 1 print(metalbear.atk)

Changing the value of ‘atk’ will not change the value of ‘metalbear.atk’

Am I missing something?

Thanks for your help @horacebury, this will come in handy. I was worried that buffing the enemy meant it would change the attack power permanently.