sujaygchand
sujaygchand

Reputation: 43

How to make a static class to reference string variables with C++?

So I am trying to make a general C++ static that just holds string variables. I am using Unreal Engine 4 in this project. I have a working solution, but am looking to see if what I do in C# can be done in C++.

Working Solution (C++): DFControllerGameModeBase.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "DFControllerGameModeBase.generated.h"


#define MoveForwardInput        "MoveForward"

Implementation:

#include "DFControllerGameModeBase.h" 

void ADFCharacter::Play(){
.....

string text = MoveForwardInput;

}

However this is what I do in C# with Unity:

using System;

namespace Assets.Scripts.Helpers
{

    public static class Utilities
    {
        public static string MoveForward = "MoveForward";
    }
}

Implementation:

using Assets.Scripts.Helpers;

void Play(){

string text = Utilities.MoveForward;

}

Upvotes: 1

Views: 2648

Answers (1)

Israel dela Cruz
Israel dela Cruz

Reputation: 806

If you have no problem with encapsulation and will not be using the statics in Blueprint, then just using #define will do.

You also want to use capital letters for #define naming convention.

#define MOVE_FORWARD_INPUT "MoveForward"

But if you want it encapsulated and be able to call it in the Blueprint. Then you'll have to create a static helper class.

.h
class Utilities
{
public:
    static const FString MoveForward;

    //create BP getter function here
}

.cpp
const FString Utilities::MoveForward = "MoveForward";

Upvotes: 2

Related Questions