Monday, September 26, 2016

Unreal Engine 4 C++ Resources

code, coder, coding



Unreal Engine 4 C++ Resources: Intro

Ah, yes, the rough looking starter projects

I have been using Unreal Engine 4 by Epic Games for almost 3 years now and have really enjoyed developing games in this game engine. I have spent hours looking for good content on applying my C++ skills in Unreal Engine 4.

What I want to share with you right now are some good resources that I have constantly gone back to for references. There are about three categories I would split my findings into for Unreal Engine 4 C++ tutorials and examples. These categories would be the obvious, the popular, and the obscure.

Unreal Engine 4 C++ Resources: The List


The Obvious


Whether it be from the horses mouth or an expert in the field. These are solid choices for learning anything about Unreal Engine 4 C++ coding.

C++ Tutorial on Unreal Engine's YouTube Playlist



This tutorial is great for beginners with Unreal Engine 4's C++ API. It will teach you how to create gameplay code, interact with objects, some simple HUD work, as well as some effects. I watched a few of the videos when I was starting out just to get a jumpstart into C++ with UE4.

The Unreal Engine 4 Wiki


The Wiki is always a good place to stop by when looking to implement some common game components. My very own Quest Framework is posted there as well. There are so many tutorials involving AI, C++, Blueprints, and Building with UE4 that it is an invaluable resource.

The Unreal Engine 4 Forums


This is a decent place to go to get some information about C++ in Unreal Engine 4. You may not always get an answer, but when it comes to the more clear questions and descriptions of what you want, there are some good people that will help you out. Sometimes a staffer will comment as well and really steer you into the right direction.

The Source Code


  • The source code. (You MUST be logged in and a part of Epic Games to view this private repository)
If there is anywhere to go as the definite source (pun intended, I'd be lying if I said it wasn't) to C++ code in UE4, it is the source code. If ever you have any questions in regards to how certian modules work or how something is implemented, this is the place to go to. I use it extensively when writing plugins and editor extensions. It is exceptionally helpful to look at the MessageBus, OnlineSubsystem, and Actor replication portions of the code when writing networked games.

The Popular


Tom Looman's Blog


When it came to writing network code and code for interactive objects in Unreal Engine 4, this was my go to place. Tom Looman has most of his tutorials in C++ and Blueprints. What this does is allow for a smooth transition if you are used to Blueprints and wish to move to C++.

The Unreal Engine 4 Sub-Reddit


Reddit seems to have a large crowd and there is relatively good activity in the subreddit. Most of the time I see a lot of beginners go there to ask questions. It is usually pretty helpful.

Rama / EverNewJoy


This person does a lot of stuff with UE4 and has a Plugin library for Blueprints. There are also a few other Plugins on the marketplace by this person. From forum posts to tutorials, transition guides, and wiki posts - Rama has a lot of content and most of it is in C++. Also, Rama seems to enjoy lots of color and hearts.


The Obscure


This is just a small collection of links to random things about Unreal Engine 4 and it's C++ components that nobody really talks about. Most of these links are one off a very specific. They are as follows:
Things start to look nicer the more you apply yourself
Well, that is about all I use when going in to take a look at C++ demos and examples in Unreal Engine 4. Obviously, this is not the extent of all the resources available for Unreal Engine 4 C++. If there is anything that you may know of that I missed and is usable by a wide range of people, feel free to comment. I hope for anyone who was looking for some help in C++ for Unreal Engine 4 that this offered as a good guidance of where to go.

Until next time! Enjoy your game programming!

Saturday, September 17, 2016

Unreal Engine Quest Framework Part 1.5

After spending some time on vacation and on an epic honeymoon, I have finally had time to get back to continue my original Quest Framework series. But, first, let me interrupt and thank everyone out there for their support. This is by far the most popular content I have ever written and it brings me great joy that you support it and find it useful.

With all the time off, and the time back at my day job, I have not been able to fully complete the second part of my Quest Framework series. However, I do have an addition I would like to share that may be useful. This addition is a new class that generates random Objectives from a predefined list of objectives inside of a Quest.


Quest Framework Part 1.5: Random Objectives


Want your players to look this excited for your quests? Don't use this framework for evil, no randomly generated fetch quests allowed!


Following the format of the last post, I will layout the header file for you, then explain some of the variables. From there I will layout the definitions and explain all of the logic.

The Head


class QUESTGAME_API AQuestRandom : public AQuest
{
    GENERATED_BODY()

public:
    AQuestRandom();

    virtual void BeginPlay() override;
 
protected:
    UPROPERTY(EditDefaultsOnly, Category = "Quest")
         TArray<TSubclassOf<AObjective>> PossibleObjectives;

    UPROPERTY(EditDefaultsOnly, Category = "Quest", meta=(UIMin=1))
         int32 MinObjectivesGenerated;

    UPROPERTY(EditDefaultsOnly, Category = "Quest", meta=(UIMin=1))
        int32 MaxObjectivesGenerated;

private:
    /** Helper methods for validation and generation.
    * ::GenerateRandomObjectives() could be made public 
    * and wrapped with UFUNCTION(BlueprintCallable, Category="Quest") to make it exposed to the rest of your game.
    */
    void GenerateRandomObjectives();
    bool IsValidConfiguration() const; 
 
};

Pretty nifty huh? We just inherit from AQuest (defined in the original Part 1 post) and add a few configuration parameters that can be set in either an inherited class or Blueprint.

PossibleObjectives is the meat of this class. Just add AObjective subclasses to it in the derived Blueprint or subclass and whenever the AQuestRandom is spawned in game, the class will do all the work of adding some random Objectives.

MinObjectivesGenerated and MaxObjectivesGenerated are there to help add some constraints to the amount of randomly generated quests. In proper fashion, these numbers are just constraints on the random determination of the amount of random Objectives created. I heard people like randomness on top of their randomness so I threw it in just for fun.

Next, I will show you the source for this nifty class and explain the true meat of the randomization algorithm (which isn't too difficult at all).



The Source


Free stock photo of technology, computer, desktop, programming

#include "QuestRandom.h"

AQuestRandom::AQuestRandom() :
    AQuest(),
    MinObjectivesGenerated(AbsoluteMin),
    MaxObjectivesGenerated(AbsoluteMin)
{

}

void AQuestRandom::BeginPlay()
{
    if (IsValidConfiguration())
    {
        GenerateRandomObjectives();
    }
    //Must be called last as we need to fill the Objective subclass array first
    Super::BeginPlay(); 
}

void AQuestRandom::GenerateRandomObjectives()
{
    const bool bMakeOnlyOne = MinObjectivesGenerated == MaxObjectivesGenerated;
    if (bMakeOnlyOne)
    {
        int32 RandomIndex = FMath::RandRange(0, PossibleObjectives.Num() - 1);
        Objectives.Add(PossibleObjectives[RandomIndex]);
    }
    else
    {
        int32 RandomCount = FMath::RandRange(MinObjectivesGenerated, MaxObjectivesGenerated);
        for (int32 i = (AbsoluteMin - 1); i < RandomCount; ++i) 
        {
            int32 RandomIndex = FMath::RandRange(0, PossibleObjectives.Num() - 1);
            /* Note: if you want only one type of quest to be active at a time,
            * i.e. treat Objective types array as a set, use AddUnique
            */
            Objectives.Add(PossibleObjectives[RandomIndex]);
        }
    }
}

bool AQuestRandom::IsValidConfiguration() const
{
    return MinObjectivesGenerated <= MaxObjectivesGenerated && PossibleObjectives.Num() > 0;
}

Awesome right? We really only need one function to add randomly generated Objectives to our Quests! Huzzah! So, let us look immediately into the GenerateRandomObjectives function as it is the crux of our class.

First, if we have the same MinObjectivesGenerated and MaxObjectivesGenerated, we are going to short circuit and say the designer only wants to pick one random Objective for this Quest. It is also the default behavior due to the values set in the Constructor of the class.

If the designer does not go with this default behavior we will actually do some work.

So, if we really look at it, the algorithm is not that hard to implement. All we need is to grab a random number clamped to the values provided to us from MinObjectivesGenerated and MaxObjectivesGenerated. From there we just iterate as many times as the RandomCount has defined to create that many Objectives.

In both configurations we just grab a random number starting at 0 (the starting index of an array) and PossibleObjectives.Num() - 1 (we subtract one to ensure we are within the bounds of the array as index access is 0 based but the Num() starts at 1 - this is a general rule).

From there, the base class takes care of everything else for us.

The End


And there you have it. Random objective generation. So, hopefully you can add some more content and unique playthroughs of your games via random Objectives in your Quests.

Until next time!

Note - I will have an official part 2 for this series that will have subsections. I am currently prototyping with the idea of implementing quests as state machines and message publishers to decouple a lot of the quest implementation from your actual game-play code. On top of that I am going to implement it as a plugin with components so that you can port it over multiple games. Why? Because 'Quests', 'Objectives', and 'Missions' are really all the same thing and can be written generic enough that the core foundation of them work across all game types.