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;

}

4
  • 2
    Possible duplicate of Static constant string (class member) Commented Apr 22, 2019 at 4: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. Commented Apr 22, 2019 at 4: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 Commented Apr 22, 2019 at 5:01
  • 1
    @sujaygchand bruglesco's redirect link should be the better option then. good luck! Commented Apr 22, 2019 at 16:39

1 Answer 1

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";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer, exactly what I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.