Creating a plugin with RedLib
This guide will show you how you can write a plugin with beautiful code thanks to RedLib and RED4ext.SDK. If you haven't, you should learn the fundamentals of writing a plugin (see Creating a Plugin).
Requirements
You must install RED4ext.SDK in your project like any other plugin (see Creating a plugin). You need to install RedLib. See the README to configure your CMake project.
Setup plugin to register types
Now you can change the entry-point of your plugin like this:
/// File: src\\main.cpp
#include <RED4ext/RED4ext.hpp>
// You must include RedLib. Note that it introduces an alias
// such as namespace RED4ext can be replaced by Red.
#include <RedLib.hpp>
RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::PluginHandle aHandle,
RED4ext::EMainReason aReason,
const RED4ext::Sdk* aSdk) {
switch (aReason) {
case RED4ext::EMainReason::Load: {
// It will automatically register types declared below.
Red::TypeInfoRegistrar::RegisterDiscovered();
break;
}
case RED4ext::EMainReason::Unload: {
break;
}
}
return true;
}
// ...Declare new classes
We are going to create a zoo, and name our mod... Zoo. Now lets create a generic Animal class:
Now lets add a Wolf to our zoo:
If you tried to declare a class with only RED4ext.SDK so far, it sure looks better now with RedLib! Now compile your project and install your plugin in the game's directory:
<Cyberpunk 2077>\red4ext\plugins\Zoo\Zoo.dll
We also need to declare native types so we can use them in scripts:
Lets create our zoo somewhere when the game starts:
Install scripts in your game's directory:
<Cyberpunk 2077>\r6\scripts\Zoo\Zoo.reds
<Cyberpunk 2077>\r6\scripts\ZooTest.reds
Run the game, you should see some outputs in the Game Log when using CET.
Conclusion
This is a simple introduction to RedLib and how it can make your life easier when writing plugin with RED4ext.SDK. You should definitively go to the GitHub repository and read through the entire README to learn about all the features it provides.
Happy coding!
Last updated