Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

[Logged] Lua .targetedBy return tbl are 0 indexed

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Tech Support >> [Logged] Lua .targetedBy return tbl are 0 indexed Page: [1]
Login
Message << Older Topic   Newer Topic >>
[Logged] Lua .targetedBy return tbl are 0 indexed - 6/12/2021 12:31:37 AM   
KnightHawk75

 

Posts: 1244
Joined: 11/15/2018
Status: offline
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.

< Message edited by WSBot -- 6/14/2021 2:49:40 PM >
Post #: 1
RE: Lua .targetedBy return tbl are 0 indexed - 6/12/2021 7:48:49 AM   
michaelm75au


Posts: 13378
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
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.


_____________________________

Michael

(in reply to KnightHawk75)
Post #: 2
RE: Lua .targetedBy return tbl are 0 indexed - 6/12/2021 7:58:07 AM   
michaelm75au


Posts: 13378
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
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.

_____________________________

Michael

(in reply to michaelm75au)
Post #: 3
RE: Lua .targetedBy return tbl are 0 indexed - 6/13/2021 2:08:41 PM   
KnightHawk75

 

Posts: 1244
Joined: 11/15/2018
Status: offline
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.


< Message edited by KnightHawk75 -- 7/18/2021 9:43:07 PM >

(in reply to michaelm75au)
Post #: 4
RE: Lua .targetedBy return tbl are 0 indexed - 6/14/2021 2:49:29 PM   
WSBot

 

Posts: 182
Joined: 1/17/2021
Status: offline
0014605

(in reply to KnightHawk75)
Post #: 5
RE: Lua .targetedBy return tbl are 0 indexed - 6/19/2021 5:27:51 AM   
michaelm75au


Posts: 13378
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
I have updated the Lua Command doc to highlight the offset starting index.

_____________________________

Michael

(in reply to WSBot)
Post #: 6
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Tech Support >> [Logged] Lua .targetedBy return tbl are 0 indexed Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.340