local dbid = 9 -- create a local variable call dbid equal to 9 were 9 is the id of the unit in the data base i use for the scenario
local quantity = 50 -- create a local variable call quantity equal to 50 in this case to number of object with the data base id 9 I want to be move
--Yes
local cargo = ScenEdit_GetUnit({name = "Blue Airfield1"}).cargo[1].cargo -- create a local variable call cargo equal to the fist listed cargo element contain in the unit name blue arifield1
print(cargo) -- print all the detail of the fist cargo element of blue airfield1 but in this case for the script it is actually the value of the local variable call cargo
--Actually, this calls not "first cargo element" in cargo table. See below.
for k, v in pairs (cargo) do -- call 2 element of the printed table of the local variable call cargo K and V. (I don't understand why K and V also next V is not use.)
if (cargo[k].dbid == dbid) and (cargo[k].quantity >= quantity) then -- check if the data base id of the cargo element is the same as the one define in the local variable dbid and check if the amount is superior or equal to the one define in the local variable quantity (Why use only k and v is not call?)
---Not profound reason
you can use v.dbid or something with no problem.
return true --or do something -- place to execute my script
end -- end the script
end -- i don't know with to end it more then one time
---first end closes if statement. second one closes for loop.
Well... As you noticed, my script at #2 post actually had bit of flaws.
--I counted container itself, not actual container contents. Is there a way to count such contents? As this is far beyond my skill, so here I will stick to how to count container.
--next(cargo) ~= nil check should have included. Using this script against unit without any cargo gets error.
edit; if there is more then 1 cargo with the same id it doesn't work. so if i have 10 cargo of id 9 and i do
local quantity = 1 -- it work
local quantity = 5 -- it doesn't work
--Yup. Container cargo are stored separately, i.e. instead of x50 pallet entry, there are 50 separete single pallet entry. So, counting each quantity always end up with 1.
local cargo = ScenEdit_GetUnit({name = "Blue Airfield1"}).cargo[1].cargo -- create a local variable call cargo equal to the fist listed cargo element contain in the unit name blue arifield1
ok the last one didn't work as cargo[2].cargo doesn't work. So if there is only one cargo type then I can make it work but if there is 2 cargo type, stranded personal and the pallet in this case then i doesn't work.
-- Related to this, there needs some complicated explanation why I used cargo[1].cargo. In short, this calls cargo table itself, not first cargo item.
For example, run this script without [1].cargo to get “parent” cargo table.
Code: Select all
local cargo = ScenEdit_GetUnit({name = "Blue Airfield2"}).cargo
print(cargo)
You will get following result (line break for ease of view):
Code: Select all
{
[1] = { guid = 'L1VD8C-0HN0UETMI2767', name = 'Blue Airfield2',
cargo = {
[1] = { mass = 0.0540000014007092, Type = 4, quantity = 1, crew = 0, status = 'Operational', guid = 'L1VD8C-0HN1DDH7SHTAM', name = 'Pallet Container', dbid = 9, area = 1.23870730400085 },
[2] = { mass = 0.0540000014007092, Type = 4, quantity = 1, crew = 0, status = 'Operational', guid = 'L1VD8C-0HN1DDH7SHTAO', name = 'Pallet Container', dbid = 9, area = 1.23870730400085 },
[3] = { mass = 0.0540000014007092, Type = 4, quantity = 1, crew = 0, status = 'Operational', guid = 'L1VD8C-0HN1DDH7SHTAQ', name = 'Pallet Container', dbid = 9, area = 1.23870730400085 },
…
[20] = { mass = 0.0540000014007092, Type = 4, quantity = 1, crew = 0, status = 'Operational', guid = 'L1VD8C-0HN1DDH7SHTAS', name = 'Pallet Container', dbid = 9, area = 1.23870730400085 },
}
}
Note that “actual” cargo table listing each cargo items is sub-table placed under [1] = {guid='L1VD8C-0HN0UETMI2767', name = 'Blue Airfield2', cargo = ...} value table. Using cargo[1].cargo can call this cargo table directly.
By the way, path to second cargo should be:
(Unit wrapper).cargo[1].cargo[2]
And not
(Unit wrapper).cargo[2]