Static Utils Class

I’m trying to make a static utils class which functions can be used from anywhere, however when I try to access the functions from other classes, the Utils class is not found.

As you guessed, I’m new to C++.

What am I doing wrong?

Since you haven’t showed us an attempted usage or the error I can only guess. But my first inclination is that you didn’t #include “Utils.h” in the source file trying to use the class.

I did actually include it, here’s my attempt to use Utils:

While we’re at it, is there also a possibility to use it without including it?

It seems like Unreal already has a class named “Utils”, and that the one I created was deleted somehow. I’ll see if that’s the problem.

My Static Function Libraries Wiki Tutorial

Enjoy!

Rama

Thanks Rama looks great!

When I’m creating new classes they’re always moved to Intermediate\ProjectFiles instead of Source\Test. Any idea why and what’s the difference?

Also is the “.generated.h” automatically created for classes I add via Unreal Editor? Should I only create new classes via Unreal Editor?

I did some research and it seems like I must always add new classes via File -> Add Code To Project in Unreal Editor, and in the most simple case base it on UObject. Please correct me if I’m wrong.

Is there a way to make the static class automatically accessible to other classes in the project without having to include its header every time?

You totally are not forced to use “Add Code To Project” only. Yes, in most of the cases your classes will be derived from UObject.

To make your class accessible anywhere with out the need to manually include it everywhere you could include it in your precompiled header, which should be “YourProjectName.h”, include it right after Engine.h.
Take a note, precompiled header is included in every .cpp file, so if you want to use your class in header files also(like inline functions) you have to include header for your class in that header as well.

When I don’t use “Add Code To Project” I always have trouble connecting it with the rest of the code. Could you please see what I wrote in one of my above replies about the folder my classes are moved to?

Making it accessible everywhere - yup good idea.

I add code manually via “Add - New Item” and add .h and .cpp files separately, you should be able to choose location where to create these files.


And they should always be located in YourProject\Source\YourProjectName.
Not exactly like this, they should be located in the corresponding game module directory, but ignore that for a while, you have only one module currently, and its directory is “YourProjectName”. Check documentation on Unreal Build Tool for a better understanding of this(don’t forget about game samples as well).

Thanks I guess I missed that. I’m guessing the other classes in the same project couldn’t find the new classes because of their different location?

Yes. Search paths could be added in YourProject.build.cs using the following line:


PrivateIncludePaths.AddRange(new string] { "SomePath", "AnotherPath" } );

By default it looks for headers in module directory, if you want to create sub-directory for your files you’ll have to add them in *.builld.cs

Gotcha, thanks.