Page 1 of 2

That facepalm moment when ...

Posted: Sat Feb 06, 2021 3:00 am
by togFox
... I spent 2 hours trying to nut out my failed vector math only to discover I didn't convert to radians. :cry:

You?

Re: That facepalm moment when ...

Posted: Sat Feb 06, 2021 9:30 am
by darkfrei

Code: Select all

function update_vector (vector, reset_vector)
  if reset_vector then 
    vector = {0,0} 
  end
end

local vector = {1,1}
update_vector (vector, true)
print (vector[1]..' '..vector[2]) -- 1 1

Re: That facepalm moment when ...

Posted: Wed Feb 17, 2021 6:30 pm
by milon
Haha! Been there, done that - both of those, actually!

Here's most recent facepalm:

Code: Select all

    for x = 1, width do
        for y = 1, height do
            heightmap[x][y] = 1 - math.abs(heightmap[x][y] * 2 - 1)
            watermap[x][y] = math.abs(heightmap[x][y] * 2 - 1)
        end
    end

Re: That facepalm moment when ...

Posted: Wed Feb 17, 2021 8:13 pm
by darkfrei
milon wrote: Wed Feb 17, 2021 6:30 pm Haha! Been there, done that - both of those, actually!

Here's most recent facepalm:

Code: Select all

    for x = 1, width do
        for y = 1, height do
            heightmap[x][y] = 1 - math.abs(heightmap[x][y] * 2 - 1)
            watermap[x][y] = math.abs(heightmap[x][y] * 2 - 1)
        end
    end
What was expected? Something like this?

Code: Select all

for x = 1, width do
	for y = 1, height do
		local h = math.abs(heightmap[x][y] * 2 - 1)
		heightmap[x][y] = 1 - h
		watermap[x][y] = h
	end
end

Re: That facepalm moment when ...

Posted: Thu Feb 18, 2021 6:44 pm
by milon
Nah, it was copypasta. The line should have read:
watermap[x][y] = math.abs(watermap[x][y] * 2 - 1)

But I see where you're coming from. I didn't exactly give any context.

Re: That facepalm moment when ...

Posted: Wed Mar 03, 2021 3:56 pm
by ZuccaroMateo
darkfrei wrote: Sat Feb 06, 2021 9:30 am

Code: Select all

function update_vector (vector, reset_vector)
  if reset_vector then 
    vector = {0,0} 
  end
end

local vector = {1,1}
update_vector (vector, true)
print (vector[1]..' '..vector[2]) -- 1 1
hey, why is it printing 1 1?

Re: That facepalm moment when ...

Posted: Wed Mar 03, 2021 6:19 pm
by MrFariator
ZuccaroMateo wrote: Wed Mar 03, 2021 3:56 pm hey, why is it printing 1 1?
Tables are passed by reference in lua. As such, if you pass in a table and do some modifications on it, then those changes will persist even after the function finishes. However, in the posted code, when the reset_vector is any non-falsey value, the table the variable "vector" points to doesn't get modified. Instead, the "vector" variable is reassigned to look at a new table. This leaves the original table untouched, so the code will print 1,1 after the function is over.

The correct code for the function would be like

Code: Select all

function update_vector (vector, reset_vector)
  if reset_vector then 
    vector[1] = 0
    vector[2] = 0
  end
end

Re: That facepalm moment when ...

Posted: Wed Mar 03, 2021 11:15 pm
by darkfrei
ZuccaroMateo wrote: Wed Mar 03, 2021 3:56 pm
darkfrei wrote: Sat Feb 06, 2021 9:30 am

Code: Select all

print (vector[1]..' '..vector[2]) -- 1 1
hey, why is it printing 1 1?
It's "facepalm" topic, when you unterstand that your mind expectations do not match the Lua reality.

I've made this mistake because I can :)

Re: That facepalm moment when ...

Posted: Thu Mar 04, 2021 12:16 am
by Xii
ZuccaroMateo wrote: Wed Mar 03, 2021 3:56 pm hey, why is it printing 1 1?
Took me a moment to get it.
Within the function, the local parameter named "vector" is assigned to point to something else.
It's illustrated by renaming this variable:

Code: Select all

function update_vector (localparameter)
    localparameter = {0,0} 
end
local vector = {1,1}
update_vector (vector)
print (vector[1]..' '..vector[2]) -- 1 1
It doesn't actually touch the vector at all.
The confusion arises from both the external and parameter variables being named "vector". ^^

Re: That facepalm moment when ...

Posted: Thu Mar 04, 2021 11:20 am
by darkfrei
Xii wrote: Thu Mar 04, 2021 12:16 am
ZuccaroMateo wrote: Wed Mar 03, 2021 3:56 pm hey, why is it printing 1 1?
Took me a moment to get it.
Within the function, the local parameter named "vector" is assigned to point to something else.
It's illustrated by renaming this variable:

Code: Select all

function update_vector (localparameter)
    localparameter = {0,0} 
end
local vector = {1,1}
update_vector (vector)
print (vector[1]..' '..vector[2]) -- 1 1
It doesn't actually touch the vector at all.
The confusion arises from both the external and parameter variables being named "vector". ^^
But!

Code: Select all

function update_vector (localparameter)
    localparameter[1] = 0
    localparameter[2] = 0
end
local vector = {1,1}
update_vector (vector)
print (vector[1]..' '..vector[2]) -- 0 0