Add Component.fromClassUnsafe
Some checks failed
Lint / lint (push) Failing after 9s

This commit is contained in:
Julien Valverdé
2026-01-02 19:18:44 +01:00
parent a97afa5dd9
commit dc2f4d81d6
2 changed files with 16 additions and 9 deletions

View File

@@ -37,6 +37,16 @@ export const fromClass = <T extends Godot.Node<Godot.NodePathMap>>(
Prototype, Prototype,
) )
export const fromSceneUnsafe = <T extends Godot.Node<Godot.NodePathMap> = Godot.Node<Godot.NodePathMap>>(
path: string
): Component<T> => Object.setPrototypeOf(
Object.assign(
(props: Props<T>) => React.createElement("scene", { ...props, path }),
{ displayName: path },
),
Prototype,
)
export declare namespace fromScene { export declare namespace fromScene {
export type SceneNames = { export type SceneNames = {
[K in keyof Godot.ResourceTypes]: Godot.ResourceTypes[K] extends Godot.PackedScene ? K : never [K in keyof Godot.ResourceTypes]: Godot.ResourceTypes[K] extends Godot.PackedScene ? K : never
@@ -45,13 +55,7 @@ export declare namespace fromScene {
export const fromScene = <A extends fromScene.SceneNames>( export const fromScene = <A extends fromScene.SceneNames>(
path: A path: A
): Component<Godot.ResourceTypes[A] extends Godot.PackedScene<infer T> ? T : never> => Object.setPrototypeOf( ): Component<Godot.ResourceTypes[A] extends Godot.PackedScene<infer T> ? T : never> => fromSceneUnsafe(path)
Object.assign(
(props: Props<Godot.ResourceTypes[A]>) => React.createElement("scene", { ...props, path }),
{ displayName: path },
),
Prototype,
)
export declare namespace useSignal { export declare namespace useSignal {
export type SignalNames<T extends Godot.Node<Godot.NodePathMap>> = { export type SignalNames<T extends Godot.Node<Godot.NodePathMap>> = {

View File

@@ -1,4 +1,4 @@
import { Node, type NodePathMap, type PackedScene, ResourceLoader } from "godot" import { Node, type NodePathMap, PackedScene, ResourceLoader } from "godot"
import ReactReconciler, { type HostConfig } from "react-reconciler" import ReactReconciler, { type HostConfig } from "react-reconciler"
import { hasProperty } from "./utils.js" import { hasProperty } from "./utils.js"
@@ -39,7 +39,10 @@ export const make = () => {
throw new Error("Property 'path' required when using the 'scene' intrinsic type") throw new Error("Property 'path' required when using the 'scene' intrinsic type")
if (typeof props.path !== "string") if (typeof props.path !== "string")
throw new Error("Property 'path' is not a string") throw new Error("Property 'path' is not a string")
instance = (ResourceLoader.load(props.path) as PackedScene<Node<NodePathMap>>).instantiate() const res = ResourceLoader.load(props.path)
if (!(res instanceof PackedScene))
throw new Error(`Resource at 'path' is not a PackedScene`)
instance = res.instantiate()
} }
else { else {
// const className = snakeToPascal(camelToSnake(type)) // const className = snakeToPascal(camelToSnake(type))