Announcement

Collapse
No announcement yet.

Saving player scores then loading them after server travel?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Saving player scores then loading them after server travel?

    I need to save playerscores for 4 seperate players at the end of the round when only 1 player is alive, and then load the player scores for each player after a Server Travel

    with help from garner we've tried a lot of things and cant seem to get it to work..

    GameInfo extending UTGame

    Code:
    function UpdateScore(int playerID, int newScore){
    	local TRAILPlayerController PC;
    
    	foreach WorldInfo.AllControllers(class'TRAILPlayerController',PC)
    	{
    		`log(PC.PlayerReplicationInfo.PlayerName $ " " $ PC.PlayerReplicationInfo.Score);
    		PC.UpdateScore(playerID, newScore);
    	}
        
    }
    
    function EndGame(PlayerReplicationInfo Winner, string Reason ){
    local TRAILPlayerController PC;
    
    	foreach WorldInfo.AllControllers(class'TRAILPlayerController',PC)
    	{
    		/** DEBUG: Save a temp value to every player.  Remove this so it doesn't override any scores during from real gameplay */
    		PC.PlayerReplicationInfo.Score = 99999999;
    
    		//Save the data that exists in the playerreplicationinfo
    		PC.SaveTheGame();
    	}
    
    	super.EndGame(Winner, Reason);
    }
    
    function ReloadPlayerScores(){
    local TRAILPlayerController PC;
    
    	foreach WorldInfo.AllControllers(class'TRAILPlayerController',PC)
    	{
    		PC.hasLoadedScores = false; //Force the playercontroller to reload scores
    	}
    }
    
    function StartMatch(){
    	super.StartMatch();
    	ReloadPlayerScores();
    }
    
    function ProcessServerTravel(string URL, optional bool bAbsolute)
    {
    	local Controller C;
    
    	if (!IsInState('MatchOver'))
    	{
    		foreach WorldInfo.AllControllers(class'Controller', C)
    		{
    			C.GameHasEnded();
    		}
    		GotoState('MatchOver');
    	}
    
    	Super.ProcessServerTravel(URL, bAbsolute);
    	ReloadPlayerScores(); //Maybe here
    	// on dedicated servers, add a delay to the travel process to give clients a little more time to construct any meshes
    	// since that process will get cut off when the server completely finishes travelling
    	if (WorldInfo.NetMode == NM_DedicatedServer && WorldInfo.IsInSeamlessTravel())
    	{
    		WorldInfo.SetSeamlessTravelMidpointPause(true);
    		SetTimer(0.1, false, 'ContinueSeamlessTravel');
    	}
    
    }
    
    defaultproperties
    {
    	PlayerControllerClass=class'TRAILGame.TRAILPlayerController'
    	//Replication classes
    	PlayerReplicationInfoClass=class'UTPlayerReplicationInfo'
    	GameReplicationInfoClass=class'UTGameReplicationInfo'
    	DefaultPawnClass=class'TRAILGame.TRAILPawn'
    	HUDType=class'TRAILGame.TRAILTeamHUD'
    	bUseClassicHUD=true
    	bFixedPlayerStart=true
    
    	DefaultMaxLives=1
    	bTeamScoreRounds=true
    	//bScoreTeamKills=false
    	bScoreDeaths=true
    	bMustJoinBeforeStart=true
    	bRestartLevel=false
        EndMessageCounter=1
    	EndMessageWait=1
    	Countdown=2
    
    	MaxPlayersAllowed=4
            DeathMessageClass=none
    
    	// Default set of options to publish to the online service
    	OnlineGameSettingsClass=class'UTGame.UTGameSettingsDM'
    	OnlineStatsWriteClass=Class'UTStatsWriteDM'
            bUseSeamlessTravel=true
    	
    }

    GameState

    Code:
    class TRAILGameState extends object;
    
    var int playerScore;
    
    
    function SaveObject(String Path)
    {
    	class'Engine'.static.BasicSaveObject(self, Path, true, 1);
    }
    
    function LoadObject(String Path)
    {
    	class'Engine'.static.BasicLoadObject(self,Path, true, 1);
    
    }
    
    DefaultProperties
    {
    	playerscore=0
    }
    PlayerController extending UTPlayerController

    Code:
    var TRAILGameState gameState; 
    var int yourScore; 
    var bool hasLoadedScores;
    
    simulated event PostBeginPlay()
    {
        super.PostBeginPlay();
    
        gameState = new class'TRAILGameState'; //Whatever you gamestate class is
    
        gameState.playerScore = 900;
      
        PlayerReplicationInfo.Score = 300;
    }
    
    //Within yourplayercontroller class
    function GetAllPlayerScores(){
        local PlayerReplicationInfo thisPRI;
    
        if (WorldInfo.GRI != none){
            foreach WorldInfo.GRI.PRIArray(thisPRI) {
                `log("PlayerName: " $ thisPRI.PlayerName $ " Score: " $ thisPRI.Score $ " TeamScore: " $thisPRI.Team.Score);
            }
        }
    }
    
    
    function UpdateScore(int playerID, int newScore){
        if (myHud != none){
            TRAILTeamHud(myHud).UpdateTheScoreForPlayer(playerID, newScore);
        }
    }
    
    reliable client function SaveTheGame(){
        `log("We are saving the game...");
    
        gameState.playerScore = PlayerReplicationInfo.Score; //Or PlayerReplicationInfo.Team.Score
        class'Engine'.static.BasicSaveObject(gameState, "GameState.bin", true, 0);
    
        `log("We have saved the game!");
    }
    
    
    //When the match is over, set the final score 
    simulated function GameIsOver_SaveScores(){
        gameState.playerScore = PlayerReplicationInfo.Score;
    
       class'Engine'.static.BasicSaveObject(gameState, "GameState.bin", true, 0);
    }
    
    
    simulated function PlayerTick( float DeltaTime ) {
    
    
            if (PlayerReplicationInfo != none && !hasLoadedScores){
                hasLoadedScores = true;
    
                if(class'Engine'.static.BasicLoadObject(gameState, "GameState.bin", true, 0)) //Load only if the file actually exists
                {
                    PlayerReplicationInfo.Score = gameState.playerScore;
                }
            }
    
    
            //`log(PlayerReplicationInfo.Score);
    
    
            super.PlayerTick( DeltaTime );
    }
    
    DefaultProperties
    {
        hasLoadedScores = false
    }

    At the moment, on Round 1 all players have 100000000 points, then after I kill 3 by suicide, 3 of them should have -1 from that. But after server travel only the host has 299 the other 3 players have 0. Then after the next server travel the host has -1 and the rest have 0. then it stays the same.


    Also my scoreboard only shows on the first run, after the server travel it doesnt show

    Any help is greatly appreciated, thanks

    #2
    reliable client function SaveTheGame(){
    Clients should save their score on their side? That should be done by the server only. Only the server replicates these scores to other player connections.

    Not even sure why you do the hard way... using ServerTravel.

    Comment


      #3
      Originally posted by RattleSN4K3 View Post
      Clients should save their score on their side? That should be done by the server only. Only the server replicates these scores to other player connections.

      Not even sure why you do the hard way... using ServerTravel.
      Whats the easy way? I want rounds but I don't know how to make rounds. So its players have 1 life and then the server restarts.

      Comment


        #4
        https://www.youtube.com/watch?v=IRLt...ature=youtu.be

        video of what happens


        each player saves their own score now

        Code:
        reliable client function SaveTheGame(){
            `log("We are saving the game...");
        
            gameState.playerScore = PlayerReplicationInfo.Score; //Or PlayerReplicationInfo.Team.Score
            class'Engine'.static.BasicSaveObject(gameState, PlayerReplicationInfo.PlayerName $ "_Save.bin", true, 0);
        
            `log("We have saved the game!");
        }
        but they dont save and load right after the travel
        also scoreboard wont show after the first load

        Comment


          #5
          Ok, its actually working for the host,

          Code:
          		/** DEBUG: Save a temp value to every player.  Remove this so it doesn't override any scores during from real gameplay */
          		//PC.PlayerReplicationInfo.Score = 99999999;
          Code:
          simulated event PostBeginPlay()
          {
              super.PostBeginPlay();
          
              gameState = new class'TRAILGameState'; //Whatever you gamestate class is
          
              //gameState.playerScore = 900;
            
              //PlayerReplicationInfo.Score = 300;
          }
          I commented out these and deleted the playername_saves.

          I loaded the game, all players have 0, I suicided 3 including host vehicle.

          Server traveled and host had -1 but others had 0.

          I done the same again and host had -2 and others had 0.


          Anyone know why its only working on host?


          EDIT: ok its so sporadic, just tried killing again and it won't go past -2

          ok it went to -3 on host then stopped for the next rounds

          Maybe its because when the save happens? sometimes it was saving after the host died and sometimes it wasnt

          Code:
          reliable client function SaveTheGame(){
              `log("We are saving the game...");
          
              gameState.playerScore = PlayerReplicationInfo.Score; //Or PlayerReplicationInfo.Team.Score
              class'Engine'.static.BasicSaveObject(gameState, PlayerReplicationInfo.PlayerName $ "_Save.bin", true, 0);
          
              `log("We have saved the game!");
          }
          This happens at random times? how do I get it to save when rounds over and winner gets focus

          Comment

          Working...
          X