x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to create custom PlayerState and PlayerController in C++?

Hi everyone! I need to want to create a custom PlayerState and PlayerController but I don't know exactly what I should do. What steps I must follow?? I couldn't find anything about creating custom c++ player states or player controller. For example, I create a class that inherit from PlayerState and I add some floats to it (I want put it some character's attributes) and I modify the defaults PlayerState class to my own class (change it in projects preferences-> maps and modes) but know, the project crash so I must fail somewhere... Later, I really don't know how to link all this class (PlayerController, PlayerStates and Character) to work together. Could anyone help me with a little guide or a link to a tutorial? Thanks in advance!!!!

PD: the code I had in CharacterStats (PlayerState inherit):

 #include ... /// the PlayerState.h
 #include CharacterStats /// generated.h file
 
 UCLASS()
 class /*API*/ CharacterStats : public PlayerState
 {
      GENERATED_BODY()
      public:
      float Health;
      float Mana;
      float Energy;
      float Agility;
      // And some more variables
 }

I comment some parts because I remove the class (a cause of the crash) but the only things that I add are the variables, rest is all like de defaults definition Unreal create when you create a class that inherit from PlayerState.

Product Version: UE 4.19
Tags:
more ▼

asked Apr 25 '18 at 10:28 PM in C++ Programming

avatar image

Superkorlas
20 2 3 9

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

You already know a lot if you did it in blueprint, there exact same events and function available, 95% of all nodes in blueprints are direct C++ bindings, all events you use there are also avable and in most cases they have exact same names. The Target is a equivalent of object you calling function on, looking on type of Target pin you know in which class this function is. Look on API refrence:

http://api.unrealengine.com/INT/API/Runtime/Engine/GameFramework/APlayerController/

All virtual functions (with V icon) you see are overable and you can use them as events. You override function like this here BeginPlay for example

.h (in class decleration):

 virtual void BeginPlay() override;

.cpp

 void AYourActor::BeginPlay() 
 {
       //your BeginPlay code
 
       Super::BeginPlay();
 }

Doing that Super call is super importent as it calls overrided code and if oyu dont do that you may block some of the engine code from execution, consequance of not doing so in BeginPlay for example is BeginPlay event not being called in blueprint based on your class, as in AActor code in BeginPlay() it calls blueprint event for BeginPlay.

As for crash you would need to show your code so we can see whats wrong. see the logs in Saved/Logs to see crash info (sometimes it gives you direcly information what went wrong) or run debugger and VS will show you like that causes the crash in stack.

more ▼

answered Apr 25 '18 at 11:30 PM

avatar image

Shadowriver
36.7k 932 169 1111

avatar image Superkorlas Apr 26 '18 at 07:20 AM

Thanks for your fast answer, I add the code I had. May I should override the BeginPlay method?? I don't override any method, just define some variables.

avatar image Superkorlas Apr 26 '18 at 04:31 PM

Ok, I das the BeginPlay event and all work properly!! Thanks!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question