Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

c# - In PUN2 the rpc is executed in other entities but not in mine

Im using Pun2 in Unity 2017 and Im still learning how Pun2 works. I think I have understood how to call RPC functions but Im a bit confused on some functions I made as it looks like not all work the same. I have a function that will display in screen who killed who in the game but while this works (text is correctly shown), its not executed by the one who received the shot ( is the same one that calls the RPC function) and RPCtarget is set to "All" that as I have read in the docs is meant to be run instantly in the client that calls it and delivered to others to do the same, so in my case the player who has been shot is not displaying the message but the entity that corresponds to the other player in my computer, so if I add a check to say only if MyPhotonview.IsMine run it then nobody runs it and text in my screen is not displayed when i get shot but it gets displayed in other machines. I hope I have explained well what trouble I face, thanks a lot for any clue beforehand =)

EDIT: The actual code is very simple, this is the RPC function, its called when a bullet hits the player in the onTriggerEnter function, in that funcion all is executed correctly because points are added bullet is destroyed etc but the rpc function is called on everyone else not in the entity that received the bullet

[PunRPC]
    void infokillScreen(string infostr)
    {      
            txtinfokill.text = infostr;
        
    }

This code is used when the player is hit by a bullet object (I use slow projectiles), I have addapted it to be better read here, only trouble I can imagine is calling 2 rpc in same frame can make trouble or something, the points are added correctly and the message is displayed correctly but as said its other entity that displays it and not my client who was shot and should be the one doing that as its the one that calls it. Only part that makes me think could be done the other way is the for loop that checks for the player that spawned the bullet but I couldnt find other way to find it, if you can suggest other better method that would be great =)

The other version of the RPC (that will display the latest 3 kills) is this one, I update this just in case it helps you see whats wrong, I was testing with the other version and it was ok as it displays correctly the latest kill but apparently its not managing the array, but as far as i understand the rpc area is not a separate space so if a variable is changed there it remains changed in the rest of the code, right? because from the test Im having it looks like the array is not keeping its values but other variables in rpc are handled correctly like points.

question from:https://stackoverflow.com/questions/66058584/in-pun2-the-rpc-is-executed-in-other-entities-but-not-in-mine

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...