Book Image

Unreal Engine 4 Scripting with C++ Cookbook

By : William Sherif, Stephen Whittle
Book Image

Unreal Engine 4 Scripting with C++ Cookbook

By: William Sherif, Stephen Whittle

Overview of this book

Unreal Engine 4 (UE4) is a complete suite of game development tools made by game developers, for game developers. With more than 100 practical recipes, this book is a guide showcasing techniques to use the power of C++ scripting while developing games with UE4. It will start with adding and editing C++ classes from within the Unreal Editor. It will delve into one of Unreal's primary strengths, the ability for designers to customize programmer-developed actors and components. It will help you understand the benefits of when and how to use C++ as the scripting tool. With a blend of task-oriented recipes, this book will provide actionable information about scripting games with UE4, and manipulating the game and the development environment using C++. Towards the end of the book, you will be empowered to become a top-notch developer with Unreal Engine 4 using C++ as the scripting language.
Table of Contents (19 chapters)
Unreal Engine 4 Scripting with C++ Cookbook
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Creating a Time of Day handler


This recipe shows you how to use the concepts introduced in the previous recipes to create an actor that informs other actors of the passage of time within your game.

How to do it...

  1. Create a new Actor class called TimeOfDayHandler.

  2. Add a multicast delegate declaration to the header:

    DECLARE_MULTICAST_DELEGATE_TwoParams(FOnTimeChangedSignature, int32, int32)
  3. Add an instance of our delegate to the class declaration:

    FOnTimeChangedSignatureOnTimeChanged;
  4. Add the following properties to the class:

    UPROPERTY()
    int32 TimeScale;
    
    UPROPERTY()
    int32 Hours;
    UPROPERTY()
    int32 Minutes;
    
    UPROPERTY()
    float ElapsedSeconds;
  5. Add the initialization of these properties to the constructor:

    TimeScale = 60;
    Hours = 0;
    Minutes = 0;
    ElapsedSeconds = 0;
  6. Inside Tick, add the following code:

    ElapsedSeconds += (DeltaTime * TimeScale);
    if (ElapsedSeconds> 60)
    {
      ElapsedSeconds -= 60;
      Minutes++;
      if (Minutes > 60)
      {
        Minutes -= 60;
        Hours++;
      }
    
      OnTimeChanged.Broadcast(Hours,...