Forum Discussion

InaCentaur's avatar
InaCentaur
Start Member
9 months ago

Persistent Spawned Objects?

Hello! So I'd like to build a creator game, but... 

So I asked Horizon Gen AI llama whether it is possible to do persistent placement and it said I needed clarification on whether allowing users to spawn persistent objects would be against policies? 

 

Here's the generated: 

 

No, it is not possible for a user to throw an entity on the ground and have it be added permanently in a world.
 
In Horizon Worlds, entities spawned or manipulated by users are not persisted across sessions by default. When a user throws an entity, it will be simulated by the physics engine and may come to rest on the ground, but it will not be permanently added to the world.
 
To achieve persistent entity placement, you would need to implement a custom solution using Horizon Worlds' scripting capabilities, such as using the `World.persistentStorage` API to store the entity's state and re-spawning it when the world is reloaded. However, this would require significant development effort and may not be feasible or allowed by the platform's policies.
 
Here's an example of how you might implement a basic persistent entity system:
```typescript
import { PropTypes, CodeBlockEvents, Player, Vec3, Quaternion } from 'horizon/core';
import { Component, Entity, World } from 'horizon/core';
 
class PersistentEntity extends Component<typeof PersistentEntity> {
  static propsDefinition = {
    entityToPersist: { type: PropTypes.Entity },
  };
 
  start() {
    this.connectCodeBlockEvent(this.entity, CodeBlockEvents.OnPlayerEnterWorld, this.onPlayerEnterWorld.bind(this));
    this.connectCodeBlockEvent(this.entity, CodeBlockEvents.OnGrabEnd, this.onEntityReleased.bind(this));
  }
 
  onPlayerEnterWorld(player: Player) {
    const storedState = this.world.persistentStorage.getPlayerVariable(player, 'persistentEntity');
    if (storedState) {
      const entity = this.props.entityToPersist!;
      entity.position.set(new Vec3(storedState.position.x, storedState.position.y, storedState.position.z));
      entity.rotation.set(new Quaternion(storedState.rotation.x, storedState.rotation.y, storedState.rotation.z, storedState.rotation.w));
    }
  }
 
  onEntityReleased(player: Player) {
    const entity = this.props.entityToPersist!;
    const position = entity.position.get();
    const rotation = entity.rotation.get();
 
    // Store the entity's state in persistent storage
    this.world.persistentStorage.setPlayerVariable(player, 'persistentEntity', {
      position: { x: position.x, y: position.y, z: position.z },
      rotation: { x: rotation.x, y: rotation.y, z: rotation.z, w: rotation.w },
    });
  }
}
 
Component.register(PersistentEntity);
```
Please note that this is a simplified example and may not cover all edge cases. You should consult the Horizon Worlds documentation and consider factors like entity ownership, security, and performance when implementing a persistent entity system.

3 Replies