Download

Passing a UObject derived object to an RPC function

Hi all,

I was wondering if it is at all possible to pass UObject-derived custom classes as parameters to RPC functions.

I have various use cases where this is necessary: one example is an interface I have on classes that request animations on actors: they derive from a custom Interface with a “callback”, so that these objects can complete certain functions when an animations starts/completes. It is not possible to pass interfaces through, so it was suggested I pass a UObject and cast to the interface - however, the UObject always comes through null.

Another use case is that I have a custom UObject-derived class, UItem. When characters equip items, I want to make an RPC call passing the UItem through so that I can inform the server and then complete a range of logic on the server and client (update statistics, show new models, etc). However, whenever I pass it through, it always appears to be NULL afterwards.

Is there a way these objects should be passed via RPC? Is there a way to make UObjects not null? Should they be of another type?

As far as I know, there are some types of UObjects that you can send through the network (i.e references to cooked assets) but I honestly wouldn’t go that route. It seems far more reliable to create a USTRUCT to represent your object’s state, and create functions to essentially “serialize / deserialize” your object into this struct, and pass the struct through the RPC. I’m not an expert but that would be my general way to do it.

For your specific usecase, what you may need is a RPC function with an enum (for the animation) and a dynamic delegate (for the callback). The enum can be made to match a specific animation, while the dynamic delegate is serializable and so seems the more likely to work through RPCs (and save files).

When you send a pointer to an object through an RPC, that object needs to have a valid network ID - so you can only send them if the UObjects themselves are already replicated through other means, otherwise clients/server won’t know object is being referred to.

Here’s a handy wiki link:
https://wiki.unrealengine.com/Replic…tor_Subobjects