[bug] luaTrain insert_fluid remove_fluid not working as expected

Place to get help with not working mods / modding interface.
adamius
Long Handed Inserter
Long Handed Inserter
Posts: 73
Joined: Mon Mar 19, 2018 9:18 am
Contact:

[bug] luaTrain insert_fluid remove_fluid not working as expected

Post by adamius »

Hi all,

I'm trying to use luaTrain.insert_fluid and luaTrain.remove_fluid.

The documentation for luaTrain isn't helping me. The insert_fluid doesn't have the parameter documented... https://lua-api.factorio.com/latest/Lua ... sert_fluid
(version 0.17.79)

I've tried passing in a fluid array to insert_fluid / remove_fluid of this form
{ type="steam", amount=42 }

But I get a bad argument #3. Something about expecting a string and getting a nil. What string is needed?

Any ideas?

Documentation extract (0.17.79):
remove_fluid(fluid) → double

Remove some fluid from the train.

Parameters
fluid: A table with type and amount
Return value
The amount of fluid actually removed.

insert_fluid(fluid) → double

Inserts the given fluid into the first available location in this train.

Parameters

Return value
The amount inserted.
Last edited by adamius on Fri Dec 06, 2019 4:08 am, edited 1 time in total.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2888
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: luaTrain insert_fluid remove_fluid not working as expected

Post by boskid »

https://lua-api.factorio.com/0.17.79/Co ... html#Fluid
you have "type" when it should be "name"

adamius
Long Handed Inserter
Long Handed Inserter
Posts: 73
Joined: Mon Mar 19, 2018 9:18 am
Contact:

Re: luaTrain insert_fluid remove_fluid not working as expected

Post by adamius »

boskid wrote:
Wed Nov 20, 2019 7:17 am
https://lua-api.factorio.com/0.17.79/Co ... html#Fluid
you have "type" when it should be "name"
Well that helped with getting rid of the error message. Cheers for that, so luaTrain's documentation still needs a nudge, but I can't get the fluid to insert into the train. I can do it using luaEntity.

But via luaTrain.... not so much. For this use case I'm not caring which fluid-wagon receives what. I don't get any errors. The got1 through got3 are implying that luaTrain's version of insert_fluid doesn't work but that luaEntity does.

My expectation is that the water and steam will go somewhere in the train and if still possible, the wagon next to player may receive crude-oil of 25k. What actually happens is the 25k crude-oil goes in without any issue and the other wagons receive nothing.

Here's my code I'm testing with, more-or-less.

Code: Select all

local player = game.players[1]

-- assume a train with locomotive, cargo-wagon and four fluid-wagons. all wagons empty
local train = {}

for idx,entity in ipairs(player.surface.find_entities_filtered{position=player.position,radius=3}) do


  if entity.name == "fluid-wagon" then   -- just grab the train reference from nearest fluid wagon
     print(entity.name)
     train = entity.train

     got1=train.insert_fluid({name="water",amount=250})
     print("water? "..got1)

     got2=train.insert_fluid({name="steam",amount=250})
    print("steam? "..got2)

    got3= entity.insert_fluid({name="crude-oil",amount=25000})
    print("crude-oil? "..got3)

 end

end

if train then
  print("train wagon contents")
    for idx,wagon in ipairs(train.carriages) do
      print(wagon.name)
      print(idx.." "..wagon.get_fluid_count("water").." "..wagon.get_fluid_count("steam").." "..wagon.get_fluid_count("crude-oil"))
   end

end
print("train contents "..train.get_fluid_count("water").." "..train.get_fluid_count("steam").." "..train.get_fluid_count("crude-oil"))


Sample output

Code: Select all


fluid-wagon
water? 0
steam? 0
crude-oil? 25000
train wagon contents
locomotive
1 0 0 0
cargo-wagon
2 0 0 0
fluid-wagon
3 0 0 25000
fluid-wagon
4 0 0 0
fluid-wagon
5 0 0 0
fluid-wagon
6 0 0 0
train contents 0 0 25000


PyroFire
Filter Inserter
Filter Inserter
Posts: 356
Joined: Tue Mar 08, 2016 8:18 am
Contact:

Re: luaTrain insert_fluid remove_fluid not working as expected

Post by PyroFire »

Don't forget temperatures

adamius
Long Handed Inserter
Long Handed Inserter
Posts: 73
Joined: Mon Mar 19, 2018 9:18 am
Contact:

Re: luaTrain insert_fluid remove_fluid not working as expected

Post by adamius »

Ok having looked at this further, I'll treat luatrain's insert_fluid/remove_fluid methods as defective.

PyroFire
Filter Inserter
Filter Inserter
Posts: 356
Joined: Tue Mar 08, 2016 8:18 am
Contact:

Re: luaTrain insert_fluid remove_fluid not working as expected

Post by PyroFire »

adamius wrote:
Sat Nov 23, 2019 5:43 am
Ok having looked at this further, I'll treat luatrain's insert_fluid/remove_fluid methods as defective.
Confirmed issue https://lua-api.factorio.com/latest/Lua ... sert_fluid doesn't work, but https://lua-api.factorio.com/latest/Lua ... sert_fluid does (on the train entity)

Post Reply

Return to “Modding help”