Can't figure out how to access fluidbox

Place to get help with not working mods / modding interface.
yay
Burner Inserter
Burner Inserter
Posts: 8
Joined: Mon May 08, 2017 12:17 am
Contact:

Can't figure out how to access fluidbox

Post by yay »

Hi!


I can't figure out what I'm doing wrong...

I checked that I have the right entity reference by destroying it and watching it disappear in-game. Also double-checked that a fluid icon is actually visible the output slot of the building GUI.

Code: Select all

				for i=1,#entity.fluidbox do
					local fluidbox = entity.fluidbox[i]
					if not fluidbox == nil then
						print("Emptying " .. fluidbox["type"] .. "( " .. fluidbox["amount"] .. ")")
						entity.damage(fluidbox["amount"], "player")
						entity.fluidbox[i] = nil
					else
						print(i .. "nil")
					end

				end
(I called it on_tick.)

Output:
TTY wrote:...
1nil
2nil
1nil
2nil
...
Whyyyyyyyyy!?!?
-__-
yay
Burner Inserter
Burner Inserter
Posts: 8
Joined: Mon May 08, 2017 12:17 am
Contact:

Re: Can't figure out how to access fluidbox

Post by yay »

I took a look at "Bottleneck", which accesses the fluidboxed (thanks!) and changed:

Code: Select all

if not fluidbox == nil[code]
to
[code]if fluidbox
And with that it works - I don't understand why.
Is that a LUA thing or is that something to do with the operator in the factorio API?
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Can't figure out how to access fluidbox

Post by prg »

Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Can't figure out how to access fluidbox

Post by Nexela »

Code: Select all

fluidbox = nil
if not fluidbox == nil   then
evaluates to

Code: Select all

if not true then
which evalautes to

Code: Select all

if false then
And since it is false goes to the else part
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Can't figure out how to access fluidbox

Post by prg »

Nexela wrote:

Code: Select all

fluidbox = nil
if not fluidbox == nil   then
evaluates to

Code: Select all

if not true then
which evalautes to

Code: Select all

if false then
And since it is false goes to the else part
That's not what happens.

According to operator precedence, "not" has higher priority than "==".

So in this expression

Code: Select all

not fluidbox == nil
"not fluidbox" gets evaluated first. Depending on the fluidbox variable, this will become true (if it's nil) or false (if it contains an actual fluidbox table).

Now both "true == nil" and "false == nil" are false, so we always go into the else branch.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
yay
Burner Inserter
Burner Inserter
Posts: 8
Joined: Mon May 08, 2017 12:17 am
Contact:

Re: Can't figure out how to access fluidbox

Post by yay »

AAAAARGH, thanks, now I see it!

My brain reads LUA as Python - I looked at that line a hundred times, never would have found the problem xD
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Can't figure out how to access fluidbox

Post by Nexela »

Could be worse I totally missed the order of not before == :)
Thanks prg
Post Reply

Return to “Modding help”