├── .gitignore ├── icon.png ├── .import ├── icon.png-487276ed1e3a0c39cad0279d744ee560.md5 ├── create_dlscript.png-783f4e84180692e4c9d6cd68227290ed.md5 ├── set_project_dllibrary.png-7a4932ac13d0d8c899d47c9d9b4fe60f.md5 ├── set_script_dllibrary.png-03bb19a0e793175be0eb24f5ef7aeb6b.md5 ├── dllibrary_save_as_resource.png-a1d9b898d5eb6b901d355d36ca6c8288.md5 ├── icon.png-487276ed1e3a0c39cad0279d744ee560.stex ├── dllibrary_create_new_dllibrary.png-a05c1c0b5ad575e545a76d5e8f23a329.md5 ├── dllibrary_create_new_resource.png-25c70021ab5fb6ab71168d34526bcc64.md5 ├── create_dlscript.png-783f4e84180692e4c9d6cd68227290ed.stex ├── set_project_dllibrary.png-7a4932ac13d0d8c899d47c9d9b4fe60f.stex ├── set_script_dllibrary.png-03bb19a0e793175be0eb24f5ef7aeb6b.stex ├── dllibrary_save_as_resource.png-a1d9b898d5eb6b901d355d36ca6c8288.stex ├── dllibrary_create_new_resource.png-25c70021ab5fb6ab71168d34526bcc64.stex └── dllibrary_create_new_dllibrary.png-a05c1c0b5ad575e545a76d5e8f23a329.stex ├── default_env.tres ├── src ├── Entity.gdns ├── EntityManager.gdns ├── Entity.h ├── Entity.cpp ├── gdlibrary.cpp ├── EntityManager.h └── EntityManager.cpp ├── .gitmodules ├── bin └── libentt_example.tres ├── project.godot ├── models └── Entity.tscn ├── README.md ├── icon.png.import ├── CMakeLists.txt ├── LICENSE └── main_scene.tscn /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /cmake-build-*/ 3 | *.so 4 | *.a 5 | *.dylib 6 | *.dll 7 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/portaloffreedom/godot_entt_example/HEAD/icon.png -------------------------------------------------------------------------------- /.import/icon.png-487276ed1e3a0c39cad0279d744ee560.md5: -------------------------------------------------------------------------------- 1 | source_md5="8dd9ff1eebf38898a54579d8c01b0a88" 2 | dest_md5="da70afec3c66d4e872db67f808e12edb" 3 | 4 | -------------------------------------------------------------------------------- /.import/create_dlscript.png-783f4e84180692e4c9d6cd68227290ed.md5: -------------------------------------------------------------------------------- 1 | source_md5="69039b59bd49d7455f8035187265b825" 2 | dest_md5="61e5c65c5d10b18da777033e58c0e90d" 3 | 4 | -------------------------------------------------------------------------------- /.import/set_project_dllibrary.png-7a4932ac13d0d8c899d47c9d9b4fe60f.md5: -------------------------------------------------------------------------------- 1 | source_md5="ca4d06c207da6acd35af39013c20782f" 2 | dest_md5="54e2b945f3d7773e111a7b5791da8049" 3 | 4 | -------------------------------------------------------------------------------- /.import/set_script_dllibrary.png-03bb19a0e793175be0eb24f5ef7aeb6b.md5: -------------------------------------------------------------------------------- 1 | source_md5="0d84a32e5ac6408cac27848b5f39e1bc" 2 | dest_md5="e0cc56497e0e0611e9e1129626f2f02b" 3 | 4 | -------------------------------------------------------------------------------- /.import/dllibrary_save_as_resource.png-a1d9b898d5eb6b901d355d36ca6c8288.md5: -------------------------------------------------------------------------------- 1 | source_md5="f03941273c3fa836d4b4a5dbbec62850" 2 | dest_md5="32d6077e8bbd66fb720628b2eb44d26d" 3 | 4 | -------------------------------------------------------------------------------- /.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/portaloffreedom/godot_entt_example/HEAD/.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex -------------------------------------------------------------------------------- /.import/dllibrary_create_new_dllibrary.png-a05c1c0b5ad575e545a76d5e8f23a329.md5: -------------------------------------------------------------------------------- 1 | source_md5="6eaefdc84aba925b1f1abd0050742dba" 2 | dest_md5="14edfdfdaa3e4ec4b3e7e08c257eb4ad" 3 | 4 | -------------------------------------------------------------------------------- /.import/dllibrary_create_new_resource.png-25c70021ab5fb6ab71168d34526bcc64.md5: -------------------------------------------------------------------------------- 1 | source_md5="3cae498ea518c0f3e8ef34b36e6b3aab" 2 | dest_md5="025cd95c518426bb754769dd365f537e" 3 | 4 | -------------------------------------------------------------------------------- /.import/create_dlscript.png-783f4e84180692e4c9d6cd68227290ed.stex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/portaloffreedom/godot_entt_example/HEAD/.import/create_dlscript.png-783f4e84180692e4c9d6cd68227290ed.stex -------------------------------------------------------------------------------- /default_env.tres: -------------------------------------------------------------------------------- 1 | [gd_resource type="Environment" load_steps=2 format=2] 2 | 3 | [sub_resource type="ProceduralSky" id=1] 4 | 5 | [resource] 6 | background_mode = 2 7 | background_sky = SubResource( 1 ) 8 | -------------------------------------------------------------------------------- /.import/set_project_dllibrary.png-7a4932ac13d0d8c899d47c9d9b4fe60f.stex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/portaloffreedom/godot_entt_example/HEAD/.import/set_project_dllibrary.png-7a4932ac13d0d8c899d47c9d9b4fe60f.stex -------------------------------------------------------------------------------- /.import/set_script_dllibrary.png-03bb19a0e793175be0eb24f5ef7aeb6b.stex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/portaloffreedom/godot_entt_example/HEAD/.import/set_script_dllibrary.png-03bb19a0e793175be0eb24f5ef7aeb6b.stex -------------------------------------------------------------------------------- /.import/dllibrary_save_as_resource.png-a1d9b898d5eb6b901d355d36ca6c8288.stex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/portaloffreedom/godot_entt_example/HEAD/.import/dllibrary_save_as_resource.png-a1d9b898d5eb6b901d355d36ca6c8288.stex -------------------------------------------------------------------------------- /.import/dllibrary_create_new_resource.png-25c70021ab5fb6ab71168d34526bcc64.stex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/portaloffreedom/godot_entt_example/HEAD/.import/dllibrary_create_new_resource.png-25c70021ab5fb6ab71168d34526bcc64.stex -------------------------------------------------------------------------------- /.import/dllibrary_create_new_dllibrary.png-a05c1c0b5ad575e545a76d5e8f23a329.stex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/portaloffreedom/godot_entt_example/HEAD/.import/dllibrary_create_new_dllibrary.png-a05c1c0b5ad575e545a76d5e8f23a329.stex -------------------------------------------------------------------------------- /src/Entity.gdns: -------------------------------------------------------------------------------- 1 | [gd_resource type="NativeScript" load_steps=2 format=2] 2 | 3 | [ext_resource path="res://bin/libentt_example.tres" type="GDNativeLibrary" id=1] 4 | 5 | [resource] 6 | class_name = "Entity" 7 | library = ExtResource( 1 ) 8 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "thirdparty/godot-cpp"] 2 | path = thirdparty/godot-cpp 3 | url = git@github.com:GodotNativeTools/godot-cpp.git 4 | [submodule "thirdparty/entt"] 5 | path = thirdparty/entt 6 | url = git@github.com:skypjack/entt.git 7 | -------------------------------------------------------------------------------- /src/EntityManager.gdns: -------------------------------------------------------------------------------- 1 | [gd_resource type="NativeScript" load_steps=2 format=2] 2 | 3 | [ext_resource path="res://bin/libentt_example.tres" type="GDNativeLibrary" id=1] 4 | 5 | [resource] 6 | class_name = "EntityManager" 7 | library = ExtResource( 1 ) 8 | -------------------------------------------------------------------------------- /src/Entity.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace godot { 7 | 8 | class Entity : public Spatial { 9 | GODOT_CLASS(Entity, Spatial) 10 | 11 | public: 12 | static void _register_methods(); 13 | 14 | Entity(); 15 | virtual ~Entity(); 16 | 17 | void _init(); 18 | 19 | void _process(float delta); 20 | }; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /bin/libentt_example.tres: -------------------------------------------------------------------------------- 1 | [gd_resource type="GDNativeLibrary" format=2] 2 | 3 | [resource] 4 | config_file = Object(ConfigFile,"script":null) 5 | 6 | entry/OSX.64 = "res://bin/libentt_example.dylib" 7 | entry/OSX.32 = "res://bin/libentt_example.dylib" 8 | entry/X11.64 = "res://bin/libentt_example.so" 9 | entry/X11.32 = "res://bin/libentt_example.so" 10 | dependency/OSX.64 = [ ] 11 | dependency/OSX.32 = [ ] 12 | dependency/X11.64 = [ ] 13 | dependency/X11.32 = [ ] 14 | -------------------------------------------------------------------------------- /src/Entity.cpp: -------------------------------------------------------------------------------- 1 | #include "Entity.h" 2 | 3 | using namespace godot; 4 | 5 | void Entity::_register_methods() 6 | { 7 | Godot::print("Register methods()"); 8 | register_method("_process", &Entity::_process); 9 | } 10 | 11 | Entity::Entity() 12 | { 13 | Godot::print("Entity()"); 14 | } 15 | 16 | Entity::~Entity() 17 | { 18 | Godot::print("~Entity()"); 19 | } 20 | 21 | void Entity::_init() 22 | { 23 | Godot::print("Init entity()"); 24 | } 25 | 26 | void Entity::_process(float delta) 27 | { 28 | 29 | } 30 | -------------------------------------------------------------------------------- /project.godot: -------------------------------------------------------------------------------- 1 | ; Engine configuration file. 2 | ; It's best edited using the editor UI and not directly, 3 | ; since the parameters that go here are not all obvious. 4 | ; 5 | ; Format: 6 | ; [section] ; section goes between [] 7 | ; param=value ; assign values to parameters 8 | 9 | config_version=4 10 | 11 | _global_script_classes=[ ] 12 | _global_script_class_icons={ 13 | 14 | } 15 | 16 | [application] 17 | 18 | config/name="entt_example" 19 | run/main_scene="res://main_scene.tscn" 20 | config/icon="res://icon.png" 21 | 22 | [rendering] 23 | 24 | environment/default_environment="res://default_env.tres" 25 | -------------------------------------------------------------------------------- /models/Entity.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=3 format=2] 2 | 3 | [sub_resource type="CapsuleMesh" id=1] 4 | 5 | [sub_resource type="CapsuleShape" id=2] 6 | 7 | [node name="Spatial" type="Spatial"] 8 | 9 | [node name="Mesh" type="MeshInstance" parent="."] 10 | transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 1.5, 0 ) 11 | mesh = SubResource( 1 ) 12 | material/0 = null 13 | 14 | [node name="RigidBody" type="RigidBody" parent="."] 15 | transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 1.5, 0 ) 16 | 17 | [node name="CollisionShape" type="CollisionShape" parent="RigidBody"] 18 | shape = SubResource( 2 ) 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Godot gdnative EnTT example 2 | 3 | An example on how to use gdnative with entt. 4 | 5 | ### Clone and build 6 | - clone with `git clone --recursive` (I use git submodules). If you forget you can always run later `git submodule init --recursive && git submodule update`. 7 | - build with `cmake`, no particular options required. 8 | - run as a normal godot project. 9 | 10 | #### How the example game works 11 | When running the example, just press enter ("ui_accept") to spawn another random entity. 12 | Entities will be removed when they get outside of the plane (72 * 72 square surface). 13 | 14 | #### Disclaimer 15 | this may not be the best way to use entt inside godot, it's just an easy way to do it that I want to share with the world. 16 | -------------------------------------------------------------------------------- /src/gdlibrary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Entity.h" 3 | #include "EntityManager.h" 4 | 5 | extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) 6 | { 7 | std::cout << "godot_gdnative_init()" << std::endl; 8 | godot::Godot::gdnative_init(o); 9 | } 10 | 11 | extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) 12 | { 13 | std::cout << "godot_gdnative_terminate()" << std::endl; 14 | godot::Godot::gdnative_terminate(o); 15 | } 16 | 17 | extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) 18 | { 19 | std::cout << "godot_nativescript_init()" << std::endl; 20 | godot::Godot::nativescript_init(handle); 21 | godot::register_class(); 22 | godot::register_class(); 23 | } 24 | -------------------------------------------------------------------------------- /icon.png.import: -------------------------------------------------------------------------------- 1 | [remap] 2 | 3 | importer="texture" 4 | type="StreamTexture" 5 | path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" 6 | metadata={ 7 | "vram_texture": false 8 | } 9 | 10 | [deps] 11 | 12 | source_file="res://icon.png" 13 | dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] 14 | 15 | [params] 16 | 17 | compress/mode=0 18 | compress/lossy_quality=0.7 19 | compress/hdr_mode=0 20 | compress/bptc_ldr=0 21 | compress/normal_map=0 22 | flags/repeat=0 23 | flags/filter=true 24 | flags/mipmaps=false 25 | flags/anisotropic=false 26 | flags/srgb=2 27 | process/fix_alpha_border=true 28 | process/premult_alpha=false 29 | process/HDR_as_SRGB=false 30 | process/invert_color=false 31 | stream=false 32 | size_limit=0 33 | detect_3d=true 34 | svg/scale=1.0 35 | -------------------------------------------------------------------------------- /src/EntityManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace godot { 10 | 11 | class EntityManager : public Spatial { 12 | GODOT_CLASS(EntityManager, Spatial) 13 | 14 | public: 15 | static void _register_methods(); 16 | 17 | EntityManager(); 18 | virtual ~EntityManager(); 19 | 20 | void _init(); 21 | void _process(float delta); 22 | 23 | void create_entity(); 24 | private: 25 | // registry 26 | entt::registry registry; 27 | 28 | // preload of the resources to create a single entity 29 | Ref entity_scene; 30 | Ref entity_script; 31 | 32 | // stuff for the random generator 33 | std::random_device rd; 34 | std::mt19937 gen; 35 | std::uniform_real_distribution dis; 36 | }; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0.2) 2 | project(entt_example) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_subdirectory(thirdparty/godot-cpp/) 7 | add_subdirectory(thirdparty/entt/) 8 | 9 | if(MSVC) 10 | # Force to always compile with W4 11 | if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") 12 | string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 13 | else() 14 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") 15 | endif() 16 | elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) 17 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") 18 | endif() 19 | 20 | set(SPACESHIP_SRC 21 | src/gdlibrary.cpp 22 | src/Entity.cpp 23 | src/EntityManager.cpp 24 | ) 25 | 26 | set(BUILD_PATH ${CMAKE_SOURCE_DIR}/bin) 27 | 28 | add_library(entt_example SHARED ${SPACESHIP_SRC}) 29 | 30 | target_link_libraries(entt_example 31 | ${GODOT_CPP_LIB} 32 | godot-cpp 33 | EnTT 34 | ) 35 | 36 | # create the library directly in the right folder 37 | set_target_properties(entt_example PROPERTIES 38 | LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Matteo De Carlo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main_scene.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=6 format=2] 2 | 3 | [ext_resource path="res://src/Entity.gdns" type="Script" id=1] 4 | [ext_resource path="res://src/EntityManager.gdns" type="Script" id=2] 5 | 6 | [sub_resource type="CubeMesh" id=1] 7 | 8 | [sub_resource type="BoxShape" id=2] 9 | 10 | [sub_resource type="CapsuleMesh" id=3] 11 | 12 | [node name="Spatial" type="Spatial"] 13 | 14 | [node name="Camera" type="Camera" parent="."] 15 | transform = Transform( 1, 0, 0, 0, 0.952987, 0.30301, 0, -0.30301, 0.952987, -0.00749135, 26.6293, 55.9162 ) 16 | 17 | [node name="MeshInstance" type="MeshInstance" parent="."] 18 | transform = Transform( 36.1443, 0, 0, 0, 0.0404921, 0, 0, 0, 36.1443, 0, 0, 0 ) 19 | mesh = SubResource( 1 ) 20 | material/0 = null 21 | 22 | [node name="StaticBody" type="StaticBody" parent="MeshInstance"] 23 | 24 | [node name="CollisionShape" type="CollisionShape" parent="MeshInstance/StaticBody"] 25 | shape = SubResource( 2 ) 26 | 27 | [node name="EntityMother" type="MeshInstance" parent="."] 28 | transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 20.6117, 1.5, 0 ) 29 | mesh = SubResource( 3 ) 30 | material/0 = null 31 | script = ExtResource( 1 ) 32 | 33 | [node name="EntityManager" type="Spatial" parent="."] 34 | script = ExtResource( 2 ) 35 | -------------------------------------------------------------------------------- /src/EntityManager.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "EntityManager.h" 6 | 7 | using namespace godot; 8 | 9 | typedef Vector3 position; 10 | 11 | struct velocity 12 | { 13 | float dx; 14 | float dy; 15 | float dz; 16 | }; 17 | 18 | void EntityManager::_register_methods() 19 | { 20 | register_method("_process", &EntityManager::_process); 21 | } 22 | 23 | EntityManager::EntityManager() 24 | : rd() 25 | , gen(rd()) 26 | , dis(-5.0, 5.0) 27 | {} 28 | 29 | EntityManager::~EntityManager() 30 | {} 31 | 32 | void EntityManager::_init() 33 | { 34 | this->entity_scene = ResourceLoader::get_singleton()->load("res://models/Entity.tscn"); 35 | this->entity_script = ResourceLoader::get_singleton()->load("res://src/Entity.gdns"); 36 | this->create_entity(); 37 | } 38 | 39 | void EntityManager::_process(float delta) 40 | { 41 | registry.view().each([delta](position &pos, velocity &vel, Spatial* s_entity) { 42 | pos.x += vel.dx * delta; 43 | pos.y += vel.dy * delta; 44 | pos.z += vel.dz * delta; 45 | s_entity->set_translation(Vector3(pos.x, pos.y, pos.z)); 46 | }); 47 | 48 | for(auto &entity: registry.view()) 49 | { 50 | const float arena_size = 36.0f; 51 | auto &pos = registry.get(entity); 52 | if (fabs(pos.x) > arena_size or fabs(pos.y) > arena_size) 53 | { 54 | registry.get(entity)->queue_free(); 55 | registry.destroy(entity); 56 | } 57 | } 58 | 59 | Input *input = Input::get_singleton(); 60 | if (input->is_action_pressed("ui_accept")) 61 | { 62 | this->create_entity(); 63 | } 64 | } 65 | 66 | void EntityManager::create_entity() 67 | { 68 | Godot::print("Adding node"); 69 | entt::entity entity = registry.create(); 70 | float dx = dis(gen); 71 | float dz = dis(gen); 72 | std::stringstream message; 73 | message << "Generating entity with velocity dx(" << dx << ") dz(" << dz << ")"; 74 | Godot::print(message.str().c_str()); 75 | registry.assign(entity, 0.0f, 0.0f, 0.0f); 76 | registry.assign(entity, dx, 0.0f, dz); 77 | Spatial* entity_node = reinterpret_cast(entity_scene->instance()); 78 | this->add_child(entity_node); 79 | entity_node->set_script(this->entity_script.ptr()); 80 | registry.assign(entity, entity_node); 81 | Godot::print("Node Added"); 82 | } 83 | --------------------------------------------------------------------------------