hostedUnits.Aircraft

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
SeaQueen
Posts: 1436
Joined: Sat Apr 14, 2007 4:20 am
Location: Washington D.C.

hostedUnits.Aircraft

Post 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.
User avatar
michaelm75au
Posts: 12467
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: hostedUnits.Aircraft

Post 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
 
Michael
User avatar
SeaQueen
Posts: 1436
Joined: Sat Apr 14, 2007 4:20 am
Location: Washington D.C.

RE: hostedUnits.Aircraft

Post by SeaQueen »

So the key bit is the parenthesis?
User avatar
SeaQueen
Posts: 1436
Joined: Sat Apr 14, 2007 4:20 am
Location: Washington D.C.

RE: hostedUnits.Aircraft

Post 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.
User avatar
michaelm75au
Posts: 12467
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: hostedUnits.Aircraft

Post 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

Michael
User avatar
michaelm75au
Posts: 12467
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: hostedUnits.Aircraft

Post by michaelm75au »

I assume that this is a typo as the Lua name is "hostedUnits"

#(base.HostedUnits.Aircraft)
Michael
User avatar
SeaQueen
Posts: 1436
Joined: Sat Apr 14, 2007 4:20 am
Location: Washington D.C.

RE: hostedUnits.Aircraft

Post by SeaQueen »

Yes.
User avatar
michaelm75au
Posts: 12467
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: hostedUnits.Aircraft

Post by michaelm75au »

I updated post #5 sample. The '' was being treated as italic font and not showing.
Michael
User avatar
michaelm75au
Posts: 12467
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: hostedUnits.Aircraft

Post 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.
Michael
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: hostedUnits.Aircraft

Post 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.
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
User avatar
michaelm75au
Posts: 12467
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: hostedUnits.Aircraft

Post 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.
Michael
User avatar
michaelm75au
Posts: 12467
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: hostedUnits.Aircraft

Post 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.
Michael
User avatar
michaelm75au
Posts: 12467
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: hostedUnits.Aircraft

Post 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.]
Michael
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: hostedUnits.Aircraft

Post by tjhkkr »

Thank you, as always, that will get me where I need it to go.
Thank you!
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
Post Reply

Return to “Lua Legion”