Page 1 of 1

hostedUnits.Aircraft

Posted: Mon Oct 08, 2018 5:10 pm
by SeaQueen
I recently did this:

base = ScenEdit_GetUnit({guid=g})

print(base.HostedUnits.Aircraft)
print(#base.HostedUnits.Aircraft)

The print statement returns a full table, but the length of the table is zero. I've played around a little bit more with the code and got an error along the lines of "userdata expected table." Is there a way to cast base.HostedUnits.Aircraft as a table? It looks like the index is a string, not a number, which is a little weird.

RE: hostedUnits.Aircraft

Posted: Tue Oct 09, 2018 5:27 am
by michaelm75au
This is a bit complicated as it is a table within a table - ['Aircraft'] and ['Boats']

Code: Select all

 local u = ScenEdit_GetUnit({name='LST 4001 Osumi', guid='8269b881-20ce-4f2e-baa0-6823e46d55a4'})
 print(u.hostedUnits)
 print(u.hostedUnits.Aircraft)
 print(#(u.hostedUnits.Aircraft))
 for i=1, #(u.hostedUnits.Aircraft),1 do
 local un = ScenEdit_GetUnit({name=u.hostedUnits.Aircraft[i]})
 print(i .. " " .. u.hostedUnits.Aircraft[i] .. ' ' .. un.name)
 end
 

RE: hostedUnits.Aircraft

Posted: Tue Oct 09, 2018 9:30 am
by SeaQueen
So the key bit is the parenthesis?

RE: hostedUnits.Aircraft

Posted: Tue Oct 09, 2018 2:26 pm
by SeaQueen
#(base.HostedUnits.Aircraft)

still returned a zero.

I had to write a function to count the elements in order to return the length of the table.

RE: hostedUnits.Aircraft

Posted: Tue Oct 09, 2018 7:49 pm
by michaelm75au
I don't seem to have an issue when I run it. The bold lines cycle thru the table.
Console:

Code: Select all

 local u = ScenEdit_GetUnit({name='LST 4001 Osumi', guid='8269b881-20ce-4f2e-baa0-6823e46d55a4'})
 print(u.hostedUnits.Aircraft) -- table of GUIDs
 print(#(u.hostedUnits.Aircraft))  -- number of entries
 [b]for i=1, #(u.hostedUnits.Aircraft),1 do
 local un = ScenEdit_GetUnit({name=u.hostedUnits.Aircraft[i]})
 print(i .. " " .. u.hostedUnits.Aircraft[i] .. ' ' .. un.name)  -- list items
 end[/b]
 

output:
{ [1] = '7a3b7dce-abe1-4b60-b0d5-b0f27510d4de', [2] = 'e7bb5afe-033d-4f9f-a248-067484d6168c', 1 = '7a3b7dce-abe1-4b60-b0d5-b0f27510d4de', 2 = 'e7bb5afe-033d-4f9f-a248-067484d6168c' }

2

1 7a3b7dce-abe1-4b60-b0d5-b0f27510d4de Seeker #1
2 e7bb5afe-033d-4f9f-a248-067484d6168c Seeker #2


RE: hostedUnits.Aircraft

Posted: Tue Oct 09, 2018 7:52 pm
by michaelm75au
I assume that this is a typo as the Lua name is "hostedUnits"

#(base.HostedUnits.Aircraft)

RE: hostedUnits.Aircraft

Posted: Tue Oct 09, 2018 8:31 pm
by SeaQueen
Yes.

RE: hostedUnits.Aircraft

Posted: Tue Oct 09, 2018 8:37 pm
by michaelm75au
I updated post #5 sample. The '' was being treated as italic font and not showing.

RE: hostedUnits.Aircraft

Posted: Tue Oct 09, 2018 8:40 pm
by michaelm75au
Can you attach the save so I can run the code against it?
Originally, this wasn't really a table, just an array of guids. I changed it to add table entries sometime back in addition to the old entries, but it should still be able to report how many items in the table.

RE: hostedUnits.Aircraft

Posted: Fri Oct 12, 2018 11:40 am
by tjhkkr
ORIGINAL: michaelm75au

I don't seem to have an issue when I run it. The bold lines cycle thru the table.
Console:

Code: Select all

 local u = ScenEdit_GetUnit({name='LST 4001 Osumi', guid='8269b881-20ce-4f2e-baa0-6823e46d55a4'})
 print(u.hostedUnits.Aircraft) -- table of GUIDs
 print(#(u.hostedUnits.Aircraft))  -- number of entries
 [b]for i=1, #(u.hostedUnits.Aircraft),1 do
 local un = ScenEdit_GetUnit({name=u.hostedUnits.Aircraft[i]})
 print(i .. " " .. u.hostedUnits.Aircraft[i] .. ' ' .. un.name)  -- list items
 end[/b]
 

output:
{ [1] = '7a3b7dce-abe1-4b60-b0d5-b0f27510d4de', [2] = 'e7bb5afe-033d-4f9f-a248-067484d6168c', 1 = '7a3b7dce-abe1-4b60-b0d5-b0f27510d4de', 2 = 'e7bb5afe-033d-4f9f-a248-067484d6168c' }

2

1 7a3b7dce-abe1-4b60-b0d5-b0f27510d4de Seeker #1
2 e7bb5afe-033d-4f9f-a248-067484d6168c Seeker #2
[/color]

Now, does this work ONLY with ships/aircraft that are presently docked/landed? Suppose there is an aircraft in the air that uses the carrier as its home base? Will it still be a part of the table?
The reason I ask is because I am constructing a scenario for Hungnam where combat operations were occurring at below zero temperatures, and I want to simulate the greater maintenance issues involved in that kind of situation.
If so, is there status to tell me the aircraft is in the air?

Thank you! You folks are always very helpful.

RE: hostedUnits.Aircraft

Posted: Fri Oct 12, 2018 6:22 pm
by michaelm75au
Yes, it only shows the units 'embarked' on the unit.
The option for units 'assigned to' sounds useful - I may rename this property to embarkedUnits() and add assignedUnits() to cover the units assigned to this unit.

RE: hostedUnits.Aircraft

Posted: Fri Oct 12, 2018 8:06 pm
by michaelm75au
ORIGINAL: SeaQueen

It looks like the index is a string, not a number, which is a little weird.

Yes, the ' 1 = ..' entries are not table items, and are only there because this was not originally a table. The '[1] = ..' items are the proper table items and should be counted as such.
I would have drop the ' 1 = ..' entries but not sure if designers had already started using this property before I realized the error. So I left it in so scenarios wouldn't break.

RE: hostedUnits.Aircraft

Posted: Fri Oct 12, 2018 8:15 pm
by michaelm75au
unit.airbornetime will show how long the plane has been airborne. This should be 0 if not flying.

You could use the old method of looking thru all the units and finding the ones with the base you are after. And then using the above to see if it is still flying.

[I hope to replace the old method with the new assignedUnits() which will basically do the search in the code which will actually be faster than Lua scanning the units.]

RE: hostedUnits.Aircraft

Posted: Wed Oct 17, 2018 1:43 am
by tjhkkr
Thank you, as always, that will get me where I need it to go.
Thank you!