hmans / miniplex

A 👩‍💻 developer-friendly entity management system for 🕹 games and similarly demanding applications, based on 🛠 ECS architecture.
MIT License
827 stars 38 forks source link

Accessing Entity Defined in Player Function from App.jsx File #292

Closed zihuaihuai closed 1 year ago

zihuaihuai commented 1 year ago

Description:

I am having an issue accessing an entity that I've defined within a Player function component from my App.jsx file. The Entity Component System (ECS) I am using is integrated with React and here's the code for my Player function component:

`export default function Player(props) { const ECS = props.ECS const entity1 = ECS.world.add({name: "test"})

return (    
    <ECS.Entity entity={entity1}>
      <ECS.Component name="position" data={{x:1, y:1, z:0}} />
      <ECS.Component name="color" data="red" />
      <ECS.Component name="poisoned" data={true} />
      <ECS.Component name="velocity" data={{x:1, y:1, z:0}} />
      <ECS.Component name="three"/>
        <mesh ref={ref}>
          <cylinderGeometry args={[1, 1, 1]} position={{x:1,y:1,z:1}} />
          <meshStandardMaterial
            wireframe={props.wireframe}
            color={ "hotpink" }
          />
        </mesh>
      <ECS.Component name="content" data="hello world" />
        {console.log("hello world")}
    </ECS.Entity>
  );
}

`

And here's how I am trying to use it in my App.jsx file: `function App() { const world = new World() const ECS = createReactAPI(world)

useEffect(()=>{ console.log(world) }, [world])

return ( <>

); } ` I want to gain access to the entity defined in the Player component from the App component. I've attempted to use the ECS.useCurrentEntity method, but it returned null. I've also tried using useRef, but to no avail. I am seeking a solution or guidance on the best approach to resolve this issue. Thanks in advance!
hmans commented 1 year ago

May I kindly redirect you to the Discussions tab and ask you to repost your question there, ideally with better formatting? It's hard to see if the code you pasted is missing bits, or if GitHub is omitting some of your JSX, when it's not in a code block tag. Thanks!