migueldeicaza / SwiftGodot

New Godot bindings for Swift
https://migueldeicaza.github.io/SwiftGodotDocs/tutorials/swiftgodot-tutorials/
MIT License
1.06k stars 65 forks source link

Proposal: Updated BindNode with safety and convenience lookup #439

Closed Nemesisprime closed 4 months ago

Nemesisprime commented 5 months ago

Just a few improvements to make @BindNode a little bit better to work with.

Improved Lookup

Given a Scene Node named "MovementComponent":

    @BindNode var MovementComponent: Node? // existing
    @BindNode var movementComponent: Node? // inferred
    @BindNode("MovementComponent") var customLabel: Node? // custom

All become valid ways to look up via BindNode.

Safe Lookup

Previously, @BindNode didn't include the ability to look-up via optional, but this now forces them to be defined as optionals. This prevents the chance that a misnamed or missing Node is going to cause a crash instead puts it on the programmer to handle the missing node.

This has a backwards incompatible change to more-or-less force safety on BindNode which is primarily the reason why I marked it a proposal in the title here. Feel feel to object but thought I'd take the chance given the early-ish state of the project and how annoying that was to stumble on 🙂

ridigilis commented 5 months ago

Suggestion: Could the argument label be omitted in the custom case?

@BindNode("MovementComponent") var customLabel: Node?

Normally I favor explicitness, but I think this reads nicely.

Nemesisprime commented 5 months ago

Oooh I agree, that looks really nice. Applied the change to the init and docs!

Nemesisprime commented 4 months ago

Was speaking in Slack and discovered @SceneTree (https://github.com/migueldeicaza/SwiftGodot/pull/297) which more-or-less replaces bindNode with the optional safety behavior (and is tested!). Magic mapping TBD.