Thursday, March 9, 2017

Introduction to programming in Unreal Engine 4 part 1

As some of you might know to write a code in Unreal Engine we use C++. But it is not as simple as just C++. There are many functions and classes that are already made to communicate with Unreal Engine editor. Most of people use only blueprints in UE4 but I am taking part in a programming competition, I have to write at least some code for my game's logic instead of blueprints.
So I started reading documentation of UE4. I was like "That is too much", "Why I have to do this here". But after these several days I think that I finally understand something. So here I will try to explain how to make our main character jumping and walking (and also falling and stopping on the ground)


So lets begin. First of all we will need to create a new pawn class.








We will name it "MainCharacter".










Now we need to create a new root component to be able to attach to it rest of our components.
So we have to add:

"RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));"

to our AMainCharacter constructor in cpp file. And now we can attach our camera and our static mesh components to root component. So first of all let's create them.
Write:

"VisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisibleComponent"));
 UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
"

 and don't forget to define our VisibleComponent, to do this write

"UPROPERTY(EditAnywhere)
USceneComponent* VisibleComponent;"

UPROPERTY(EditAnywhere) will allow us to add our custom static mesh in Unreal Editor and under this line we will define VisibleComponent as a USceneComponent (don't write a semicolone after UPROPERTY).

Now we are ready to attach them:

"Camera->SetupAttachment(RootComponent);
  Camera->SetRelativeRotation(FRotator(0.0f, -90.0f, 0.0f));
  Camera->SetRelativeLocation(FVector(0.0f, 250.0f, 100.0f));
"

We just attached our new created camera to our RootComponent, then turned it through an angle of -90° in Y and moved it 250 units in Y and 100 units in Z (so up) relatively to RootComponent.

Now if we compile it in our Unreal Editor and drag in our viewport we will see something like this.



To find out more visit the 2nd part.



6 comments:

  1. Hi, did you try any other game engines before Unreal, like Unity? I've heard, that Unreal is very overwhelming for newcomers. Unity is way better start in game development, just to warm up with some basic concepts.

    Pozdrawiam serdecznie,
    Artur "ekhart" Dębkowski
    http://ekhart.pl

    ReplyDelete
    Replies
    1. Yes I've tried Unity before but I decided to learn Unreal first just like in programming where I started with C++ instead of Python. It's hard especially with 2D games that are not supported as good as in Unity. I think, that it's possible for me to change game engine because I haven't written a lot of code yet and fighting with Unreal Engine is not funny anymore. Thanks a lot for your feedback. If you convince me I will be grateful.

      Delete
  2. I've also started learning programing from C++, so I understand your point well. I won't also convince you to changing something, it's your choice and what you'll learn will be valuable for all of us. If you interested on learning mainly tech like Unreal, C++ good to go. But if you would like to make games, it would be better to start small. It's general rule of thumb.

    Pozdrawiam serdecznie,
    Artur "ekhart" Dębkowski
    http://ekhart.pl

    ReplyDelete
    Replies
    1. I think, that it is too early for me to try understand UE. I should start with something simpler, so I grabbed Unity. What a long night before me ;) Thank you for comment, I was thinking about throwing away UE before but I didn't want to learn everything (that I've already learned) again. Now I am sure.

      Delete
  3. Your welcome. Like all new skills it take time to understand some concepts, so don't rush yourself & be persistent. Best skills are transferable, so I think that after some Unity experience, Unreal will be way simpler next time. Good luck! ;)

    Kind regards,
    Artur "ekhart" Dębkowski
    http://ekhart.pl

    ReplyDelete