Can you make and use an array of variable names?

Post Reply
jwrobbs
Posts: 21
Joined: Sat Sep 23, 2017 1:26 am

Can you make and use an array of variable names?

Post by jwrobbs »

I have a bunch of variables. I'm trying to figure out a simple and clean way to do something simple to them like subtract 1 from them all.

Can I make an array of the variable names and use that in a function?

Is there a better way to manage this? A table?

The end goal is to update the variables tracking duration of spells each tick.

(I know php but I'm brand new to Lua.)

Thanks for the help!

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Can you make and use an array of variable names?

Post by Jor'Mox »

So, in Lua, anything that looks remotely like an array... is a table. Tables are the only variable type that can hold more than a single value in Lua. To track spell durations, I would recommend a keyed table, with the spell names as the keys, and the durations as the values. Here is a bit of example code showing the basics you need to make that happen.
Code: [show] | [select all] lua
-- First, obviously, you need to make a table, I'll declare it here for clarity
-- note that Lua treats any variable value other than FALSE or NIL as TRUE for logic purposes, allowing for some complex variable declarations.
local spell_table = spell_table or {}

-- then you could create a new entry like this
spell_table["armor"] = 10

-- OR

local spell_name = "armor"
local spell_duration = 10
spell_table[spell_name] = spell_duration

-- to run through all your spells, and subtract 1, you would do this
for k, v in pairs(spell_table) do
    spell_table[k] = v - 1
end

jwrobbs
Posts: 21
Joined: Sat Sep 23, 2017 1:26 am

Re: Can you make and use an array of variable names?

Post by jwrobbs »

Thanks, Jor'Mox! That clears up a lot and is exactly what I was looking for.

jwrobbs
Posts: 21
Joined: Sat Sep 23, 2017 1:26 am

Re: Can you make and use an array of variable names?

Post by jwrobbs »

1 more question.

What is the best place to declare a table? Is it okay if I put it in a trigger and it runs multiple times? Or should I put it somewhere else?

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Can you make and use an array of variable names?

Post by Jor'Mox »

jwrobbs wrote:
Mon Oct 02, 2017 5:09 pm
1 more question.

What is the best place to declare a table? Is it okay if I put it in a trigger and it runs multiple times? Or should I put it somewhere else?
So, the answer to that depends. If you want a global level table, then you can declare it inside a trigger without any difficulty, so long as you declare it in a manner similar to how I did above (so that existing values are preserved if there are any, and if the variable doesn't exist yet, an empty table is created). If you want a local level table, then you would need to declare it in a script, because one declared in a trigger would be discarded after the trigger fired, and a totally new one would be created the next time the trigger fired. So long as you only have a few things going on, global level variables are usually just fine, but they can become problematic if you find yourself with a lot of different scripts, looking for room in a now cluttered name-space.

However, the actual act of table declaration is innocuous, so if you are only concerned about performance, it isn't an issue.

Edit: And, for clarity, to make a global variable, all you have to do is NOT put "local" before the declaration.

User avatar
SlySven
Posts: 1023
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Can you make and use an array of variable names?

Post by SlySven »

As Jor'Mox pointed out in Lua the (only) complex data store is a table but those exist in two flavours, more generally they are associative arrays where a key identifies something stored as a value, and in general you can create them:
Code: [show] | [select all] lua
myTable = { "first entry key" = 42, "second entry" = "something", 42 = "forty-two", "forty one" = 43 }

echo("    I think that 42 is smelled: \"" .. myTable[42] .. "\". \n")
    I think that 42 is smelled: "forty-two". 
Pretty much anything can be used as the key for an associative array/table and the same for the value - except that a value of nil removes that entry from the table as a value and is not valid as a key. On the other hand you can also use monotonically incrementing integers starting from 1 {i.e. 1, 2, 3, 4,...n with no spaces} to make an indexed array/table - that happens automagically if you just supply a list of values - and the keys are implied/created for you, and the '#' operator returns the number of entries (already) in the table {it does NOT work on associative arrays/tables}:
Code: [show] | [select all] lua
myTable = { "three", "1 + 1", 3 }

echo("    I think that two is the same as: " .. myTable[2] .. "\". \n")
    I think that two is the same as 1 + 1.
myTable[#myTable  +1] = "four"
echo("    Table has " .. #myTable .. " entries, the last one is: " .. myTable[ #myTable ] .. "\n"
    Table has 4 entries, the last one is: four

Post Reply