asmaloney / GDExtensionTemplate

📜 A template project for building Godot 4 GDExtensions using CMake
The Unlicense
228 stars 22 forks source link

How to use this? Sample Project? #69

Open ahrbe1 opened 1 month ago

ahrbe1 commented 1 month ago

So I am pretty new to Godot, though I do know CMake and C++ pretty well. I went through the official gdextension docs for scons, and then later found this project. I managed to get this project building out of the box, no problem. The question I have is where does this go in relation to my Godot project files? The setup instructions in the readme don't quite tell me that part (or at least I missed it).

After inspecting https://github.com/nathanfranke/gdextension, I made some guesses and attempted to structure things like the following:

.
├── CHANGELOG.md
├── CMakeLists.txt
├── CMakePresets.json
├── MyProject/
|   └── addons/
│   |   └── MyPlugin/
│   |       ├── MyPlugin.gdextension
│   |       ├── icons/
│   |       │   └── Example.svg
│   |       └── lib/
│   |           └── Windows-AMD64
│   |               └── MyPlugin.dll
│   ├── icon.svg
│   ├── icon.svg.import
│   ├── icons/
│   ├── main.tscn
│   └── project.godot
├── LICENSE.md
├── README.md
├── build/
├── cmake/
├── extern/
│   └── godot-cpp/
├── src/
├── support_files/
└── templates/

By using the following build commands:

// x64 Native Tools Command Prompt for Visual Studio 2022
> cmake -DCMAKE_BUILD_TYPE=Release -B build -DCMAKE_INSTALL_PREFIX=MyProject/addons/ -G Ninja
> ninja -C build
> ninja -C build install

Everything compiles fine and installs fine with that. The problem is I can't seem to get Godot to find or import the extension.

Can you advise on how to create a working Godot project with this? Do I need to edit my project file? Or add this extension via the project plugin list somehow?

Also, is it possible to use this repo for multiple (unrelated) C++ extensions in the same Godot project? How does the folder structure change in that case?

Maybe consider extending the readme or adding a wiki article?

Thanks!

ahrbe1 commented 1 month ago

Ok. After poking around with this more I wound up having to tweak a couple things to get this to build and load for me in Godot 4.3 under MSVC 2022 on Windows 10:

# checkout the C++ bindings to match Godot 4.3
cd extern/godot-cpp
git checkout 4.3
# fix cmake issue with unquoted variables near line 100
vim extern/godot-cpp/CMakeLists.txt
- string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
+ string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
# disable warnings-as-errors
vim cmake/CompilerWarnings.cmake
-option( ${PROJECT_NAME_UPPERCASE}_WARNING_AS_ERROR "Treat warnings as errors" ON )
+option( ${PROJECT_NAME_UPPERCASE}_WARNING_AS_ERROR "Treat warnings as errors" OFF )
# Godot won't load the gdextension unless I edit this file
vim MyPlugin.gdextension
- linux.release.x86_64 = "lib/Linux-x86_64/libMyPlugin.so"
- windows.release.x86_64 = "lib/Windows-AMD64/MyPlugin.dll"
+ linux.x86_64 = "lib/Linux-x86_64/libMyPlugin.so"
+ windows.x86_64 = "lib/Windows-AMD64/MyPlugin.dll"