-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added guide for adding a tab to the GUI with a C++ mod
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
This guide will show how you create tabs for the GUI with a C++ mod, and the guide will take the form of comments in the code example below: | ||
```c++ | ||
#include <Mod/CppUserModBase.hpp> | ||
#include <UE4SSProgram.hpp> | ||
|
||
class MyAwesomeMod : public RC::CppUserModBase | ||
{ | ||
private: | ||
int m_private_number{33}; | ||
std::shared_ptr<GUI::GUITab> m_less_safe_tab{}; | ||
|
||
public: | ||
MyAwesomeMod() : CppUserModBase() | ||
{ | ||
ModName = STR("MyAwesomeMod"); | ||
ModVersion = STR("1.0"); | ||
ModDescription = STR("This is my awesome mod"); | ||
ModAuthors = STR("UE4SS Team"); | ||
|
||
// It's critical that we enable ImGui before we create our tab. | ||
// If you don't do this, a crash will occur as soon as ImGui tries to render anything in your tab. | ||
UE4SS_ENABLE_IMGUI() | ||
|
||
// The 'register_tab' function will tell UE4SS to render a tab. | ||
// Tabs registered this way will be automatically cleaned up when this C++ mod is destructed. | ||
// The first param is the display name of your tab. | ||
// The second param is a callback that UE4SS will use to render the contents of the tab. | ||
// The param to the callback is a pointer to your mod. | ||
register_tab(STR("My Test Tab"), [](CppUserModBase* instance) { | ||
// In this callback, you can start rendering the contents of your tab with ImGui. | ||
ImGui::Text("This is the contents of the tab"); | ||
|
||
// We can access members of our mod class with the 'instance' param. | ||
auto mod = dynamic_cast<MyAwesomeMod*>(instance); | ||
if (!mod) | ||
{ | ||
// Something went wrong that caused the 'instance' to not be correctly set. | ||
// Let's abort the rest of the function before we access an invalid pointer. | ||
return; | ||
} | ||
|
||
// You can access both public and private members. | ||
mod->render_some_stuff(mod->m_private_number); | ||
}); | ||
|
||
// The 'UE4SSProgram::add_gui_tab' function is another way to tell UE4SS to render a tab. | ||
// This way of registering a tab will make you responsible for cleaning up the tab when your mod destructs. | ||
// Failure to clean up the tab on mod destruction will result in a crash. | ||
// It's recommended that you use 'register_tab' instead of this function. | ||
m_less_safe_tab = std::make_shared<GUI::GUITab>(STR("My Less Safe Tab"), [](CppUserModBase* instance) { | ||
// This callback is identical to the one used with 'register_tab' except 'instance' is always nullptr. | ||
ImGui::Text(std::format("This is the contents of the less safe tab").c_str()); | ||
}); | ||
UE4SSProgram::get_program().add_gui_tab(m_less_safe_tab); | ||
} | ||
|
||
~MyAwesomeMod() override | ||
{ | ||
// Because we created a tab with 'UE4SSProgram::add_gui_tab', we must manually remove it. | ||
// Failure to remove the tab will result in a crash. | ||
UE4SSProgram::get_program().remove_gui_tab(m_less_safe_tab); | ||
} | ||
|
||
auto render_some_stuff(int Number) -> void | ||
{ | ||
auto calculated_value = Number + 1; | ||
ImGui::Text(std::format("calculated_value: {}", calculated_value).c_str()); | ||
} | ||
}; | ||
|
||
#define MY_AWESOME_MOD_API __declspec(dllexport) | ||
extern "C" | ||
{ | ||
MY_AWESOME_MOD_API RC::CppUserModBase* start_mod() | ||
{ | ||
return new MyAwesomeMod(); | ||
} | ||
|
||
MY_AWESOME_MOD_API void uninstall_mod(RC::CppUserModBase* mod) | ||
{ | ||
delete mod; | ||
} | ||
} | ||
``` |