Page 1 of 1

Can't figure out how to access fluidbox

Posted: Wed May 17, 2017 6:22 pm
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!?!?
-__-

Re: Can't figure out how to access fluidbox

Posted: Wed May 17, 2017 8:08 pm
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?

Re: Can't figure out how to access fluidbox

Posted: Wed May 17, 2017 8:37 pm
by prg

Re: Can't figure out how to access fluidbox

Posted: Wed May 17, 2017 11:50 pm
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

Re: Can't figure out how to access fluidbox

Posted: Thu May 18, 2017 6:22 pm
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.

Re: Can't figure out how to access fluidbox

Posted: Thu May 18, 2017 9:51 pm
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

Re: Can't figure out how to access fluidbox

Posted: Thu May 18, 2017 10:46 pm
by Nexela
Could be worse I totally missed the order of not before == :)
Thanks prg