Question: what is an underscore?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Question: what is an underscore?

Post by togFox »

This badly rotated set of tiles (they should be aligned to x/y axis):

Image

Was caused by this line:

Code: Select all

love.graphics.draw (imgTileimage[21],graphicsx,graphicsy,_, tilesize / 32, tilesize / 32)

When I replaced the underscore (rotation) with a zero (0) then everything was corrected - as expected.

So, why was LOVE interpreting the underscore that way when the native image is nice and neat and square?

#curious
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Question: what is an underscore?

Post by MrFariator »

This has more to do with the arguments love.draw accepts, than what an underscore is. As per löve wiki, the third argument is rotation, fed by radians. If the third argument is any non-zero number value, the drawn image will be rotated.

In lua, it's a convention to use a single underscore as a shorthand for an "unneeded" value. For example, in a pairs loop you may not be interested in the 'key'.

Code: Select all

-- code assumes that myTable contains other tables, and we're doing something with those
-- as such, the "key" that pairs() usually returns is not needed, hence the _
for _, value in pairs (myTable) do
  value.someValue = value.someValue + 1
end
As such, an underscore is basically a normal variable - that us lua programmers tend to use for throwaway values returned by functions or other things. The underscore will still (probably) have some value assigned to it, but you're meant to reason that it's not going to be used later in the same code block, if you follow the convention.

In your case, it seems the underscore in your code happens to contain a numeric non-zero value, which happens to rotate the image as shown. This could be because you're using it within a loop in similar fashion to the example code I wrote above, or maybe you've accidentally created a global variable. Check where you might have assigned a value to an underscore, and see if that's causing the behavior you're experiencing.
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Question: what is an underscore?

Post by togFox »

Perhaps I did your code (everyone does do pairs/ipairs frequently) followed by my code:

Code: Select all

for _, value in pairs (myTable) do
  value.someValue = value.someValue + 1
end

love.graphics.draw (imgTileimage[21],graphicsx,graphicsy,_, tilesize / 32, tilesize / 32)

Meaning the underscore is the last 'key' value used in some loop which the DRAW function took as a rotation value?
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Question: what is an underscore?

Post by grump »

togFox wrote: Mon Aug 23, 2021 10:15 am Meaning the underscore is the last 'key' value used in some loop which the DRAW function took as a rotation value?
No, it doesn't work like that. You have a global or upvalue with the name _ in your code.

This is one reason why global variables are bad. And you shouldn't use random identifiers that you hope are nil if you really mean nil. Using _ in your draw line is super weird and bad style. Use 0 if you mean 0, or use nil if you want the default value.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Question: what is an underscore?

Post by pgimeno »

> No, it doesn't work like that.

More specifically, variables used as the control variables of a for loop are always local to that loop, therefore they are destroyed when the loop finishes.

It may well be that some library is using `_` without declaring it local.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Question: what is an underscore?

Post by milon »

togFox wrote: Mon Aug 23, 2021 9:11 am...
When I replaced the underscore (rotation) with a zero (0) then everything was corrected - as expected.

So, why was LOVE interpreting the underscore that way when the native image is nice and neat and square?
...
I believe you can also use 'nil' instead of 0 and have it work as intended, FWIW. At least I've done that in (minor!) projects past, and it worked fine.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Nabeel and 36 guests