finnvoor / PlaydateKit

Create games for Playdate using Swift.
https://finnvoor.github.io/PlaydateKit/documentation/playdatekit
Creative Commons Zero v1.0 Universal
198 stars 21 forks source link

How do I use/cast `OpaquePointer` to `Sprite`? #50

Closed iain-reid closed 6 months ago

iain-reid commented 6 months ago

I'm pretty excited about this library - and the instructions worked perfectly!

However I'm running into an issue (that may be my misunderstanding of how to use PlaydateKit!)

When I use setUpdateFunction on a sprite - the closure defaults to passing me a variable called sprite (which makes perfect sense - I probably want access to that sprite to do something with it) - but the type is OpaquePointer...

Is there a way to cast this into a PlaydateKit Sprite? What is the best way to use this?

finnvoor commented 6 months ago

Hi! Currently this isn't really possible, but I have a PR #32 that fixes this.

Instead of setting an update function on a sprite, you will be able to subclass Sprite and override the update function:

class MyGameObject: Sprite {
    override func update() {
        // update code
    }
}

I was waiting on some fixes to embedded swift, and currently the draw function can't be overridden/set due to a bug in the swift compiler, but it looks like the issues blocking the update function were fixed so I'll try to merge this PR later today.

iain-reid commented 6 months ago

Looks like a good solution. Thanks for the quick response - and great work!

finnvoor commented 6 months ago

done! example usage here: https://github.com/finnvoor/PlaydateKitTemplate/blob/17a810ba7c7cb66edf0ee7bb409c508bb52377bb/Sources/PlaydateKitTemplate/Game.swift#L29-L43