That facepalm moment when ...
Posted: Sat Feb 06, 2021 3:00 am
... I spent 2 hours trying to nut out my failed vector math only to discover I didn't convert to radians.
You?
You?
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
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?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
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
hey, why is it printing 1 1?darkfrei wrote: ↑Sat Feb 06, 2021 9:30 amCode: 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
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.
Code: Select all
function update_vector (vector, reset_vector)
if reset_vector then
vector[1] = 0
vector[2] = 0
end
end
It's "facepalm" topic, when you unterstand that your mind expectations do not match the Lua reality.
Took me a moment to get it.
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
But!Xii wrote: ↑Thu Mar 04, 2021 12:16 amTook 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:
It doesn't actually touch the vector at all.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
The confusion arises from both the external and parameter variables being named "vector".
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