I've been meaning to start using the site more, quite a lot of work to maintain it these days, needs a big Joomla upgrade, so use it or lose it! With that in mind, I'm going to be starting to post little nuggets of (hopefully) wisdom, and some longer articles, all game dev, AI, and Unreal related. So...first up...a code smell. I've been having to work with some less-than-expertly written plugins recently, and have done a lot of bug fixing, many of which can be easily avoided by identifying common issues.

Common UStruct Issues

This is a pattern that I've seen cause issues many times. There are no compile or run time warnings when you do this, so it isn't obvious when you have a problem.

USTRUCT()
struct FThing
{
    UPROPERTY()
    float Foo;

    float Bar;
}

 There are a few things wrong here which we should think about.

Uninitialised Members

Neither member variable is initialised, however, the Unreal property system will assign a default value to Foo. Bar will be junk.

Non-UPROPERTY Members

Only one of the members is marked as UPROPERTY. This isn't automatically an issue, but why is the member Bar not UPROPERTY? Usually when you create a UStruct, you want to create a data object that is exposed to the reflection and blueprint system....having some data exposed in this way, and some not, I'd be wanting to know a good reason why this is the case, and check it's not an oversight.

What's The Problem?

Imagine you're working on a multiplayer title, you're sending a FThing from server to client with an RPC and using it to drive some game logic. But what the hell? I've set Bar to X on the server, and when it's replicated to the client, it's Y? Well, the reason is simple, Bar is not a UPROPERTY, so it isn't serialized. When the struct is replicated, the new FThing is constructed, Foo is set to default by the property system, then set to the replicated value, but Bar is neither initialised, nor deserialised, it it's value could be anything, so you have this new FThing on the client which appears to have replicated correctly, but one of the members is a randomly wrong value! Cue much head-scratching.