That facepalm moment when ...
That facepalm moment when ...
... I spent 2 hours trying to nut out my failed vector math only to discover I didn't convert to radians.
You?
You?
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Re: That facepalm moment when ...
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 ...
Haha! Been there, done that - both of those, actually!
Here's most recent facepalm:
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
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Re: That facepalm moment when ...
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
Re: That facepalm moment when ...
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.
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.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
-
- Prole
- Posts: 17
- Joined: Tue Jul 14, 2020 11:07 pm
Re: That facepalm moment when ...
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
-
- Party member
- Posts: 559
- Joined: Wed Oct 05, 2016 11:53 am
Re: That facepalm moment when ...
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 ...
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 ...
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
The confusion arises from both the external and parameter variables being named "vector".
Re: That facepalm moment when ...
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
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests