[2.0.x Space Age] Get inventory of a launched rocket

Place to get help with not working mods / modding interface.
User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

[2.0.x Space Age] Get inventory of a launched rocket

Post by picklock »

How do I get the payload or inventory of a launched rocket in V2.0?
The code from V1.1 no longer works and I couldn't find anything about it. This is how I solved it in V1.1:

Code: Select all

event.rocket.get_inventory(defines.inventory.rocket).get_contents()
Can someone please help me.
Last edited by picklock on Tue Nov 05, 2024 4:53 pm, edited 1 time in total.
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
User avatar
BraveCaperCat
Fast Inserter
Fast Inserter
Posts: 242
Joined: Mon Jan 15, 2024 10:10 pm
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by BraveCaperCat »

What is the

Code: Select all

event
object you are referring to? Are there any errors when you try to use that code in 2.0?
Creator of multiple mods, including Quality Assurance - My most popular one. Expect multiple modding-related questions, answers and other posts.
User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by picklock »

Hi BraveCaperCat, an everyone else,

the event is: on_rocket_launched

The error is: Error while running event PicksRocketStats::on_rocket_launched (ID 10)
'inventory index': real number expected got nil.
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
User avatar
Silari
Filter Inserter
Filter Inserter
Posts: 530
Joined: Sat Jan 27, 2018 10:04 pm
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by Silari »

defines.inventory.rocket no longer exists per the docs: https://lua-api.factorio.com/latest/def ... .inventory

That means you're sending nil to the get_inventory method, so it fails. Not sure how to solve it though - none of the ones that are there look to be correct. For lack of a better guess, try defines.inventory.cargo_unit ?
User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by picklock »

Hi Silari,

thank you for your feedback.
I had already seen that defines.inventory.rocket no longer exists according to the documentation.

I have also tried it with defines.inventory.rocket_silo_rocket. But that didn't work either. Your tip defines.inventory.cargo_unit does not work either. In both cases the error message appears:

Code: Select all

Error while running event PicksRocketStats::on_rocket_launched (ID 10)
__PicksRocketStats__/control.lua:631: attempt to get length of local 'pi_rss_rocket_inventory' (a nil value)
It looks to me as if the inventories do not belong to the rocket that has just been launched and is being returned via the event (on_rocket_launched; event.rocket ).

Does anyone else have an idea how I can determine the payload of a launched rocket with the V2.0?
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
User avatar
LCStark
Fast Inserter
Fast Inserter
Posts: 157
Joined: Thu Jan 28, 2021 5:04 pm
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by LCStark »

User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by picklock »

Hi LCStark,

thank you. I will check this out.
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by picklock »

cargo_pod does also not work for me.

Any other ideas?
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
Xorimuth
Filter Inserter
Filter Inserter
Posts: 692
Joined: Sat Mar 02, 2019 9:39 pm
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by Xorimuth »

picklock wrote: Mon Nov 04, 2024 3:01 pm cargo_pod does also not work for me.

Any other ideas?
You have to get the inventory of the rocket.cargo_pod
My mods
Content: Lunar Landings | Freight Forwarding | Spidertron Patrols | Spidertron Enhancements | Power Overload
QoL: Factory Search | Module Inserter Simplified | Wire Shortcuts X | Ghost Warnings
User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by picklock »

Thanks for the answer. I have tried it. it does not work with the following code (event = on_rocket_launched).

Code: Select all

local myCargoPod = event.rocket.cargo_pod
local myInventory = myCargoPod.get_inventory()
I get the following error message with the 2nd line of the code above:

Code: Select all

attempt to index local 'myCargoPod' (a nil value)
How can I access the cargo_pod and cargo_pod inventory? I am at a loss.
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
User avatar
LCStark
Fast Inserter
Fast Inserter
Posts: 157
Joined: Thu Jan 28, 2021 5:04 pm
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by LCStark »

Are you by any chance using the launched item to unlock a technology? The only time I've noticed cargo pod not being readable is when I had a technology unlocked by launching an item into orbit. Any subsequent launches of the same item worked fine though.
User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by picklock »

I have checked this again. I have sent items to the platform. It seems to me that "myCargoPod" is not valid if I set it via "local myCargoPod = event.rocket.cargo_pod".
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
User avatar
BraveCaperCat
Fast Inserter
Fast Inserter
Posts: 242
Joined: Mon Jan 15, 2024 10:10 pm
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by BraveCaperCat »

Check if event.rocket.type is equal to "RocketSiloRocket" before getting the inventory of event.rocket.cargo_pod.
Creator of multiple mods, including Quality Assurance - My most popular one. Expect multiple modding-related questions, answers and other posts.
Xorimuth
Filter Inserter
Filter Inserter
Posts: 692
Joined: Sat Mar 02, 2019 9:39 pm
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by Xorimuth »

picklock wrote: Mon Nov 04, 2024 6:41 pm Thanks for the answer. I have tried it. it does not work with the following code (event = on_rocket_launched).

Code: Select all

local myCargoPod = event.rocket.cargo_pod
local myInventory = myCargoPod.get_inventory()
I get the following error message with the 2nd line of the code above:

Code: Select all

attempt to index local 'myCargoPod' (a nil value)
How can I access the cargo_pod and cargo_pod inventory? I am at a loss.
Well `get_inventory()` by itself wouldn't work, you'd have to do `event.rocket.cargo_pod.get_inventory(defines.inventory.cargo_unit)`.
This code definitely works: https://github.com/tburrows13/LunarLand ... o.lua#L395 although it hasn't be extensively tested so there may be some edge cases where it fails. This is only on a base rocket silo though, I don't know if it works the same with a SA silo (launch_to_space_platforms = true).
My mods
Content: Lunar Landings | Freight Forwarding | Spidertron Patrols | Spidertron Enhancements | Power Overload
QoL: Factory Search | Module Inserter Simplified | Wire Shortcuts X | Ghost Warnings
User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by picklock »

@ Xorimuth:
Thank you for your answer. You are right. With the line of code you mentioned it works in the base game.

@All:
But in Space Age there seems to be no cargo_pod. I get the error message in SA as already posted:

Code: Select all

Error while running event PicksRocketStats::on_rocket_launched (ID 10)
__PicksRocketStats__/control.lua:631: attempt to index field 'cargo_pod' (a nil value)
The corresponding code line is:

Code: Select all

local pi_rss_rocket_inventory = event.rocket.cargo_pod.get_inventory(defines.inventory.cargo_unit)
Does anyone else have an idea how I can read the inventory of the rocket in Space Age?
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
User avatar
LCStark
Fast Inserter
Fast Inserter
Posts: 157
Joined: Thu Jan 28, 2021 5:04 pm
Contact:

Re: [2.0.x] Get inventory of a launched rocket

Post by LCStark »

Xorimuth wrote: Tue Nov 05, 2024 2:40 pm This is only on a base rocket silo though, I don't know if it works the same with a SA silo (launch_to_space_platforms = true).

I've tried it with the SA silo and it does seem to only work with items sent to orbit, items sent to platform don't seem to have an accessible cargo pod.

picklock wrote: Tue Nov 05, 2024 3:39 pm But in Space Age there seems to be no cargo_pod.

I think you may be right. I've made this little demonstration:

Code: Select all

data.lua

data.raw["item"]["accumulator"].send_to_orbit_mode = "automated"

control.lua

function test_on_rocket_launched(event)
  if event.rocket and event.rocket.valid then
    if event.rocket.cargo_pod then
      game.print("rocket had a cargo pod!")
      game.print(serpent.block(event.rocket.cargo_pod))
      game.print("item count: " .. tostring(event.rocket.cargo_pod.get_item_count()))
    else
      game.print("no cargo pod detected!")
    end
  end
end
script.on_event(defines.events.on_rocket_launched, test_on_rocket_launched)

... and this is the result:



So unless it was a deliberate choice to not allow access to the cargo pod sent to a platform, I'd guess it is a bug. Might want to post it in the Bug Reports subforum.
Attachments
cargo_pod.mp4
(27.22 MiB) Downloaded 12 times
User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: [2.0.x Space Age] Get inventory of a launched rocket

Post by picklock »

Thanks again for the answer and the testing.
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
User avatar
LCStark
Fast Inserter
Fast Inserter
Posts: 157
Joined: Thu Jan 28, 2021 5:04 pm
Contact:

Re: [2.0.x Space Age] Get inventory of a launched rocket

Post by LCStark »

In the meantime, I guess a temporary workaround that might work for you is to use the `on_rocket_launch_ordered` event, it seems to detect the cargo pod on both types of launches.
User avatar
picklock
Fast Inserter
Fast Inserter
Posts: 214
Joined: Sat Nov 09, 2019 6:49 am
Contact:

Re: [2.0.x Space Age] Get inventory of a launched rocket

Post by picklock »

Yes, the workaround works. Many thanks for your support.
My Mods: Picklocks Fusion Power | Picklocks Inserter | Picklocks Lithium Polymer Accumulator | Picklocks rocket silo stats | Picklocks Set Inventory Filters | Picklocks QuickBar Import/Export | Picklocks Nauvis Cliff-Explosives
Post Reply

Return to “Modding help”