1

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;

}

  • 2
    Possible duplicate of [Static constant string (class member)](https://stackoverflow.com/questions/1563897/static-constant-string-class-member) – Summer Apr 22 '19 at 04:29
  • i don't know if this is what you really need, but you can do this using namespace and enum class. Won't be a static variable, but will fit the purpose. And you can use include guards or pragma once to avoid duplicate. – Radagast Apr 22 '19 at 04:58
  • @Radagast Thanks for the direction, I have considered enums. This is more of a question of me trying to explore C++ and translating my C# sample over – sujaygchand Apr 22 '19 at 05:01
  • 1
    @sujaygchand bruglesco's redirect link should be the better option then. good luck! – Radagast Apr 22 '19 at 16:39

1 Answers1

2

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";
Israel dela Cruz
  • 794
  • 1
  • 5
  • 11