[2.0.x Space Age] Get inventory of a launched rocket
Posted: Wed Oct 23, 2024 5:20 pm
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:
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 ?
Re: [2.0.x] Get inventory of a launched rocket
Posted: Thu Oct 24, 2024 5:37 am
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:
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?
How can I access the cargo_pod and cargo_pod inventory? I am at a loss.
Re: [2.0.x] Get inventory of a launched rocket
Posted: Mon Nov 04, 2024 11:51 pm
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.
Re: [2.0.x] Get inventory of a launched rocket
Posted: Tue Nov 05, 2024 6:20 am
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".
Re: [2.0.x] Get inventory of a launched rocket
Posted: Tue Nov 05, 2024 2:22 pm
by BraveCaperCat
Check if event.rocket.type is equal to "RocketSiloRocket" before getting the inventory of event.rocket.cargo_pod.
Re: [2.0.x] Get inventory of a launched rocket
Posted: Tue Nov 05, 2024 2:40 pm
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).
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).
Re: [2.0.x] Get inventory of a launched rocket
Posted: Tue Nov 05, 2024 3:39 pm
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:
Error while running event PicksRocketStats::on_rocket_launched (ID 10)
__PicksRocketStats__/control.lua:631: attempt to index field 'cargo_pod' (a nil value)
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?
Re: [2.0.x] Get inventory of a launched rocket
Posted: Tue Nov 05, 2024 4:13 pm
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:
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.
Re: [2.0.x Space Age] Get inventory of a launched rocket
Posted: Tue Nov 05, 2024 4:54 pm
by picklock
Thanks again for the answer and the testing.
Re: [2.0.x Space Age] Get inventory of a launched rocket
Posted: Tue Nov 05, 2024 5:23 pm
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.
Re: [2.0.x Space Age] Get inventory of a launched rocket
Posted: Tue Nov 05, 2024 5:53 pm
by picklock
Yes, the workaround works. Many thanks for your support.