Announcement

Collapse
No announcement yet.

Questions on PlayerState + Replication

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

    Questions on PlayerState + Replication

    Hello unreal developers,

    I have some questions regarded the PlayerState class and logic on Replication

    PlayerState
    #1. I have create my own playerstate class and set it up in GameMode.cpp constructor like:
    Code:
    PlayerStateClass = ATTSPlayerState::StaticClass();
    But whenever I try to access it it and it's variables it says: Error at reading address violition or something like that... How do I access that class?

    #2. Is it a good idea to put health/hunger/energy inside PlayerState and replicate it?

    #3. If so.. How do I acces these variables inside my Character?
    Last edited by Young_Wolf; 01-03-2015, 11:15 AM.
    Raise the Black Flag!

    - [TUT]: HTTP GET Request in C++
    - [Tool]: Game Site Tracker website for your project
    - [Website]: Website for my game Leviathan

    #2
    1. How are you trying to access the class?
    2. When I was looking through the ShooterGame example...it seemed they used the PlayerState to keep up with Statistical type of information (Number of Kills ect). I believe the reason for this is, there is a PlayerState per PlayerController, and a Player Controller has the capability of possessing different characters or pawns. So for example, in your example, if you were to put health there, if your game had any capability of the PlayerController switching pawns...they would all share the same PlayerState. I think this is one of the more confusing aspects of Unreal...I think the documentation should do more to describe what kind of information should typically go where.
    3. From your Character, you can get the PlayerController using the property 'Controller' (just cast it to your player controller type) that you are possessed by, and from there the PlayerState or you can access the PlayerState directly using the property of the same name, just keep in mind its pointing to the one associated with the controller that possesses your character. (which goes back to the problem of keeping something like 'Health' in playerstate)

    I would definitely take a look at the ShooterGame example, as it helped me alot with this as it fully implements replication. You can find it in the 'Learn' section of the launcher to download.

    edit:

    Just to wrap things up, I think the 'Character' is the more appropriate place to put Health, as Health is usually specific to them. I believe PlayerState and PlayerController is more of representation of the Human player himself, so information related to that is more appropriate there.
    Last edited by Awdogsgo2heaven; 01-03-2015, 02:05 PM.

    Comment


      #3
      Here's what I did:

      Code trying to access player state inside Character:
      Code:
      GEngine->AddOnScreenDebugMessage(21, 1.0f, FColor::Red, FString::SanitizeFloat(Cast<ATTSPlayerState>(GetController()->PlayerState)->SomeVariable));
      It does not work it says violition to address and stuff

      But this code here works. I wrote it inside a custom hud class:

      Code:
      ATTSCharacter* Char = Cast<ATTSCharacter>(GetOwningPawn());
      	
      	if (Char)
      	{
      		ATTSPlayerState* PlayerS = Cast<ATTSPlayerState>(Char->PlayerState);
      
      		if (PlayerS)
      		{
      			GEngine->AddOnScreenDebugMessage(1, 3.0f, FColor::Red, FString::SanitizeFloat(PlayerS->SomeVariable));
      
      		}
      	}
      The second code displays the code perfectly... but I can't access it in Character
      Raise the Black Flag!

      - [TUT]: HTTP GET Request in C++
      - [Tool]: Game Site Tracker website for your project
      - [Website]: Website for my game Leviathan

      Comment


        #4
        Did you verify that you are getting the access violation on the PlayerState and not he Controller (Controller could be null if the Character is not possessed by one? Also, at hwat point are you printing that on the Screen?

        Comment


          #5
          Just tried accessing it like this:

          Code:
          GEngine->AddOnScreenDebugMessage(21, 1.0f, FColor::Red, FString::SanitizeFloat(Cast<ATTSPlayerState>(PlayerState)->SomeVariable));
          Still not working :/
          Raise the Black Flag!

          - [TUT]: HTTP GET Request in C++
          - [Tool]: Game Site Tracker website for your project
          - [Website]: Website for my game Leviathan

          Comment


            #6
            Originally posted by OzoneBG View Post
            Just tried accessing it like this:

            Code:
            GEngine->AddOnScreenDebugMessage(21, 1.0f, FColor::Red, FString::SanitizeFloat(Cast<ATTSPlayerState>(PlayerState)->SomeVariable));
            Still not working :/
            Hey try doing it a bit safer mate.
            Code:
            // This is done from inside the character/ pawn class, but all you need is ether a Pawn or Controller to get the Player State
            if (GEngine)
            {
            	const AYourPlayerController* PlayerPC = Cast<AYourPlayerController>(GetController());
            	if (PlayerPC)
            	{
            		ATTSPlayerState* YourPlayerStateClass = Cast<ATTSPlayerState>(PlayerPC->PlayerState);
            		if (YourPlayerStateClass)
            		{
            			GEngine->AddOnScreenDebugMessage(21, 1.0f, FColor::Red, FString::SanitizeFloat(YourPlayerStateClass->SomeVariable);
            		}
            	}
            }
            Also as mentioned above Health belongs in the character/ pawn, and Player State is for information about that player relevant to other players.
            e.g: Score, Deahs, Player Name and so 4th.
            Last edited by WCode; 01-04-2015, 11:25 PM. Reason: spelling

            Comment


              #7
              Originally posted by OzoneBG View Post
              #1. I have create my own playerstate class and set it up in GameMode.cpp constructor like:
              Code:
              PlayerStateClass = ATTSPlayerState::StaticClass();
              But whenever I try to access it it and it's variables it says: Error at reading address violition or something like that... How do I access that class?
              As others have said, you should guard your usage to make sure the variable is accessible, it will help ascertain what isn't present at the time of the call. That being said there are lots of reasons why an APlayerState is NULL where you are trying to access it.

              If this is a client, the server has to first give the player state to you via replication. This can take an indeterminate amount of time and you should wait for an "OnRep" notify. There are already two for this class, one on APlayerController and another on APawn. You may not have a pawn yet, but you should have a controller pretty quickly after login (again its not guaranteed based on network handshaking). APlayerController is pretty safe to work with after ReceivedPlayer or you could always have an RPC from the server in PostLogin/GenericPlayerInitialization tell the client that it is ok to do something (still may not have an APlayerState yet, but you should in most normal situations).

              If this is a server, you should pretty much have one after the constructor for the AController is complete. If you need to do something, you should at least wait for InitPlayerState on the AController.


              #2. Is it a good idea to put health/hunger/energy inside PlayerState and replicate it?
              The APlayerState was meant to be a lightweight extension of the AController. Clients only have only their own controller, so in to get information on everyone else we have the array of APlayerState on the AGameState. Put anything you want there that you'd typically liked all clients to know. Server can write to the class and if the property is replicated, everyone will get it. Clients can write to the class and keep the data local to the client. Clients can make an RPC to the server and tell it to change the value which replicates to clients (including back to the client that made the call). It will be up to you to keep the flow of information straight, but the point is that it can be anything from server authoritative information to client scratchpad information. All caveats about availability of data apply, especially around new players joining and others leaving.

              #3. If so.. How do I acces these variables inside my Character?
              ACharacter derives from APawn, so any pawn function should be accessible. PlayerState is a public member of both APawn and AController.

              Hope this helps.

              Josh
              Last edited by Crzyhomer; 01-05-2015, 05:19 PM.

              Comment

              Working...
              X