How to handle a NPC properly?
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
How to handle a NPC properly?
Tomza
2018-06-17 13:13:28
Hi,
I spawn one NPC (PhotonNetwork.InstantiateSceneObject()) and one player at the same time by a game manager. When the player is killed by the NPC, he is respawned with another NPC (now two NPCs) and when the respawned player kills the second NPC, all is Ok, the body is then destroyed, but when the player kills the first NPC, the body is not destroyed with PhotonNetwork.Destroy(gameObject); Why isn't the previous NPC destroyed? Who owns the NPCs (scene obejcts) and how to control it properly?
In other words, what happens with sceneobjects when a player (client) is respwned and there is no other client, so there is only one client connected? When a player is destroyed (after have been killed), is the client disconnected? Adn when the player spawned, it is connected again?
Comments
[Deleted User]
2018-06-21 09:56:39
Hi @Tomza,
by default Scene Objects are owned and controlled by the MasterClient. But it's possible to transfer the Ownership of Scene Objects to other clients. Since you don't transfer Ownerships according to your description above, the objects will be owned by the MasterClient.
If your instantiated character dies and gets removed, the client doesn't get disconnected from the server by default.
@Christian_Simon wrote:
Hi @Tomza,
by default Scene Objects are owned and controlled by the MasterClient. But it's possible to transfer the Ownership of Scene Objects to other clients. Since you don't transfer Ownerships according to your description above, the objects will be owned by the MasterClient.
If your instantiated character dies and gets removed, the client doesn't get disconnected from the server by default.
Ok, thank you. So how to spawn, kill and control a NPC. I know how to do that in a dingle player game, so I don't need such details. I mean Photon mechanics, best practices, etc.
[Deleted User]
2018-06-21 11:00:42
In order to instantiate a scene object, you can use PhotonNetwork.InstantiateSceneObject(...);
. Make sure that you use 0 as group parameter in order to instantiate this object 'globally'. Controlling is done by the owner of the object, which is the MasterClient by default. You can handle this basically the same way you are handling a client's character. Removing an object can only be done by either the MasterClient or the Owner of the object. Please note: MasterClient and Owner can be the same client, but it doesn't have to be this scenario. In order to remove a networked object, you can use PhotonNetwork.Destroy(...);
.
@Christian_Simon wrote:
In order to instantiate a scene object, you can use
PhotonNetwork.InstantiateSceneObject(...);
. Make sure that you use 0 as group parameter in order to instantiate this object 'globally'. Controlling is done by the owner of the object, which is the MasterClient by default. You can handle this basically the same way you are handling a client's character. Removing an object can only be done by either the MasterClient or the Owner of the object. Please note: MasterClient and Owner can be the same client, but it doesn't have to be this scenario. In order to remove a networked object, you can usePhotonNetwork.Destroy(...);
.
Great! But why my two NPCs do the same - shooting, reloading and dying. When I kill one, another dies too, for example. Can I make them separate units like players? What should I use for that? photonView.isMine? Hope you understand what I mean.
What a function/moment in game is the best for NPC spawning?
[Deleted User]
2018-06-21 11:27:52
If you instantiate them you have two different objects, which you have to deal with in different ways. For now it seems that you apply everything you do to both objects which you have to avoid. It seems that this is more a game logic problem than a network logic problem.
isMine
only describes if you are the owner of the object.
@Christian_Simon wrote:
If you instantiate them you have two different objects, which you have to deal with in different ways. For now it seems that you apply everything you do to both objects which you have to avoid. It seems that this is more a game logic problem than a network logic problem.
isMine
only describes if you are the owner of the object.
If it is not a network problem, Ok. So it is like in a single player game. When to spawn NPC ? The best function/moment for that?
[Deleted User]
2018-06-21 13:49:23
You basically have two options: the first is to place objects already while editing the scene in the editor, the other is to instantiate them at runtime. Both options are possible in single- and multiplayer games. Since you want to dynamically instantiate objects at runtime, you are fine with the second option.
For your case you can use the OnJoinedRoom
callback, to create your character and the first NPC. This way both objects will be created as soon as the client has joined the room. You can use PhotonNetwork.Instantiate
for your character and PhotonNetwork.InstantiateSceneObject
for the NPC.
For each following NPC you can use PhotonNetwork.InstantiateSceneObject
, too.
When you want to remove your character or a certain NPC, you can use PhotonNetwork.Destroy
as long as you are the owner of the object or the MasterClient in the room.
Ok, thank you for your precious help. All clear!
Back to top