Best place for generic code

Hello :slight_smile:

I have not yet decided where is the best place to put code that processes some topic and is not associated with an instance of any class. I don’t have a C++ background, perhaps that is why I don’t know what is the usual approach here.

Let me describe with two examples:

I have an InGame timer, this has nothing to do with any instance of any object. I just need a place to put some general logic to deal with integer numbers (I did it similar to UNIX Timestamp). I couldn’t find an easy answer to where this code/logic should go. In the end I made a raw C++ class and coded it there. It felt strange to do so because I don’t know to which extent I can use Unreal functionalities when I am dealing with a Raw C++ class and it felt like I was doing something hacky as I could never be sure if this class would work on not when I had to interact with it from within an Unreal Class.

What should it be? Should I make an Unreal Object that has only one instance? (just feels like unnecessary complexity (and features) for my case.

The current scenario I am facing and I have the same doubt in my head: I want to implement some generic code that will deal with relocation logic of the character when an animation is playing and the real location of the character must be synced to. I want to do this as a logic that is completely independent from any instance and just receive characters that need relocation as a parameter.

What is the correct approach to do this in Unreal? Could someone share some insights or forward me to some documentation I could check?

well, i can read this question in two ways:

  1. “Gameplay” systems, that is systems that isn’t tied to particular actor while not quite fitting into gamemode or gameinstance, like time-of-the-day system or some item-drop-manager.

Those are goes into Subsystems. note: this doc is lacking: UWorldSubsystem is also a thing and is missing from doc

If you ever question yourself “should i make a singleton for this”, in UE the answer is always “definitely use ue’s subsystems instead”

  1. Generic utility functions, that mostly consist of a single function, not assembling into a particular system, and have no need to store any data, like custom LineOfSight check, IsAlive(unit), GetAllUnits() or custom math like IsPointOnScreen().

I’m having those stored as a static functions in a single class derived from UBlueprintFunctionLibrary. Well, it’s mostly a bloated junkyard of a 3k lines in sum now, but it does its work well enough.
Can also be structured into a several classes, but i find using the single generic UUtils easier than several different UKismetRenderingLibrary, UGameplayStatics, UKismetSystemLibrary, etc. Though this ones have a good reason to be separated, it doesn’t makes things easier.
Also, you does want to derive it’s from UObject(i mean UBlueprintFunctionLibrary), so you can easily expose your utilities to blueprints

Another few notes:

Generally this sounds like a candidate to be implemented like an Actor’s Component

On the other side, this exact task, unless you wan’t to make something custom, in 3d game is usually handled by Animation’s root motion (if your motion for specific animation is static. If it’s dynamic, like charging to enemy, it’s another thing)

2 Likes

Thank you @IrSoil
your two answers are on point with what I was wondering about, on the UWorldSubsystem this open a complete new topic to be aware of when deciding where to place logic.

And about Animation’s Root Motion, I will read through Unreal Documentation on the topic as it looks like I was about to implement something that Unreal already offers support to.