That facepalm moment when ...

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

That facepalm moment when ...

Post 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?
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: That facepalm moment when ...

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: That facepalm moment when ...

Post 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
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: That facepalm moment when ...

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: That facepalm moment when ...

Post 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.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
ZuccaroMateo
Prole
Posts: 17
Joined: Tue Jul 14, 2020 11:07 pm

Re: That facepalm moment when ...

Post 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?
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: That facepalm moment when ...

Post 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
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: That facepalm moment when ...

Post 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 :)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: That facepalm moment when ...

Post 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". ^^
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: That facepalm moment when ...

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests