r-value reference

Can I create a reference to a temporary object with UFUNCTION()? If I write SomeUnrealStruct&& _name he gives “Missing variable name”. I tried searching but there is no such question anywhere.

Hi Nagibator_1229,

Can you explain what it is you’re trying to do - are you trying to get a reference to the whole object or just a Function in the instanced object? How are you creating the object? Can you show us your code?

I’ll try. I’m working on the network part of my game. Because so much of a multiplayer game is done through the server, I was trying to create a template server function that would take the name of a function to be called on the server, and any number of arguments of any type, so that I could call it from the server using a delegate. Unfortunately, a delegate (FSimpleDelegate, for example) cannot be in function parameters. Unreal swears at it. I’m trying to get around this by creating my struct and declaring a FSimpleDelegate in it. But it seems that this does not work through BindUFunction. The value of the variable in the structure is simply lost. And it doesn’t matter what I declare in the structure, FSimpleDelegate or regular int. In general, here is the code:

DECLARE_DELEGATE_OneParam(SomeDelegate, const FVariable);
PlayerInputComponent->BindAction<SomeDelegate>("X", IE_Pressed, this, &AChernishCharacter::Server, FVariable(5));

Then Server:

void AChernishCharacter::Server_Implementation(const FVariable _delegate)
{
	int temp = _delegate.delegate;
	UE_LOG(LogTemp, Warning, TEXT("Delegate = %d"), temp);
}

As I understand it, the move constructor must be called between these two methods. But he doesn’t. And when the program starts, the value of the variable is 0. I tried to write FVariable && in the server arguments, but it gives the error “Missing variable name”. And all this nonsense only works if you declare FVariable as a field of the Character class with UPROPERTY(). I can leave it like that, but can it work with temporary objects? Or maybe you have ideas how to make the server logic so that you don’t have to write a separate Server implementation for each method where it should be called? Thank you in advance

You should be able to pass FDelegateHandles as parameters, the hooks all do that.

Check out some of the code in UE for the hooks, here’s one:

template <typename UserClass, typename... VarTypes>
	inline FDelegateHandle AddRaw(UserClass* InUserObject, typename TMemFunPtrType<false, UserClass, void (ParamTypes..., VarTypes...)>::Type InFunc, VarTypes... Vars)
	{
		static_assert(!TIsConst<UserClass>::Value, "Attempting to bind a delegate with a const object pointer and non-const member function.");

		return Add(FDelegate::CreateRaw(InUserObject, InFunc, Vars...));
	}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.