# Can someone explain this code, it does and how to it?

**URL:** <https://forums.unrealengine.com/t/can-someone-explain-this-code-it-does-and-how-to-it/480445>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 21, 2021, 7:28pm UTC](https://forums.unrealengine.com/t/can-someone-explain-this-code-it-does-and-how-to-it/480445 "2021-03-21T19:28:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![HAVmatrix](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HAVmatrix](https://forums.unrealengine.com/u/HAVmatrix)\
**Post date:** [March 21, 2021, 7:28pm UTC](https://forums.unrealengine.com/t/can-someone-explain-this-code-it-does-and-how-to-it/480445/1 "2021-03-21T19:28:02Z")

</div>

i am new to unreal engine and I saw this code of github and didn’t really understand how it work

```
#pragma once

#include "CoreMinimal.h"

#include "GameFramework/GameModeBase.h"

#include "CRDCustomReplay.h"

#include "CRDGameModeBase.generated.h"

UCLASS()
class CUSTOMREPLAYDEMO_API ACRDGameModeBase : public AGameModeBase
{
	GENERATED_BODY()

public:
    ACRDGameModeBase();

    virtual void InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage) override;
    virtual void Tick(float DeltaSeconds) override;

private:
    /** Current replay we're playing. */
    UPROPERTY()
    FCRDCustomReplay Replay;
    
    /** Current frame of the replay we're playing. */
    int32 ReplayFrame;

    /** Unreal actors we've spawned for units of the replay. */
    UPROPERTY()
    TMap<FName, AActor*> Units;

    /** Duration of a replay frame, in seconds. */
    UPROPERTY(EditDefaultsOnly)
    float SecondsPerReplayFrame;

    /** Class of actors to spawn for replay units. */
    UPROPERTY(EditDefaultsOnly)
    TSubclassOf<AActor> UnitActorClass;

    void ApplyReplayFrame(int32 NewReplayFrame);
};

#include "CRDGameModeBase.h"

#include "JsonObjectConverter.h"

#include "CRDLog.h"

ACRDGameModeBase::ACRDGameModeBase()
{
    PrimaryActorTick.bCanEverTick = true;
}

void ACRDGameModeBase::InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage)
{
    Super::InitGame(MapName, Options, ErrorMessage);

    // Build custom replay path.
    FString ReplayPath = FPaths::ProjectSavedDir() / TEXT("CustomReplays") / TEXT("CustomReplay.json");

    UE_LOG(LogCRD, Log, TEXT("Opening custom replay file %s ..."), *ReplayPath);

    // Parse JSON.
    FString ReplayJsonString;
    FFileHelper::LoadFileToString(ReplayJsonString, *ReplayPath);
    
    UE_LOG(LogCRD, Log, TEXT("%s"), *ReplayJsonString);

    FJsonObjectConverter::JsonObjectStringToUStruct<FCRDCustomReplay>(ReplayJsonString, &Replay, 0, 0);

    UE_LOG(LogCRD, Log, TEXT("Successfully parsed custom replay file %s"), *ReplayPath);

    // Log replay contents.
    for (int32 FrameIndex = 0; FrameIndex < Replay.Frames.Num(); ++FrameIndex)
    {
        const FCRDCustomReplayFrame& Frame = Replay.Frames[FrameIndex];

        for (const FCRDCustomReplayUnitState& Unit : Frame.Units)
        {
            UE_LOG(LogCRD, Log, TEXT("Frame %i - Unit %s - Location: %f, %f, %f"),
                FrameIndex, *Unit.Id.ToString(), Unit.LocationX, Unit.LocationY, Unit.LocationZ);
        }
    }

    // Start playing.
    ReplayFrame = -1;
}

void ACRDGameModeBase::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);

    // Calculate new replay frame index.
    int32 NewReplayFrame = FMath::FloorToInt(GetWorld()->GetTimeSeconds() / SecondsPerReplayFrame);

    // Check if we need to update the game world.
    if (NewReplayFrame > ReplayFrame && Replay.Frames.IsValidIndex(NewReplayFrame))
    {
        ApplyReplayFrame(NewReplayFrame);
    }
}

void ACRDGameModeBase::ApplyReplayFrame(int32 NewReplayFrame)
{
    ReplayFrame = NewReplayFrame;

    UE_LOG(LogCRD, Log, TEXT("Replay frame %i started!"), ReplayFrame);

    const FCRDCustomReplayFrame& Frame = Replay.Frames[ReplayFrame];

    // Update all units.
    for (const FCRDCustomReplayUnitState& ReplayUnit : Frame.Units)
    {
        // Get Unreal actor representing the replay unit.
        AActor* Unit = Units.FindRef(ReplayUnit.Id);

        if (!IsValid(Unit))
        {
            // We've never seen that unit before.
            // TODO: Unit might also have been destroyed in the meantime! We have to take care of that.
            // Pragmatic approach: Just lazily spawn new actor for the unit.
            Unit = GetWorld()->SpawnActor(UnitActorClass);
            Units.Add(ReplayUnit.Id, Unit);

            UE_LOG(LogCRD, Log, TEXT("Spawned new actor %s for unit %s."), *Unit->GetName(), *ReplayUnit.Id.ToString());
        }

        // Update actor location.
        FVector NewLocation = FVector(ReplayUnit.LocationX, ReplayUnit.LocationY, ReplayUnit.LocationZ);
        Unit->SetActorLocation(NewLocation);

        UE_LOG(LogCRD, Log, TEXT("Unit %s moved to location %f, %f, %f"),
            *ReplayUnit.Id.ToString(), ReplayUnit.LocationX, ReplayUnit.LocationY, ReplayUnit.LocationZ);
    }
}
```
