Variable input name and value to string

Hello! In C++ you can define MACRO and use #x to get name of the variable x. This is used in Engine code as well, for example if you search for #x you can find these useful MACROS:

  • MSC_FORMAT_DIAGNOSTIC_HELPER
  • PREPROCESSOR_TO_STRING and etc

Hi, currently trying to learn some C++ coding.
What I try to do is to have a node that basically converts a variable to a string, defined as “VariableName, Value”.

Any ideas on achieving that ? I can’t seem to be able to get the name exposed of the input.

thanks in advance.

Hi Kehel18, thanks for the reply, since i’m still trying to grasp how everything works, how would that look like in code ?

What I have so far is (manually setting the variable name in UE)

// Convert variable to name and value string
bool USaveTxtFile::VariableToString(FString VariableName, float VariableInFloat, int32 VariableInInteger, FString VariableInString, FString& StringOut)
{
StringOut = VariableName + TEXT(",") + FString::FromInt(VariableInInteger);

//StringOut = VariableName + TEXT(",") + FString::SanitizeFloat(VariableInFloat);
//StringOut = VariableName + TEXT(",") + VariableInString;
	
return true;

}

Hi Kehel18, thanks for the reply, since i’m still trying to grasp how everything works, how would that look like in code ?

What I have so far is (manually setting the variable name in UE)

// Convert variable to name and value string
bool USaveTxtFile::VariableToString(FString VariableName, float VariableInFloat, int32 VariableInInteger, FString VariableInString, FString& StringOut)
{
StringOut = VariableName + TEXT(",") + FString::FromInt(VariableInInteger);

//StringOut = VariableName + TEXT(",") + FString::SanitizeFloat(VariableInFloat);
//StringOut = VariableName + TEXT(",") + VariableInString;
	
return true;

}

Just take a look at this c++ - print variable name in a #define - Stack Overflow

As far as I got it gives me the name of the variable that is defined in the c++ code, not the one of the node that is attached to it in Unreal. Is that correct ?

Yes but I want to get to the node that is attached to the input of the C++

I was thinking that you need it in C++ because you have pointed that you are learning some code. Do you need that for Blueprint variables?

Brrr ) I dond understand what are you searching ) You have BP variable node and search for BP node that will get some info from variable node and will have “VariableName, Value” formatted string as output, right? Or you want to create that kind of node?

hahaha let me try to clarify, so what i’m trying to do is create a blueprintnode with C++ that can function as a vault to store variables into a local file. Ideally no matter what variable node you would plug into it (wildcard), the C++ blueprintnode uses the name of the variable and the value and stores it into a stringmap so that later on you can use the name and value to feed again in unreal. There is the option in Unreal to store variables to a configfile but it reloads only after build so I’m trying to make it work whenever I want with a simple save/load.

Does that make more sense ?

At last I understand you )) Can you try this

FString VariableToString2(const FString& VariableName, float VariableInFloat, int32 VariableInInteger, const FString& VariableInString, FString& StringOut) {
		...
}

I found something like this:

#define TT(Value) FString::Printf(TEXT(“%s: %i”),TEXT(value), Value)

int z = 5;
UE_LOG(LogTemp, Warning, TEXT(“%s”), *TT(z));

Ofcourse this works only for int. For now I’am working on class that could represend logs like std::cout to the console, and you could print variable with name.