Buzzer1977 Posted March 20, 2022 Posted March 20, 2022 Hi, I'm a absolute lua noob and i don't know why the table 'retval' that i'm filling with key(unit name), value (distance) pairs remains empty. I've added a counter and some debug output, so adding the unit to the table get's called, but the table remains empty. What am i doing wrong ? I'd guess its something trivial i just can't see because i'm new to lua. Any help would be appreciated Regards Buzzer function units_in_range(position, range, filter, matches ) retval = {} units = mist.getUnitsByAttribute(filter,matches,false) local count = 0 for id, unit_name in pairs(units) do local unit = Unit.getByName(unit_name) if unit ~= nil then local unit_position = unit:getPosition().p local distance = mist.utils.get3DDist(unit_position, position ) if ( distance < range ) then retval[unit_name] = distance count = count + 1 dbg("UIR: added [" .. unit_name .. "] = " .. distance .. " len: " .. #retval) end end end dbg("UIR added count: " .. count .. " of units: " .. #units .. " but #retval is: " .. #retval) return retval end 2022-03-20 15:06:56.176 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-9] = 552.78357821229 len: 0 2022-03-20 15:06:56.176 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-7] = 180.60067457258 len: 0 2022-03-20 15:06:56.177 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-5] = 1521.1784022833 len: 0 2022-03-20 15:06:56.177 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-10] = 618.28916828517 len: 0 2022-03-20 15:06:56.177 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-3] = 1314.4743733626 len: 0 2022-03-20 15:06:56.177 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-1] = 1245.1431009738 len: 0 2022-03-20 15:06:56.177 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-4] = 1113.1370216443 len: 0 2022-03-20 15:06:56.177 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-8] = 1874.3983142877 len: 0 2022-03-20 15:06:56.177 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-12] = 1547.6561564401 len: 0 2022-03-20 15:06:56.177 INFO SCRIPTING: UIR: added [BLU_GE-TBILISI-LOCHINI-GND-1-2] = 879.63989188534 len: 0 2022-03-20 15:06:56.177 INFO SCRIPTING: UIR added count: 10 of units: 183 but #retval is: 0 AMD Ryzen 9 5950x, MSI MEG x570 Unify, G.Skill 128GB DDR4-3200, MSI RTX3090 Ventus 3x 24GB, Samsung PCIe 4.0 M.2 1TB 980 Pro, Seagate PCIe 4.0 M.2 2TB FireCuda 520, Quest 3
Buzzer1977 Posted March 20, 2022 Author Posted March 20, 2022 Learned it the hard way ... LUA '#' operator won't work on key,value tables. Is there a way to add a size() method to the table type ? This doesn't work ... function table.size(Table) local count = 0 for k,v in pairs( Table ) do count = count + 1 end return count end AMD Ryzen 9 5950x, MSI MEG x570 Unify, G.Skill 128GB DDR4-3200, MSI RTX3090 Ventus 3x 24GB, Samsung PCIe 4.0 M.2 1TB 980 Pro, Seagate PCIe 4.0 M.2 2TB FireCuda 520, Quest 3
toutenglisse Posted March 20, 2022 Posted March 20, 2022 1 hour ago, Buzzer1977 said: ...Any help would be appreciated... Hi, I don't think you can index table's entry with strings but I can be wrong. You can index it with dynamic values like unit's ID number (and have for example 2 entries : retval[1] and retval[14]). In your example I think the easiest way is (I replaced dbg by trigger.action.outText to test on screen directly) : Spoiler function units_in_range(position, range, filter, matches ) retval = {} units = mist.getUnitsByAttribute(filter,matches,false) local count = 0 for id, unit_name in pairs(units) do local unit = Unit.getByName(unit_name) if unit ~= nil then local unit_position = unit:getPosition().p local distance = mist.utils.get3DDist(unit_position, position ) if ( distance < range ) then table.insert(retval, {["name"] = unit_name, ["distance"] = distance}) count = count + 1 trigger.action.outText("UIR: added [" .. retval[count]["name"] .. "] = " .. retval[count]["distance"] .. " len: " .. #retval, 10) end end end trigger.action.outText("UIR added count: " .. count .. " of units: " .. #units .. " but #retval is: " .. #retval, 10) return retval end units_in_range(Unit.getByName('Unité Sol-1-1'):getPoint(), 3000, {coalition = "red"}, 1) 1
Buzzer1977 Posted March 20, 2022 Author Posted March 20, 2022 31 minutes ago, toutenglisse said: Hi, I don't think you can index table's entry with strings but I can be wrong. You can index it with dynamic values like unit's ID number (and have for example 2 entries : retval[1] and retval[14]). In your example I think the easiest way is (I replaced dbg by trigger.action.outText to test on screen directly) : Well, indexing with all kind of values works. It just doesn't work to get the table size with '#'. A standalone function to get the size like this works: function tsize(Table) local count = 0 for k,v in pairs( Table ) do count = count + 1 end return count end n = tsize(table) What doesn't work is to add the function to the table type. function table.size(Table) local count = 0 for k,v in pairs( Table ) do count = count + 1 end return count end n = tab.size() The fun part is, adding functions to the string type works quite well ... function string.starts(String,Start) return string.sub(String,1,string.len(Start))==Start end foo = 'bar' if ( bar.starts('B')) then .... -- to upper and replace '-' with '_' function string.unify(String) return string.gsub(String:upper(), "-", "_") end foo = "Red_CAP-Unit-4_3" foo.unify() -- RED_CAP_UNIT_4_3 AMD Ryzen 9 5950x, MSI MEG x570 Unify, G.Skill 128GB DDR4-3200, MSI RTX3090 Ventus 3x 24GB, Samsung PCIe 4.0 M.2 1TB 980 Pro, Seagate PCIe 4.0 M.2 2TB FireCuda 520, Quest 3
ADHS Posted March 21, 2022 Posted March 21, 2022 (edited) ALA = {"A","B","C","D"} for i = 1, #ALA do if ALA[i] == "B" then print("OK") else print("NO") end end Edited March 21, 2022 by ADHS Democracy was already invented, while Scrat was eating oak fruits.
Recommended Posts