So, despite working with C++ and software development for quite a while now, I just started working with Unreal Engine, so I still have some questions that may sound really newbie, but I couldn’t find the answer anywhere (at least not a good one).
So, my question is, when adding “features” for my character in the game, what is the best way for doing so?
Example:
My Character can jump, sprint and grab objects. Should I create a “Character” class, and add all these method to it, or should I create separated classes for each function (Sprint, Jump and Grab) as a Actor Component class?
If so, how should I “add” these classes to the main character, including it in the code, or add them as component in the editor?
What I’m doing for now is creating separated classes and adding them as a player component, but I’m not sure if it is the best or the correct way for doing so.
If the jumping, grabing or sprinting something super advanced, you could implement them in an actor components. But for something simple, implement them as methods for the Character class. Makes more sense.
For the sprinting, I’m doing just a function Sprint() which sets the current speed from 600 to 1000 (for example) and OnStartSprint() and OnStopSprint() sets a bIsSprinting variable to true or false. So the animation blueprint can effectively detect when the player is sprinting or not.
All those capabilities are in built in the Character pawn class as a component, follow the same example from unreal, you can extend that component and add more capabilities or create your own if your want, after all that, especially for characters UE has a very sofisticated system to handle character with states machines and animation transitions, the persona editor and the animation blueprints will be your best friends, that is my suggestion
Yeah, that’s somewhat what I did, but my real question is, is there a correct or “best practice” way of adding these functions for the character? If I create an actor component, should I “attach” it to the player as a component in the editor, or direct in the code? Or there is no better way, I should just do what suits better for each situation?
As a best practice, it’s best to not have anything on the actor other than component creation if at all possible. In the ideal world, all functionality is on components since anything that goes on the actor is potentially duplicated code.
Adding components in C++ is significantly more performant than adding them in the editor. This cost is paid when the actor is spawned, so if you have something you’re spawning a ton of, it’s better to add the components in C++.
For your player character, performance doesn’t matter much since there won’t be many of them spawning and it’ll usually happen under your level load. However, for complicated objects like the player, I prefer adding components in C++ mainly because it’s harder to break things by accident there and you don’t have to worry about losing work if a blueprint class gets corrupted, which happens sometimes. It also lets me mess with various property values for balancing and tuning knowing I can always safely “reset” that property to what I started with, and then when I’m happy with it, I’ll move the new defaults over to the actor’s C++ constructor. For simpler objects (more content-y stuff) then I’ll just build the actor in the editor. If I have categories of objects, I’ll build an abstract C++ actor class with any shared components I know every object of that type has. (For example, I have an “ItemActor_Base” C++ actor class which I can use to create new blueprints which automatically have my item data component on them, etc.)
Now, that is exactly the kind of answer I was looking for! Thank you so much for your help, it was really clarifying. What you said makes a lot of sense and is how I expected it to work, since it’s basically how any other good software works, I just wanted to make sure since I really don’t know much about the engine and the editor yet.