Workaround Posted for Compiler Error When Passing FString as Server Function Parameter

Update 2: For anyone who needs a workaround for this issue, here’s my solution that I use for my multiplayer chat system, use an FName as the parameter instead, and convert your FString to FName as follows (till the fix occurs)

GRI .h

UFUNCTION(reliable, server)
 void SERVER_ChatToGRI(FName ReceivedText);

controller or anywhere.cpp

FString ChatswithName = "Happy Day";
GRI->SERVER_ChatToGRI(FName( *ChatswithName));

then to convert FName back to string

FString ReceivedString = ReceivedText.ToString();

Update: Anything I am missing here, or will this be fixed in next build? Anyone else able to reproduce this?


Dear Friends at Epic,


Woohoo UE4 is Awesome!!!

tyepicdevsheart.jpg


I can pass in a FName, but when I try to pass in a FString as follows

.h

UFUNCTION(reliable, server)
void SERVER_AddToVictoryChatText(FString test);

.cpp

void AVictoryGRI::SERVER_AddToVictoryChatText_Implementation(FString test)
{
       //some code
}

I get the compile error:

error C2511: ‘void
AVictoryGRI::SERVER_AddToVictoryChatText(const
FString &)’ : overloaded member
function not found in ‘AVictoryGRI’

This can’t possibly be expected behavior, right?


Compiler Erroneous Assumption

Why is the compiler assuming I am using a const FString & when I am not ?


You can’t pass variables by reference into server functions anyway, so why would VS compiler think this?


thanks!

:slight_smile:

:heart:

Rama

Hi Nathan, this is a bug and should be fixed soon. Check again in the next Rocket build.

thanks for the answer Dave!

I hope my research helped you all out :slight_smile:


I posted the workaround that I am using for this, hope it helps!

Rama

Hi Nathan,

As of CL# 1876359, UHT will generate an error when you pass replicated FStrings by value. The intended use is by const reference, and so your function should be declared as:

UFUNCTION(reliable, server)
void SERVER_AddToVictoryChatText(const FString& test);

This is equivalent to the issue described in this UDN article:

https://udn.unrealengine.com/questions/175795/tarray-as-a-replicated-function-parameter.html

Steve

Thanks for the answer Steve! (I cannot access the UDN using my Rocket login info)

I am confused because I thought we could not pass server parameters by reference, similar to the restriction on out parameters in UDK UE3.

I tried to implement what you suggested and it broke my chat system

Sometimes tone is not always clear in an internet communication, I want to make it clear I am not trying to sound know-it-all or antagonistic in any way, I am simply explaining what I am experiencing and also my confusion as to why passing by reference would work in a networked context, still very eager to hear your feedback:

On the server, where the memory address will be accurate, the message goes through, but does not appear on the client

on the client, where memory address will not be accurate, the message does not go through at all

I have had to revert to my FName workaround in order to maintain my game feature.


Can you explain to me why I would be able pass variables by reference to server functions in UE4, but could not do so with out parameters in UE3?

If I am passing a memory address across the network, how would it possibly be accurate on the other computer?


If this const &FString policy is not going to be changed at any time, more clear compiler error message would certainly help others with debugging once rocket goes public :slight_smile:


but again,

as of now I am unable to successfully implement your suggested use of FString by reference in a server function

If the issue is on my end or is beta version related let me know please, in meantime I am compelled by necessity of functionality to use my FName workaround

:slight_smile:

Rama

For clarity, here’s my code:

GRI .h

UFUNCTION(reliable, server)
	void SERVER_AddToVictoryChatText(const FString& test);

gri. cpp

void AVictoryGRI::SERVER_AddToVictoryChatText_Implementation(const FString& test)
{    	
	//weird FString server function parameter bug
	FString newS = test;//.ToString();

//newS was originally the FName converted to FString
//no other code has been changed....

again, no compile errors now, but now the game feature simply does not work in my multiplayer tests, whereas it worked when not passing variables by ref across the network, and instead

passing FName by value and converting to FString (since FString by value does not compile)

The only lines of code that are different between the working version and the non-functional but compiling FString by Reference version are the lines I am showing, all other code is the same.


Again I am not proclaiming I am “right” about anything here, I am just reporting back my experience

:slight_smile:

Rama

When passing by a const reference, what is the actual problem you are seeing? You say it doesn’t go through but does this mean the server doesn’t execute SERVER_AddToVictoryChatText or that the test parameter string is empty?

Hi Nathan,

Dave is in a far better position to answer specific replication questions.

As for your question about addresses, you shouldn’t think of a const reference parameter as the address of the server object, but rather a reference to an already-replicated client-side copy of that object. You are of course going to get problems if you retain that address, rely on it being a reliable handle or cast away const and modify the object, but you get those problems on regular const reference parameters too.

Instead, you can query/copy the string and it will be a valid const reference for the duration of the function, just like you would expect in a normal C++ function. The server will retain its copy of that argument, unchanged.

Out parameters in UE3 (and indeed UE4 as non-const references) are not supported, because writing through a reference is expected to change the server’s view of the object, which of course doesn’t happen.

Hope this helps clarify things,

Steve

I tried the test again, and it worked this time!

I must have had some other wires crossed in my internal code when I was attempting the test the first time.

Thank you Steve for you very detailed explanation, that is helping me to understand this topic better :slight_smile:

And thanks to Dave also, for all your network help!

:slight_smile:

My only remaining comment on this topic is that it would be nice if the compiler explained better that the error was lack of const FString &, the error as reported in my main post was very vague because it said “over loaded” and I was not even trying to use that syntax so I was confused :slight_smile:

No problem. :slight_smile: And the fix I mentioned in my first post is the better error message. It will say:

Replicated FString parameters must be passed by const reference

Steve

Great! That’s a very nice error msg :slight_smile:

Thanks for all the help!

Rama