Update README.md

This commit is contained in:
Sara 2025-06-25 18:31:03 +00:00
parent a379a5b564
commit c12633143c

View file

@ -2,51 +2,53 @@
> Specifically for use with [godot-cpp-template](https://git.objectionable.solutions/hertog/godot-module-template). > Specifically for use with [godot-cpp-template](https://git.objectionable.solutions/hertog/godot-module-template).
## Headers ## Header Files
```cpp ```cpp
// always use #define-based header guards, // #define-based header guards are required,
// the name has to match the filename // and have to match the name has to match the filename
#ifndef MY_CLASS_H #ifndef MY_CLASS_H
#define MY_CLASS_H #define MY_CLASS_H
// PascalCase for all types // PascalCase for all types
class MyClass : public Object { class MyClass : public Object {
GDCLASS(MyClass, Object); GDCLASS(MyClass, Object);
// functions are always snake_cased // functions have to be snake_cased
static void _bind_methods(); static void _bind_methods();
void on_some_signal(); // signal listeners are private unless otherwise called void on_some_signal(); // signal listeners have to be private unless they have to be publicly accessible otherwise
public: public:
void do_things(); void do_things();
// getters and setters go after other types of functions, closest to the variables.
void set_my_var(int value); // set first void set_my_var(int value); // set first
int get_my_var() const; // get second, and const except when it can't be int get_my_var() const; // get function goes second, and should be const if possible
// private variables go at the bottom
private: private:
int myvar{1}; // variables are snake_case, and always initialized in headers unless impossible int myvar{ 1 }; // variables are snake_case, and always initialized in headers unless impossible
// (e.g, const vars that get initialized in constructors) // (e.g, const vars that get initialized in constructors)
String const sig_some_signal{"some_signal"}; // store signal names in a variable to make them easier to keep track of String const sig_some_signal{ "some_signal" }; // store signal names in a variable to make them easier to keep track of
}; };
#endif // !MY_CLASS_H // end #if/#ifdef/#ifndef directives with a repeat of the condition #endif // !MY_CLASS_H // end #if/#ifdef/#ifndef directives with a repeat of the condition
``` ```
# Implementations # Implementation Files
```cpp ```cpp
#include "my_class.h" #include "my_class.h"
// the matching header always goes on top, other header includes come after in alphabetical order // the matching header always goes on top, other header includes come after in alphabetical order
#include "macros.h" #include "macros.h"
void MyClass::_bind_methods() { void MyClass::_bind_methods() {
BIND_PROPERTY(Variant::INT, my_var); // use macros.h simplified binding tools when possible BIND_PROPERTY(Variant::INT, my_var); // use macros.h simplified property binding macros when possible
} }
void MyClass::do_things() { void MyClass::do_things() {
set_my_var(10); // this-> is not required for class methods set_my_var(10); // this-> is not required for class methods
cast_to<Object>(this); // nor for static class methods. cast_to<Object>(this); // static class methods don't require scope specification.
// use self_type when referring to the type of 'this' // use self_type when referring to the type of 'this'
this->connect(this->sig_some_signal, callable_mp(this, &self_type::on_some_signal)); this->connect(this->sig_some_signal, callable_mp(this, &self_type::on_some_signal));
} }
void MyClass::set_my_var(int value) { void MyClass::set_my_var(int value) {
// for all uses of member variables, use this-> // all uses of member variables REQUIRE this->
this->my_var = value; this->my_var = value;
} }