How do I get Accelerometer x,y,z values? Compiler says it's a single number

I’m clearly missing something about how the API works.

When I use print(playdate.readAccelerometer()), the console shows all three values, x, y and x, as a string with tabs between each (using some internal toString() function? I’m new to lua).

But when I assign playdate.readAccelerometer() to a new variable and print that, it only returns the x value.

readAccelerometer

I only even checked this because every time I try to get playdate.readAccelerometer().x the game crashes and complains that readAccelerometer() is a number, not a table.

What am I missing? The Accelerometer values are some kind of a table/array-esque thing… until I check its values, when it becomes simply a number equal to the x value.

Is there some Lua syntax I’m unfamiliar with here? The PlayDate docs refer to this as a “touple” which according to the Internet is a thing that doesn’t exist in Lua but I assumed was just a concept? I’m lost and could use a 101 lesson on this.

Thanks!

1 Like

Have you tried:

{x,y,z} = playdate.readAccelerometer()

Untested

There’s also an accelerometer example in the Single File Examples folder.

1 Like

Ha, yes! I literally just tried something similar:

local tiltX, tiltY, tiltZ = playdate.readAccelerometer()

And it worked. This syntax makes absolutely no sense to me but here we are!

Thanks for the reminder about the existence of examples, too! I’m working on Windows (running the emulator in VMWare, shhh!), so I missed them because they’re all in the macOS SDK folder.

2 Likes

It’s a Lua thing! You’ll grow to love it.

If the method returns multiple parts (tuple) then you can assign them all at once. Have a read of (some of) the Lua manual.

1 Like

My problem is a lack of familiarity with the terminology, which I wasn’t expecting. I spent literally an hour pouring though Lua documentation about this before posting here, but I just didn’t know what to look for.

But, I’m sure I’ll get used to it!

1 Like

I think the documentation call them tuples, but I don’t think they are actually tuples.

You just need to know that in lua a function can return multiple values which is extremely handy.

function getPosition()
  return x, y
end

You can also assign multiple values at once
x, y = 10, 20

and so you can catch all return values in one go
x, y = getPosition()

or even passing them as an multiple argument in a function call
image:draw( getPosition() )

but only for the last arguments
print( getPosition(), "foobar" ) -- will print x and "foobar", y will be skiped
print( "foobar", getPosition() ) -- will print "foobar", x and y

If you want to get all the return values in a single object you can do
object = { getPosition() }
or
object = table.pack( getPosition() ) -- get an extra 'n' member for the number of fields

and you can do the opposite and call table.unpack() to get a sequence of return values

6 Likes

Sorry for the necroposting, I of course found the problem immediately after posting.

If anybody passes around here with this question in the future, if you get weird numbers from readAccelerometer, remember that you should be calling it with readAccelerometer(). It's not going to give you an error if you just call it without parentheses.

1 Like

Can the api reference be updated to not use the word "tuples"? It's just very misleading. Those that know lua know that it doesn't do tuples, and those that don't know what a tuple is will just be confused.

2 Likes

Agreed. Even better would be an example! @panic_staff

Sorry, that's entirely my fault. What would be a better way to describe Lua's multiple return values?

I think you've got it :laughing:

1 Like

Hm. It's a little unwieldy in the docs. How about "list"?

1 Like

Why not a usage example to make things clear?

I think the problem is that we mention "tuple" many times in the documentation, when some function returns or expects multiple values.

Yes, I would expect "tuple" to refer to a table with a fixed size and "multiple values" to refer to what you get when you unpack a tuple.

“List” sounds a bit technical to me; I’d probably go googling what exactly a Lua list is. In fact I just did so, and the top results aren’t relevant to return values.

Here’s how Matt’s suggestion would sound in practice:

Returns the size of the given table as multipke values (arrayCount, hashCount).

Returns multiple values (width, height) about the Playdate display size

It could get a bit grammatically hairy, but it doesn’t seem too bad…

Returns multiple values (_x, y, width, height_) representing the smallest possible rect ..

yeah, okay, I guess it's not that bad

That is probably the best thing to do because tuple implies some sort of data structure in Lua which doesn't technically exist. However.. this sort of "multiple return values" in Lua is often referred to as a tuple due to writing "multiple return values" being somewhat unwieldy and perhaps not logically complete, since functionality like unpack and pack exists on these sort of returns.

List to me sounds like table, where indexes matter, so I think that is off the table (lol).

I think a simple primer on common return types to expect within the SDK could be helpful to those who are new to programming.

Interestingly, Roblox has taken quite a different approach from what have agreed upon here: Tuple and appears to try an teach some of the language that might be involved.

1 Like

Yeah but I'd rather be able to do

X = function_that_returns_multiple_values()

and X be the table.

I just tested this locally in Lua and you can do the following:

local x = { function_that_returns_multiple_values() }
print(#x) -- number of values

This is the opposite operation as the following:

local x, y, z = table.unpack(function_that_returns_table_of_values())

Edit:

Just realized that @Nic already wrote this :grimacing:

1 Like