-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scorbeoards #82
Scorbeoards #82
Conversation
This PR serves as a PSA that this feature is being worked on. I decided to create a branch on the main repo so it's easier to collaborate if needed |
I got stuck calling the binding function with this error:
From the example, I called it like this: discard playdate.scoreboards.getPersonalBest(
"hills",
proc(score: PDScore, errorMessage: string) =
discard
) |
You may have to use playdate-nim/src/playdate/graphics.nim Lines 216 to 222 in 1a752d3
|
That definetly worked. I now have the issue that a call to addScore works on sim but crashes device, which makes it hard to debug. Even when I replace the entire content of invokeAddScoreCallback by Edit: crash does not happen on 2.5.0 firmware; note that this crash is from fw 2.6.0-beta4
|
@Nycto thanks for the review. I cleaned it up a bunch. Enjoy ;-p |
src/playdate/scoreboards.nim
Outdated
template invokeCallback(callbackSeqs, value, errorMessage, freeValue, builder, emptyValue: untyped) = | ||
let callback = callbackSeqs.pop() | ||
if value == nil and errorMessage == nil: | ||
callback(emptyValue, "Playdate-nim: No value provided for callback") | ||
return | ||
|
||
if value == nil: | ||
callback(emptyValue, $errorMessage) | ||
return | ||
|
||
try: | ||
let nimObj = builder(value) | ||
callback(nimObj, $errorMessage) | ||
finally: | ||
freeValue(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
callback
shouldn't be a proc in this situation -- It makes your callsites more complicated than they need to be (and also incurs an unnecessary memory allocation because Nim has to create a lambda). You can also get rid of the emptyValue
parameter by using Nim's default
proc.
Here is what I mean:
template invokeCallback(callbackSeqs, value, errorMessage, freeValue, builder: untyped) =
let callback = callbackSeqs.pop()
if value == nil and errorMessage == nil:
callback(default(typeof(builder)), "Playdate-nim: No value provided for callback")
return
if value == nil:
callback(default(typeof(builder)), $errorMessage)
return
try:
let nimObj = builder
callback(nimObj, $errorMessage)
finally:
freeValue(value)
Then your callsites will look like this:
proc invokePersonalBestCallback(score: PDScorePtr, errorMessage: ConstChar) {.cdecl, raises: [].} =
invokeCallback(privatePersonalBestCallbacks, score, errorMessage, playdate.scoreboards.freeScore):
scoreBuilder(score)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't quite figure this out, maybe missing a piece of the puzzle here.
"callback shouldn't be a proc in this situation"
What should they be then? Feel free to create a PR to this branch with the solution if that's easier, or directly commit to the branch if you are sure of it.
@Nycto Thanks again fr the feedback. I couldn't get this suggestion to compile. Can you fix it yourself? |
Alright -- I published a version that uses I also had another realization while making this change. For the callbacks, what we actually have here is an type
# ...
PDResultKind* = enum PDResultSuccess, PDResultError
PDResult*[T] = object
case kind*: PDResultKind
of PDResultSuccess: value: T
of PDResultError: message: string
PersonalBestCallback* = proc(result: PDResult[PDScore]) {.raises: [].}
AddScoreCallback* = proc(result: PDResult[PDScore]) {.raises: [].}
BoardsListCallback* = proc(result: PDResult[PDBoardsList]) {.raises: [].}
ScoresCallback* = proc(result: PDResult[PDScoresList]) {.raises: [].} |
I decided to just make the change and push so you can see what it looks like in context. If you prefer the dual argument version let me know and I'll pop this commit off the branch. |
Looks good to me from my phone, will have another look tomorrow. Anything you want to see changed? Otherwise please approve it and I'll merge it |
Done ✔️ |
https://help.play.date/catalog-developer/scoreboard-api/#pdscore