From a4508fb05305ebbfc7fdb2e3547a990502a6652f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Fri, 2 Jan 2026 12:33:07 +0100 Subject: [PATCH] Test Component.fromScene --- packages/example/src/TestUi2Component.tsx | 3 +-- .../react-godot-renderer/src/Component.ts | 8 +++---- .../react-godot-renderer/src/Reconciler.ts | 24 ++++++++++++------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/example/src/TestUi2Component.tsx b/packages/example/src/TestUi2Component.tsx index 6728c65..1e4dd59 100644 --- a/packages/example/src/TestUi2Component.tsx +++ b/packages/example/src/TestUi2Component.tsx @@ -1,14 +1,13 @@ import Godot from "godot" import { useState } from "react" import { Component } from "react-godot-renderer" -import TestUi1 from "./TestUi1" const HFlowContainer = Component.make(Godot.HFlowContainer) const VFlowContainer = Component.make(Godot.VFlowContainer) const Label = Component.make(Godot.Label) const CheckBox = Component.make(Godot.CheckBox) -const TestUi1FC = Component.make(TestUi1) +const TestUi1FC = Component.fromScene("res://src/TestUi1.tscn") export function TestUi2Component() { const [show, setShow] = useState(false) diff --git a/packages/react-godot-renderer/src/Component.ts b/packages/react-godot-renderer/src/Component.ts index 200e527..7389388 100644 --- a/packages/react-godot-renderer/src/Component.ts +++ b/packages/react-godot-renderer/src/Component.ts @@ -31,7 +31,7 @@ export const make = >( class_: new (...args: any[]) => T ): Component => Object.setPrototypeOf( Object.assign( - (props: Props) => React.createElement("element", { ...props, class: class_ }), + (props: Props) => React.createElement("node", { ...props, class: class_ }), { displayName: class_.name }, ), Prototype, @@ -44,11 +44,11 @@ export declare namespace fromScene { } export const fromScene = ( - name: A + path: A ): Component ? T : never> => Object.setPrototypeOf( Object.assign( - (props: Props) => React.createElement("element", { ...props }), - { displayName: name }, + (props: Props) => React.createElement("scene", { ...props, path }), + { displayName: path }, ), Prototype, ) diff --git a/packages/react-godot-renderer/src/Reconciler.ts b/packages/react-godot-renderer/src/Reconciler.ts index c96312e..0cfaf09 100644 --- a/packages/react-godot-renderer/src/Reconciler.ts +++ b/packages/react-godot-renderer/src/Reconciler.ts @@ -1,6 +1,6 @@ -import { ClassDB, Node, type NodePathMap } from "godot" +import { Node, type NodePathMap, type PackedScene, ResourceLoader } from "godot" import ReactReconciler, { type HostConfig } from "react-reconciler" -import { camelToSnake, hasProperty, snakeToPascal } from "./utils.js" +import { hasProperty } from "./utils.js" const DefaultEventPriority = 32 @@ -29,16 +29,24 @@ export const make = () => { createInstance(type, props) { let instance: Node - if (type === "element") { + if (type === "node") { if (!hasProperty(props, "class")) - throw new Error("Property 'class' required when using the 'element' intrinsic type") + throw new Error("Property 'class' required when using the 'node' intrinsic type") instance = new (props.class as any)() } + else if (type === "scene") { + if (!hasProperty(props, "path")) + throw new Error("Property 'path' required when using the 'scene' intrinsic type") + if (typeof props.path !== "string") + throw new Error("Property 'path' is not a string") + instance = (ResourceLoader.load(props.path) as PackedScene>).instantiate() + } else { - const className = snakeToPascal(camelToSnake(type)) - if (!ClassDB.class_exists(className)) - throw new Error(`Class is invalid: '${className}' (declared as '${type}') is not a valid engine or GDExtension class`) - instance = ClassDB.instantiate(className) + // const className = snakeToPascal(camelToSnake(type)) + // if (!ClassDB.class_exists(className)) + // throw new Error(`Class is invalid: '${className}' (declared as '${type}') is not a valid engine or GDExtension class`) + // instance = ClassDB.instantiate(className) + throw new Error(`Unsupported JSX type: '${ type }'`) } if (!(instance instanceof Node))