Hi, I searched for a proper docs, how to create a plugin from zero to a “here comes a big one”, but found only some questions on stackoverflow and wiki on ue4. There’s some main stuff how to begin with, but no information how to continue and at least some info on code style and style of creating new classes etc.
So I’ll tell a few words, what are the main points of plugin from my noobish point of view and then ask you, how to continue that or edit my mistakes, if I understood wrongly.
Basically I looked in other plugin, that’s already implemented and actively used in the project called VaRestPlugin and tried to do something similar.
First important part is that you need add
*.uplugin
file, to modules array the name of your plugin f.e. like here:
Modules:
{
"Name": "My Plugin",
"Type": "Runtime",
"LoadingPhase": "PreDefault"
}
]
Basically that’s it. Buut, I totally don’t know, what’s “Type” and what’s “LoadingPhase”. Maybe its just a stage at which plugin will be loaded into the project. And I hope somebody can explain me it here.
Second, we move on and there’s C# file, called *.Build.cs and there’re just some includes from UE4. What I did, I just included basic stuff like “Engine”, “Core”. I guess that’s the basic stuff, that every plugin should have. I also use “CoreUObject” if you want to have functions, which you can add in BPs.
And I almost forgot about there’re functions called public dependencies and private dependencies. Well the name tells, what this stuff do, by itself. Public - outworld plugins, source code, third party services etc. Private - now there some stuff I think I don’t understand about this, but I’m guessing, what this stuff does is making all plugin in one piece or in smart words collects all the source files to one place, which means just makes
#include
to whatever you write.
Ok, moving on.
There’s some conventions where to place your files. Basically in good plugin you have 3 of those: Public, Private and Classes. First 2 are explained on wiki, Classes is just a folder for header source code, that use BP macroses.
Basically what you should have is a Public/IMyPlugin.h. This file usually has default source code, f.e. here:
class ISocket : public IModuleInterface
{
public:
static inline ISocket& Get()
{
return FModuleManager::LoadModuleChecked<ISocket>("SocketPlugin");
}
static inline bool IsAvailable()
{
return FModuleManager::Get().IsModuleLoaded("SocketPlugin");
}
};
What you need to turn attention to, the arguments to these functions should be the name of your plugin(in my case its SocketPlugin) and template should be the same as class.
There’re 2 more required files of source codes:
- MyPluginPrivatePCH.h - source file, where you put all your headers. In most cases they’re just general engine files and public files. Example:
#pragma once
#include "CoreUObject.h"
#include "Engine.h"
#include "ModuleManager.h"
#include "ISocket.h"
- MyPlugin.cpp. It should be named as your plugin with cpp added. Basically what this class does it, do some stuff when you startup plugin and when you shutdown it.
Why you need this? To load some other classes you’ve made and create a ue4 module. Example:
#include "SocketPrivatePCH.h"
class SocketImpl : public ISocket
{
public:
virtual void StartupModule() override
{
SocketUObject::StaticClass();
}
virtual void ShutdownModule() override
{
}
};
IMPLEMENT_MODULE(SocketImpl, Socket)
Hm, what to turn attention to. First, you include, that PCH header file, then create a class which extends class that was from Public/IMyPlugin.h. Second to override functions StartupModule() and ShutdownModule(). Third there’s a UE4 macros IMPLEMENT_MODULE, first argument should name of class in the MyPlugin.cpp, second the name of the plugin.
Basically its a good to go plugin, then you just need to generate binaries for plugin. This can be done with right mouse button on *.uproject → generate VS project files.
Ok this stuff is for those, who starts doing a plugin and is stuck at basic level.
And my questions.
You see in code above, there’s a code line
SocketUObject::StaticClass();
, which must include an external class to the module, but it doesn’t. So, how can I include this static class into startup module?
P.S. As the conversation progresses I’ll add new stuff that ppl will tell me and I’ll test it myself, that it works. Again for other newcomers like myself.
P.S.S. Because in my point of view, wiki and sources, doesn’t explain fully the given topic.
Thanks to everyone, who shares ideas and knowledge.