Skip to main content
InaCentaur
Inspiring
May 5, 2025
Question

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

SeeingBlue
Honored Contributor
May 5, 2025

Is this related to this question also?
https://communityforums.atmeta.com/t5/Creator-Questions-Discussions/Allow-users-to-rotate-scale-and-otherwise-transform-entities-in/m-p/1318634/highlight/true#M1485

You cannot have world persistent like you're thinking. The only persistence we have is per player only.

Upcoming Events : Performance Pro - UI Unleashed - Building Responsive & Performant Interfaces Thursday June 19th 5:00 PM PT - Register June Programming Calendar
InaCentaur
Inspiring
May 5, 2025

Can you explain more by "only persistence" 

When a player throws an object on the floor, does this mean no one else sees it except the local player? 

What if a player throws an object and hits another player? Do they not see the object hitting them? 

PigeonNo12
Honored Contributor
May 6, 2025

The creator manual has excellent explanation of persistence data: https://developers.meta.com/horizon-worlds/learn/documentation/mhcp-program/community-tutorials/creator-manual#persistence

For your use case, it's not possible to persist the state of a world beyond the current instance, and each instance is independent from each other. If you want to preserve the transformation of an object, this can only be done on a per-player basis: using persistent storage to save on the player profile the position, rotation, scale, and other properties, in such way that when the player returns to the world you can read those variables to reconstruct what the player created the last time they were in the world.

Upcoming Events : Build with a Mentor: Custom UI Thursday 3/20 at 5:00 PM PT Register March calendar of all Mentor Workshops and Office Hours