Setting a default fluid amount

Place to get help with not working mods / modding interface.
wizmut
Burner Inserter
Burner Inserter
Posts: 7
Joined: Fri Apr 26, 2019 3:46 am
Contact:

Setting a default fluid amount

Post by wizmut »

I am trying to fill my new 'tube' entity with some 'air' in it by default. 50 units of air. Tubes have the Storage Tank prototype.

I have this snippet in control.lua:

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
    local entity = event.entity

    if entity.name == "tube" or entity.name == "tube-elbow" then
        if entity.fluidbox then
            entity.fluidbox[1] = {name = "air", amount = 50}
        end
    end
end)
The result is quite confusing. Placing down an individual tube will indeed create a tube entity with 50 air in it. But if I place another tube down connected to it, then each will only have 25 air. Placing down two tubes separated by an empty space will give each 50 air, but if I place another between them then they'll all have 16.6 air. However, if I place down the middle one unconnected (tubes are directional), and then rotate it, then each will have 50 air.

It seems that the whole fluid system is being given the amount of air that I assign to one tube.

It gets weirder. If I change my code to try and anticipate the amount of fluid currently in the system, say with this code:

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
    local entity = event.entity

    if entity.name == "tube" or entity.name == "tube-elbow" then
		local current_fluid = 0
	
		if entity.fluidbox[1] then
			game.print("entity.fluidbox[1].amount " .. entity.fluidbox[1].amount)
			current_fluid = entity.fluidbox[1].amount
		end
	
		if entity.fluidbox then
			local added_fluid = 50
			local new_fluid_amount = current_fluid + added_fluid
		entity.fluidbox[1] = {name = "air", amount = new_fluid_amount}
		end
	end
end)
then it will be even crazier. Say I have 3 tubes in a row with 50 air each. Attaching a fourth tube will result in each tube having (you guessed it) 21.8 air! After some investigation, this figure comes from the 150 total air in the system being divided into 4 tubes, so 37.5 each. Then the 50 is added (great). So now the new tube has 87.5 air, and should donate it to the rest of the tubes, right? Nope, it evacuates the air in the other tubes and each tube now has 87.5 / 4 = 21.8 air in it, after some rounding down.

If I could assume that every connected entity will have the same capacity, then I could hack around this, but not every air-holding entity will have 100 capacity. I am looking for some way to set a default starting amount of fluid for an entity.
Post Reply

Return to “Modding help”