Photon Pick up Item - Like in PUBG

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

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).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Photon Pick up Item - Like in PUBG

Dochder
2018-06-23 12:02:49

Hey Guys,

I'm having a problem with picking up Items. The Items in my Game are picked up like this: When the player moves over an Item, which is in on the floor, the same item is copied into the Players Inventory "Floor-Panel", when the player opens up his Inventory, a copy of the item on the floor is displayed in his inventory. When the player moves the Item from the Floor-Panel to the Bag Panel, the original item on the floor is destroyed and the copy is then the inventory. If the player wants to put the item back on the floor it's the same procedure - he opens the Inventory, drags the Item from the Bag-Panel to the Floor Panel, and as soon as this happens, the item is there twice again - On the Floor-Object and on in the inventory. As long as the player stands on the item, it is always shown twice - as soon as the player goes out of reach, the copied item in the Inventory Floor-Panel is not shown there anymore.

How can I achieve this one in Photon? My script for picking and throwing away an item already works offline - but when I try to to this in photon and give the item an Photon-View component it dissapears, whats logic, because the duplicated Item appears here two times and Photon doesn't know how to handle the same item two times.

My approach was something like this: Copy the item to the Floor-Inventory as soon as the player walks over the item (but without the photon-View component -> how can i do this?), the copied item in the floor-Inventory has now NO photon View Component. As soon as the item is moved from the Floor-Panel to the Bag-Panel, destroy the original item on the floor (which has a photon-View component) and to the Item added to the Bag Add a Photon-View.

Is it right to do it like that? I really don't know how to solve this.

Thank you so much,
Greetings, Dario

Comments

Vallar
2018-06-24 09:38:25

Let me start by saying I am still starting out with PUN. That out of the way, I believe you may want to handle the item as one and the same rather than two different items. So when the player stands on the item on the floor and drags it into the bag you just add the item to the Bag's list/array/etc... and remove it from the floor's.

So in your item setup the PhotonView needs to be set to Request. Then when the player attempts to move it to its inventory you call PhotonView.RequestOwnership(); If it can take ownership, it will, if not, then it remains there (you can do your own requests by implementing a call back). This should prevent the item from appearing twice and confusing photon. This is "network side". Client side, network doesn't care much about what you display on the GUI so you can handle that "offline" without too much hassle (though you'll need network results to display the proper stuff in GUI, just that the network won't care about controlling the GUI itself).

Sorry if this isn't helpful or anything. I just ran into the information here.

Dochder
2018-06-28 18:21:33

Hey Guys,

I found a solution already: What Vallar said is a good start, I had to put the Item on the floor, set the observe-option to "off" (so that the items position is not updated all time, but only when it's state is changed via RPC) and put the owner of the photon-view to "takeover"... Then there is another idea that came in my mind and worked perfectly for me in this case: In my prefabs I always have one and the same item two times (but in two different sub-folders in my resources folder). Both prefabs are the same (they even have the same name!), one is for "online" the other is for the client itself, let's call it "offline". The difference between both items is that the "online"-item has a Photon-View and an (I called it) "ItemStateScript", which has an RPC that enables and disables the item and is the observed component of the PhotonView, if someone took ownership of it and called the RPC to activate or deactivate it. The offline-Item doesn't have these two components - the rest is the same. When moved over the "online" item on the floor, a prefab from the resources folder is made:

copiedItem = Instantiate(Resources.Load("LocalItems/" + originalItem.name, typeof(GameObject)), originalItem.transform) as GameObject;

and then parent the "copy" to my floor-panel.

while originalItem.Name is the original "online"-item's name laying on the floor.

With ("LocalItems/" + originalItem.name) I don't have to care about names, the items only have to have the same name, the originalItem is in the "OnlineItems"-Folder of my resources while the offline-Item is in the "LocalItems"-folder. I'm so happy that the Folder-Structure is passed via string, since it would not work without that.

As soon as the the copiedItem is dragged onto my Bag-Panel, the Player takes ownership of the "online"-item, sends an RPC and deactivates it. This costs me only one message. The Player now has a copied, "offline"-Version of the item and no one else can take the "original"-item from the floor since it was deactivated for everyone. Now let's say the item was some kind of Ammo with an amount of shots left and you wear the copied-version of it, and you want to drop it on the floor, but u fired some shots already so the Ammo is not full like when u picked it up, only lets say 5 shots in the ammo left. In the moment when you move the item from the bag-panel to the floor panel, the copied item will be destroyed, the original "online"-item activated again and it's position will be set to my player. Before deleting the "offline"-item from my inventory/bag I send the informations needed (in this case the ammo left - means the stats-script which is on the "offline"-item) and pass it's values to the "online"-Item.

It's a very nice workaround which worked perfectly for me.

Dochder
2018-06-28 18:23:35

Ah, and this is also to say: You cannot just Instantiate an item which is the same like your original item, because they will have the same photon view. You are also not able to deactivate the photonView first and then clone it. It's simply not possible, pun will automatically destroy the item, because the same ID appears twice.

Back to top