Photon Bolt Destroy game objects created by other players

2 minute read

Introduction

Photon Bolt is a Unity solution that makes it easy to create online games with Unity.
This time I’m in trouble ** I’m going to write a method for destroying game objects created by others ~~ for myself ~~.

Thing you want to do

I want to Destroy a game object (entity) created by another person.

The problem you are having

Doing BoltNetwork.Destroy () in Photon Bolt destroys game objects online. However, you cannot destroy game objects created by other players.

When I BoltNetwork.Destroy () a game object created by another person, the following is displayed and it is not destroyed normally.

Only the owner can destroy an entity, ignoring call to Destroy().

Overview of ownership

See Entity Ownership (https://doc.photonengine.com/en-us/bolt/current/in-depth/entity-ownership).

Ownership is granted only to the person who created the entity with BoltNetwork.Instantiate () and cannot be transferred.

BoltEntity.isOwner on the owner’s device returns true.
This time we will use this to destroy someone else’s game object.

I will actually assemble

1. Create an event

Events can be used to get some or all of the people in a room to do the same thing.

  1. Open Bolt Assets with ʻAssets on the Bolt` tab
  2. Right-click on the window and click New Event
  3. Change the Event name to DestroyRequest
  4. Create a new property from New Property and set its type to ʻEntity`
  5. Name the new property ʻentity`
  6. Compile with Compile Assembly from the Bolt tab

I have created an event to send a request to destroy a GameObject and a Bolt Entity. Call this event when destroying an entity.

** Do the Compile Assembly every time you make a change on Bolt. ** **

2. Write the process to receive the event

Here, we will add the function to receive a request to destroy the game object.

  1. Create a new script and rename it to NetworkCallbacks
  2. Add ʻusing Bolt;` above class
  3. Change MonoBehaviour to GlobalEventListener
  4. Write the following code in the class and execute Compile Assembly
    public override void OnEvent(DestroyRequest evnt)
    {
        //Check if you are the owner of the sent entity, and if so, Destroy
        if (evnt.entity.IsOwner)
            BoltNetwork.Destroy(evnt.entity.gameObject);
    }
3. Write the process to send the event

_2. Write a process to receive an event Create a function to send to the receptionist created in _.

  1. Overwrite the part that was previously BoltNetwork.Destroy () with the following
    //The entity of the game object you want to destroy
    BoltEntity ent;
 
   //In the case of single play, destroy it as it is without sending a request
    if (BoltNetwork.IsSinglePlayer)
        Destroy(gameObject);
    else
    {
        //Create DestroyRequest, set entity to ent and then send
        var request = DestroyRequest.Create();
        request.entity = ent;
        request.Send();
    }  
  • Only BoltEntity can be sent. GameObject cannot be sent as it is, so let’s send it after GetComponent <BoltEntity> ().
  1. Run Compile Assembly

important point

Since we are using the network, there will naturally be a lag in the process from “destroy request” → “destroy on the network” → “destroy on the client”.

If you send a “destroy request” again in the meantime, you will get an error as a non-existent entity.

Before sending a “destroy request”, you need to take measures such as disabling the collider of the object and ignoring the new request in the request.

If you make a mistake

If you make a mistake in this article, please let us know in the comments: pray: