Creating a Custom Native Class
When writing a RED4ext plugin in conjunction with a Redscript project, it can be useful to create class members in RED4ext, and access them through Redscript. A similar example in code form can be seen here.

RED4ext Snippets

Here we need to create a struct that inherits from RED4ext::IScriptable (the class that all classes inherit from in Redscript). The header for this needs to be defined at the top-level, or in a namespace (outside any function):
1
struct CustomController : RED4ext::IScriptable
2
{
3
RED4ext::CClass* GetNativeType();
4
};
Copied!
A template is then used to create all the necessary CP2077 hooks for the class - the type we pass to the template function is the type we just created, and the argument is the name of our class as a string in Redscript. We'll also need to define GetNativeType() that returns a reference to our CP2077 type. The name of customControllerClass doesn't matter in this context, as it's pointing to our type in RED4ext.
1
RED4ext::TTypedClass<CustomController> customControllerClass("CustomController");
2
​
3
RED4ext::CClass* CustomController::GetNativeType()
4
{
5
return &customControllerClass;
6
}
Copied!
Using the class & type we've created, we'll write two hooks (RegisterTypes and PostRegisterTypes) to register the type with CP2077 at the appropriate times. The first one that gets called sets the isNative flag for the class, which will match the native descriptor on the Redscript side, and registers the type to CP2077:
1
RED4EXT_C_EXPORT void RED4EXT_CALL RegisterTypes()
2
{
3
RED4ext::CRTTISystem::Get()->RegisterType(&customControllerClass);
4
}
Copied!
The next block will manually set the parent of our type to IScriptable, to match the class.
1
RED4EXT_C_EXPORT void RED4EXT_CALL PostRegisterTypes()
2
{
3
auto rtti = RED4ext::CRTTISystem::Get();
4
auto scriptable = rtti->GetClass("IScriptable");
5
customControllerClass.parent = scriptable;
6
}
Copied!
To make all of this work, we then need to hook-in our callbacks, which we do with RED4ext::RTTIRegistrator::Add in our main function:
1
RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::PluginHandle aHandle,
2
RED4ext::EMainReason aReason, RED4ext::RED4ext* aRED4ext)
3
{
4
switch (aReason)
5
{
6
case RED4ext::EMainReason::Load:
7
{
8
RED4ext::RTTIRegistrator::Add(RegisterTypes, PostRegisterTypes);
9
break;
10
}
11
case RED4ext::EMainReason::Unload:
12
{
13
break;
14
}
15
}
16
17
return true;
18
}
Copied!

Redscript Snippet

On this side, the declaration is pretty simple, and similar to the headers from the decompiled scripts. Technically the extends IScriptable is implied for all classes, and can be omitted here:
1
public native class CustomController extends IScriptable {
2
​
3
}
Copied!
Like usual, you can define any non-native member variables and methods here, and use them in Redscript, but out of the box our class doesn't do anything yet. In the following pages, we'll define some functionality with custom native functions.
Export as PDF
Copy link