Updating Score over Photon Network

So I have been running around in circles for over a week now and I can't get the score change to work.

And with "work" I mean initiate the score change when it happens... not on a player turn change.

At around 20 seconds into the below clip the yellow disc hits the blue disc.


Now when it stops moving, the "15" written in red (P2 Score) should immediately flip to "10"...

But it only changes, the moment the other player turn is current and the disc moves

I have tried so many different ways and means, transferred ownership , used photnView .IsMine which didn't work either.


So I could use another pair of eyes on this, I have made a pastebin with snippets of the code to give you an idea as to the flow (but again the code functions,it's just that it doesn't update immediately)

https://pastebin.com/keqjgi2P

Thanks in advance for your time and effort!

Comments

  • This might be a task for Custom Player Properties. In it, you can store the highscore independent from a players move / turn. One of the clients needs to rule that points are awarded and then updates the property for a player.

    You could also use Room Properties (and use keys "scorePlayer1" or shorter: "sp1" for example).

    Last but not least, there are Utility Scripts in the PUN package and one is the ScoreExtensions. So you can use AddScore and GetScore right on the Player class.

  • Thanks Tobias!

    The scoring is not a highScore, but the scoring of the game which can and probably will change at every turn.Basically the game loop is that the first person who gets to a 100 points wins.


    It's strange to me that everything runs properly on the first turn and score gets updated across the network, but once player 1 affects the disc of player 2 (and vice versa ) it doesn't work anymore ... so I really hope I can fix this with customplayer properties, as it is a maddening thing I have been struggling with for more than a week now :)

  • Custom properties don't work either.

    It seems like if there are 2 scenes ... and in scene 1 player 1 gets control over player 1 discs and in scene 2 the same goes for Player 2.

    And that they each control their own score and won't allow it to be set remotely.

    because no matter what I do ...photon won't allow me to set values on the other players disc and vice versa...why is that?

    It is really maddening, because this is teh final thing I have to do on teh Photon side and it just won't effin budge

  • Custom Properties don't mind scenes.

    Photon doesn't allow everyone to set everyone's score because this is hard to control when done concurrently. Just have each player set the own score and you're good. I don't get the problem, really. You synced the move / shot and why shouldn't the shooting player also set the score?

  • My thoughts exactly ... yet it has been two weeks now and I still haven't figured out what is going wrong.

    What I have done is that with each turn, I transfer ownership of all the discs to the player who's turn is up.

    And then use an "IsMine" IF check to control who sets the score.

    But no matter what I do, it won't budge... the best result I get is when I use an if check based on who's the masterclient (or not), but then I get a delay in score (which makes sense ) and the score of opposing discs gets set when the opposing player has its turn.

    This week I am swamped with stupid workstuffs , but in my next try I am going to do an if check based on explicit ownerid...


    Who knows, maybe after a few days not looking at the code , I see (probably my) mistake in the code

  • Tobias
    Tobias admin
    edited September 2022

    I wrote a minimal test: For each PhotonNetwork.CurrentRoom.Players.Values I call player.SetCustomProperties(props); and it works just fine. The new value shows up on both test player instances.

    I hope the pause (between working on this) will help you.

  • I hope so too ... thing is that I don't think I should even need to set customproperties as the scoring code on the scoremanager and UI is working as expected ... it's just that they don't get the data .

  • First of all, make sure the scoring happens when it should.

    When scores change, don't change them locally (in the ui) but via SetCustomProperties. No matter where and why. Don't update the UI without using the properties. This way, when the UI shows changes, you know it's the properties. And .. in almost all cases, the other player should also see that (because it also uses properties).

    You could use Custom Room Properties for the scores instead of using Player Custom Properties. The room props can be changed by anyone, so there is no question if it's allowed or not.

    The game is turn based, right? So there is no concurrency issue.

  • yes the game is turnbased


    So I converted the code to use room properties, but it doesn't change anything whatsoever.

    For some reason when a P2 disc hits a P1 disc (and vice versa) the change in value doesn't get transmitted ... the best thing I have been able to do is have the change happen when the affected discs's player is up.


    So what I am doing is on each turn I transmit ownership of all relevant scene objects to the player who's turn it is and then use photonView.IsMine to have the new owner set the score .


    I checked in inspector and confirmed at each turn which player is the owner...but the score won't get set on the object unless the original creator is the one setting the score (or so it seems)


    Each disc has its own scoreScript which transmits its score to the scoremanager (where teh score gets calculated) upon which the calculated score value gets send to the UI manager .

    The problem is not the scoremanager nor the ui manager ...if they get a value it gets executed properly.

  • So the above code gets triggered in the playerscorescript

  • This gets triggered at the next turn even though the boolean which checks whether or not to set the score, is enabled (checked via Debug.Log)

    So why won't it execute?

    Again I transfer all ownership of relevant sceneobjects to teh respective player...so shouldn't this just work?

  • The code you shared doesn't have a single ownership check (IsMine). So I don't know why ownership transfer should be relevant.

    The physics callbacks by Unity will execute on all affected game objects and all clients (assuming you sent the shot / move and the discs move on both clients). So those scripts run immediately on the clients.

    Maybe the code updating properties / score is running more than once and or on both clients involved. So it might override the value one player sets.

    I think you may want to simplify matters: Start a tiny project with just the discs someone could shoot. Don't do scoring or anything complicated. Strip the tags. You probably want to observe which scripts get called and detect when discs hit one another. Add logic so one player runs a script and beeps or updates some score by sending an RPC. Increase the score value for each called RPC (don't just set score to X or Y, actually ADD to it) and you'll see if it increased by the amount you planned to increase it.

    This is practically all I can help. Maybe someone else can but we can't spend more time on checking code and or figuring out problems with game logic. Sorry.

  • Johan_Liebert96
    edited October 2022

    I don't understand where your struggling, you can simply make a method which checks the players score using the players custom properties, then call this in the update method

    I haven't seen how your game works, but whenever a player deals damage or whatever you add the value to the previous custom properties, this way it'll update every frame

  • I think I have found it ...


    The problem wasn't in the adjustpoints section or even the (photon) logic ... but in setting the initial scoring.

    So basically when the disc get's shot for the first time , at the end in a separate function I flip a boolean (firstTurn) to false ... which enables the possibility for the score to be adjusted .


    The problem was that I send this change only to the local player which meant that neither disc could affect the other as the boolean .

    I probably don't even need to set room or player properties ...but as I have already have it implemented I 'll just use that and will use the weekend to finally move along and fix scoring logic ( as I have changed so many things I need to go through everything, but at least I am crawling ahead).


    Thanks Tobias and Johan for pitching in

  • Thanks for the update, Martin! Glad you found it (I saw that in Discord, too). Tricky stuff...

  • Yeah that's for sure ... for my next project I am going to look for a way to program things from more centralized functions ... as boolean based things gets messy too quickly ...ah well live and learn