[Logged] Lua .targetedBy return tbl are 0 indexed (Full Version)

All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Tech Support



Message


KnightHawk75 -> [Logged] Lua .targetedBy return tbl are 0 indexed (6/12/2021 12:31:37 AM)

Background
Lua operates on 1 based indexes by default, and assumes such for most operations.
In most places in the CMO Lua API when a table is returned to Lua environment and it's keys are integer based\indexed, it starts at 1.
for example:
quote:

local u = ScenEdit_GetUnit( { guid="someguidhere" } ) --some unit actually seen by another side.
print(u.ascontact);
-->> { [1]={ therecord here }, [2]={ therecordhere }...etc }


However in at least 2 if not more places it is zero based.
quote:

local u = ScenEdit_GetUnit( { guid="someguidhere" } ) -- some unit actually being targeted by another side.
print(u.targetedBy);
-->> { [0]={ therecord here }, [1]={ therecordhere }...etc }

Beyond just consistency the benefit of starting at 1 is that a correct value will be returned on a count request on a table, as well as being able to operate on said table using 'for' with ipairs(tbl), both of which require numeric index's starting > 0 to work properly. In the above for example #u.targetedBy for a count will deceptively return 0 instead of 2. If one is aware of this, there are workarounds, but if one is not aware it can waste fair amount of time tying to track down 'bugs' due to a couple tables here an there breaking from the pattern.

I noticed it specifically (and painfully lol) on the UnitWrapper.targetedBy and the ContactWrapper.targetedBy.

I think this may also be the case with .firedOn, and .firingAt. It may be that anything is using a certain LuaUtility function that converts a .net dictionary to LuaTable that may be what's producing the 0 based return tables. Dev's would have to double check me on that but it seemed that was a common factor in cursory look.

Can this be changed to return the specified table(s) with key's\indexes starting at 1 please?
If not though, may flag these it in the docs as being zero based return tables, noting the exception to the general rule\pattern.




michaelm75au -> RE: Lua .targetedBy return tbl are 0 indexed (6/12/2021 7:48:49 AM)

These 'tables' are created from a .net array which starts at relative '0'. Which may explain why this is happening. If it was reported when the methods were first implemented, I could have changed them.
But I am wary of changing the start number in case it breaks existing Lua scripts in scenarios.




michaelm75au -> RE: Lua .targetedBy return tbl are 0 indexed (6/12/2021 7:58:07 AM)

I checked the Lua code and it is only the wrappers for unit and contact affected.
Methods: firedOn , firingAt, targetedBy

As I did these methods, it is my fault for not noticing the initial discrepancy.




KnightHawk75 -> RE: Lua .targetedBy return tbl are 0 indexed (6/13/2021 2:08:41 PM)

quote:

ORIGINAL: michaelm75au

These 'tables' are created from a .net array which starts at relative '0'. Which may explain why this is happening. If it was reported when the methods were first implemented, I could have changed them.
But I am wary of changing the start number in case it breaks existing Lua scripts in scenarios.


Yeah I saw that. ;) maybe can just tack on a +1 to the original key value if numeric during conversion?

I hear you about changing it. Then again ..as if anyone noticed this but me (kidding! you are right someone may have and adjusted without using pairs), hell I wouldn't have noticed it either unless I was dumbly pre-optimizing for max perf in what I happen to be doing with in certain function to purposely avoided using a 'for'.

quote:

As I did these methods, it is my fault for not noticing the initial discrepancy.

Don't beat yourself up over it, missing 3 out of .. what 100+ maybe 200+ tables isn't so bad. [:D]




WSBot -> RE: Lua .targetedBy return tbl are 0 indexed (6/14/2021 2:49:29 PM)

0014605




michaelm75au -> RE: Lua .targetedBy return tbl are 0 indexed (6/19/2021 5:27:51 AM)

I have updated the Lua Command doc to highlight the offset starting index.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.0234375