3 LUA questions (array, vars scope, mob health)
Hello,
I have some LUA related questions I hope someone here will be able to answer. 1) What is the best way to search an array? Code:
for _, value in pairs(myArray) do Code:
for i = 1, #myArray do 2) Let's say I have this code: Code:
function xxx () 3) I want to track boss health. for that I use "boss1" .. "boss4" which relate to the 4 boss frames on the right side of the screen. for example: Code:
UnitHealth("boss1") Is there a way to get unit health through the unit name for example? Any other solution? Thanks in advance! |
1) Depends on the content of your array. If it is an array with unsorted data, then the second solution should be slightly better, but not by much. If it's sorted then doing a binary search would probably be better.
2) The first counter is local to the scope of xxx(), while the for loops counter is only local to the for loop. quick example: Lua Code:
3) You can do it by tracking changes to the HP through the combat log or through someones target. No directly easy way to track such when there's no valid unit id to track. |
Thanks!
Can you elaborate a bit about the impact of unsorted array on those two search methods? btw, I also saw usage of "ipairs" instead of "pairs". Which one is better for going over array elements? Thanks. |
It means by either having numerical keys or a dictionary-style lookup table. (ie, whether you name the keys or leave them as numbers)
|
Array = indexed table = table where the keys are sequential integers beginning with 1.
Dictionary = hash table = table where the keys are anything else. Unlike some programming languages, Lua uses the same data structure -- the table -- for both paradigms. This is an array: Code:
local t = { Code:
local t = { Code:
for i, v in ipairs(t) do print(i, v) end Quote:
Code:
local t = { Code:
for i, v in ipairs(t) do print(i, v) end Quote:
You can also use pairs on an array, and it will return all the values, including the ones that ipairs skips because they aren't sequential, but it returns them in an undefined order, which basically means random: Code:
for k, v in ipairs(t) do print(k, v) end Quote:
Code:
local t = { Code:
for i, v in ipairs(t) do print(i, v) end Quote:
Code:
for k, v in ipairs(t) do print(k, v) end Quote:
Code:
for i = 1, #t do print(i, t[i]) end Quote:
As for which type of table structure you use, that depends on what kind of data you want to store, and how you want to use it. Without knowing that, I can't make any useful suggestions. |
Phanx,
Thank you for your thorough explanation! Why is it faster to avoid ipairs and simply iterate through the array, as you wrote? |
This thread might also be of help
http://forums.wowace.com/showthread.php?t=19541 Quote:
Quote:
and maybe someone is going to pop up with some benchmarks |
Quote:
Here are some very basic benchmark results between an empty loop iterating over an array with 10,000,000 entries using ipairs versus the # operator. The code is at the bottom of the post: Code:
Generating array with 10000000 entries... Lua Code:
|
Very interesting, thank you for that benchmark!
|
Quote:
Code:
local function DoAnotherThing() |
Then why in the wow programming book they wrote that
Code:
table.insert(tbl, "new element") Code:
tbl[#tbl + 1] = "new element" Quote:
|
Because it's "tedious and error-prone". The second way is faster, though, and it's what I tend to use.
|
The most efficient code isn't always the most readable code, so if you find a particular construct difficult to read and understand, you might choose to use code that's slightly slower or uses slightly more memory to make the code easier for you to write and maintain.
Generally, the difference between things like: Code:
for i, v in ipairs(tbl) do Code:
for i = 1, #tbl do Code:
table.insert(tbl, "lolcats") Code:
tbl[#tbl+1] = "lolcats" The exception would be if you're using it somewhere where speed really matters, like inside an OnUpdate script or in response to an event that fires very frequently like COMBAT_LOG_EVENT_UNFILTERED; in these kinds of places, you should always use the faster code. |
All times are GMT -6. The time now is 05:11 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI