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!
Can you make and use an array of variable names?
Re: Can you make and use an array of variable names?
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.
Re: Can you make and use an array of variable names?
Thanks, Jor'Mox! That clears up a lot and is exactly what I was looking for.
Re: Can you make and use an array of variable names?
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?
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?
Re: Can you make and use an array of variable names?
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.
- 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?
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:
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}: