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.
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.
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.
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.
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
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.
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.
“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…
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: Tuples | Documentation - Roblox Creator Hub and appears to try an teach some of the language that might be involved.