mrSkortch / MissionScriptingTools

Mission Scripting Tools for Digital Combat Simulator
GNU General Public License v3.0
190 stars 42 forks source link

getUnitsInZones return value seems incorrect? #58

Closed Kinkkujuustovoileipa closed 3 years ago

Kinkkujuustovoileipa commented 3 years ago

Hi, I'm having some issues with getUnitsInZones. It's returning a table with different format than expected or documented (as best I can make out from the documentation).

I have a simple mission with a few named units. Upon execution and evaluation of the debug output I'm seeing the following: mist.makeUnitTable({'[blue][vehicle]'}) is returning: { [1] = FARP 1 BLUE VEH 1,[2] = FARP 1 BLUE VEH 2,[3] = FARP 1 BLUE VEH 3,[4] = FARP A RED VEH 1-1-1,[5] = FARP 1 RED VEH 1-1-1,[6] = FARP 1 BLUE VEH 4,["processed"] = 1.801,} which seems to make sense - apart from the last value, which I can live with. However: mist.getUnitsInZones(mist.makeUnitTable({'[blue][vehicle]'}), zone_names) is returning: { [1] = { ["id_"] = 16779264,} ,[2] = { ["id_"] = 16780032,} ,[3] = { ["id_"] = 16780288,} ,[4] = { ["id_"] = 16780544,} ,}

I can't see anything obviously wrong with my code, albeit I'm fairly new to Lua and finding it somewhat obnoxious to work with. Could be it an issue with the function returning the unit IDs instead of the names?

Kinkkujuustovoileipa commented 3 years ago

Did some investigation and fixed by getting the name of the unit being returned within the MIST function. I changed lines 2655 and 2658 in mist.getUnitsInZones to in_zone_units[#in_zone_units + 1] = Unit.getName(units[units_ind]). I'll leave this here, I'm not sure if original return was by design or not, but personally think it's better to have it consistent with makeUnitTable.

mrSkortch commented 3 years ago

Yes it is correct, but the documentation could be more clear. It is returning a table of unit objects. Its a bit more useful because you can run any unit function on it immediately without having to first get that object again. Your change is fine if you are only concerned about getting the unit names, but if you are wanting to run any other function on it then you have to recall Unit.getByName for each one. With the original code that won't be changed you can run something like this to get their names.

local whatever = mist.getUnitsInZones(mist.makeUnitTable({'[blue][vehicle]'}), 'zone')
for i = 1, #whatever do
   env.info(Unit.getName(whatever[i]))
end

Btw the processed key is mostly used for the flagfuncs and will only impact any loop you run on what is returned if you use in pairs and don't check the key or value type. for i = 1, #table will only iterate numbered keys. If you used in pairs you just have to filter out the processed value.

for key, val in pairs(whatever) do
   if type(key) == 'number' then
      -- do your code
   end 
end