- Home /
How to update score (M2H networking)
Hello,
I am using M2H networking tutorial example4(FPS games) for my games. So i only used the gameSetup, FPSChat and ScoreBoard for my games. So now what i would like to ask is how to update my score table? It is quite hard for me to update the table as i feel that the score script somehow rely on gamesetup.correct me if i am wrong.
So now i will have my own gamecontrol. So how can i go about updating the score at the score script??
#pragma strict
#pragma implicit
#pragma downcast
var skin : GUISkin;
//static var newRecordMessage : String = "";
private var displayingHighscore : boolean = false;
private var highscoreText : GUIText;
private var playerName : String = "";
private var score : int = 0;
private var scoreText : String = "Loading scores";
private var hidestats : boolean = true;
private var scoreBoardHeight : int = 150;
private var gameSetupScript : GameSetup;
function Awake()
{
gameSetupScript = GetComponent(GameSetup);
//highscoreText = GetComponent(GUIText);
//highscoreText.enabled=false;
playerName = PlayerPrefs.GetString("playerName");
}
function OnGUI ()
{
GUI.skin = skin;
//scoreText = "Scoreboard:\n";
scoreText = "Players in the game:\n";
for(var entry : FPSPlayerNode in gameSetupScript.playerList)
{
//scoreText += entry.playerName+" \t"+" score: " + openableDoor1.score1 + "\n";
scoreText += entry.playerName+" \n";
//print(scoreText);
}
GUILayout.BeginArea (Rect ((Screen.width-1020),10,175,scoreBoardHeight));
GUILayout.Box(scoreText);
GUILayout.EndArea ();
}
function LocalPlayerHasKilled()
{
var kills : int =0;
var score : int =0;
for (var playerInstance : FPSPlayerNode in gameSetupScript.playerList)
{
if (Network.player == playerInstance.networkPlayer)
{
//kills = playerInstance.kills;
score = playerInstance.score;
break;
}
}
kills++;
//Overwrite the data of other players with the new correct score
networkView.RPC("UpdateScore",RPCMode.All, Network.player, score);
}
function LocalPlayerDied()
{
var kills : int = 0;
var deaths : int = 0;
var score : int = 0;
for (var playerInstance : FPSPlayerNode in gameSetupScript.playerList)
{
if (Network.player == playerInstance.networkPlayer)
{
//kills = playerInstance.kills;
score = playerInstance.score;
//print("score at SB:" + playerInstance.score);
//print("i am inside localPlayerDied");
break;
}
}
//deaths++;
//Overwrite with new correct score
//RPCMode.All -> RPC call on everyone who is connected to the server but did not tell him to buffer the call for clients which connect later
networkView.RPC("UpdateScore",RPCMode.All, Network.player,score);
}
@RPC
function UpdateScore(player : NetworkPlayer,score : int)
{
print("I am inside scoreBoard UpdateScores RPC");
Debug.Log((Network.player==player)+"= local "+ score + "score");
var found : boolean = false;
for (var playerInstance : FPSPlayerNode in gameSetupScript.playerList)
{
if (player == playerInstance.networkPlayer)
{
//playerInstance.kills=kills;
playerInstance.score = score;
found=true;
break;
}
}
if(!found)
{
Debug.LogError("Could not find network player "+player+" in the gamesetup playerlist!");
}
scoreBoardHeight = gameSetupScript.playerList.Count*15+40;
}
P.S This is the script that i will use to store my score.
Do you want to update the score online or just want a simple leaderboard?
A simple leaderboard I can explain you. Online sorry, no experience (Except using gamecenter plugins)
What you mean by update online?? No, i want a simple leaderboard that will update all players in the games plus to store the value to mysql..
See if you just want an offline leaderboard - Of that player only (the player who is using your game) simply use PlayerPrefs http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
If you want a world leaderboard use one of the GameCenter plugins :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613065445im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get NetworkPlayer from ViewID 1 Answer
Networking: RPC To Specific Player? 1 Answer
networkView not called on Child objects? 0 Answers
NetworkView conflicts between Levels 1 Answer
Couldn't Invoke RPC Function ! 1 Answer