From 791cea356d9bc1a5f9cd8e5156b23e200a35ad1f Mon Sep 17 00:00:00 2001 From: Morph01 <145839520+Morph01@users.noreply.github.com> Date: Fri, 30 Aug 2024 15:49:22 +0200 Subject: [PATCH] Fix SubViewport bug when opening the scene FirstPersonPlayer / Zombie (bone collision shape) / Bullet in the editor --- src/BoneCollisionShape.cpp | 48 ++++++++++++++++++++++---------------- src/Bullet.cpp | 7 ++++++ src/Crosshair.cpp | 15 ++++++++++-- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/BoneCollisionShape.cpp b/src/BoneCollisionShape.cpp index 2322cab..97bba9d 100644 --- a/src/BoneCollisionShape.cpp +++ b/src/BoneCollisionShape.cpp @@ -1,23 +1,25 @@ #include "BoneCollisionShape.h" +#include + using namespace godot; namespace blitz { void BoneCollisionShape::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_head_damage"), &BoneCollisionShape::get_head_damage); - ClassDB::bind_method(D_METHOD("set_head_damage", "m_HeadDamage"), &BoneCollisionShape::set_head_damage); - ADD_PROPERTY(PropertyInfo(Variant::INT, "m_HeadDamage"), "set_head_damage", "get_head_damage"); + ClassDB::bind_method(D_METHOD("get_head_damage"), &BoneCollisionShape::get_head_damage); + ClassDB::bind_method(D_METHOD("set_head_damage", "m_HeadDamage"), &BoneCollisionShape::set_head_damage); + ADD_PROPERTY(PropertyInfo(Variant::INT, "m_HeadDamage"), "set_head_damage", "get_head_damage"); - ClassDB::bind_method(D_METHOD("get_body_damage"), &BoneCollisionShape::get_body_damage); - ClassDB::bind_method(D_METHOD("set_body_damage", "m_BodyDamage"), &BoneCollisionShape::set_body_damage); - ADD_PROPERTY(PropertyInfo(Variant::INT, "m_BodyDamage"), "set_head_damage", "get_body_damage"); + ClassDB::bind_method(D_METHOD("get_body_damage"), &BoneCollisionShape::get_body_damage); + ClassDB::bind_method(D_METHOD("set_body_damage", "m_BodyDamage"), &BoneCollisionShape::set_body_damage); + ADD_PROPERTY(PropertyInfo(Variant::INT, "m_BodyDamage"), "set_head_damage", "get_body_damage"); ADD_SIGNAL(MethodInfo("a_ZombieHeadShotHit", PropertyInfo(Variant::INT, "dam"))); ADD_SIGNAL(MethodInfo("a_ZombieBodyShotHit", PropertyInfo(Variant::INT, "dam"))); - ClassDB::bind_method(D_METHOD("headshot_hit"), &BoneCollisionShape::headshot_hit); - ClassDB::bind_method(D_METHOD("bodyshot_hit"), &BoneCollisionShape::bodyshot_hit); + ClassDB::bind_method(D_METHOD("headshot_hit"), &BoneCollisionShape::headshot_hit); + ClassDB::bind_method(D_METHOD("bodyshot_hit"), &BoneCollisionShape::bodyshot_hit); } BoneCollisionShape::BoneCollisionShape() {} @@ -25,36 +27,42 @@ BoneCollisionShape::BoneCollisionShape() {} BoneCollisionShape::~BoneCollisionShape() {} void BoneCollisionShape::_ready() { - m_Crosshair = Object::cast_to(get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->find_child("Crosshair")); - DEV_ASSERT(m_Crosshair); +#if DEBUG_ENABLED + if (Engine::get_singleton()->is_editor_hint()) { + return; + } +#endif + m_Crosshair = Object::cast_to( + get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->find_child("Crosshair")); + DEV_ASSERT(m_Crosshair); } void BoneCollisionShape::set_head_damage(int a_D) { - m_HeadDamage = a_D; + m_HeadDamage = a_D; } int BoneCollisionShape::get_head_damage() const { - return m_HeadDamage; + return m_HeadDamage; } void BoneCollisionShape::set_body_damage(int a_D) { - m_BodyDamage = a_D; + m_BodyDamage = a_D; } int BoneCollisionShape::get_body_damage() const { - return m_BodyDamage; + return m_BodyDamage; } void BoneCollisionShape::headshot_hit() { - ERR_PRINT("zombie headshot hit"); - m_Crosshair->set_dot_color(Color(1,0,0,1)); - emit_signal("a_ZombieHeadShotHit", m_HeadDamage); + ERR_PRINT("zombie headshot hit"); + m_Crosshair->set_dot_color(Color(1, 0, 0, 1)); + emit_signal("a_ZombieHeadShotHit", m_HeadDamage); } void BoneCollisionShape::bodyshot_hit() { - ERR_PRINT("zombie body hit"); - m_Crosshair->set_dot_color(Color(0,0,1,1)); - emit_signal("a_ZombieBodyShotHit", m_BodyDamage); + ERR_PRINT("zombie body hit"); + m_Crosshair->set_dot_color(Color(0, 0, 1, 1)); + emit_signal("a_ZombieBodyShotHit", m_BodyDamage); } } // namespace blitz \ No newline at end of file diff --git a/src/Bullet.cpp b/src/Bullet.cpp index 2cbaf41..65734ed 100644 --- a/src/Bullet.cpp +++ b/src/Bullet.cpp @@ -1,6 +1,8 @@ #include "Bullet.h" #include "BoneCollisionShape.h" +#include + using namespace godot; namespace blitz { @@ -25,6 +27,11 @@ void Bullet::_ready() { m_Timer = memnew(Timer); add_child(m_Timer); m_Timer->connect("timeout", callable_mp(this, &Bullet::_on_timer_timeout)); +#if DEBUG_ENABLED + if (Engine::get_singleton()->is_editor_hint()) { + return; + } +#endif m_Crosshair = Object::cast_to(get_parent()->find_child("Crosshair")); DEV_ASSERT(m_Crosshair); } diff --git a/src/Crosshair.cpp b/src/Crosshair.cpp index 2f23961..1d57586 100644 --- a/src/Crosshair.cpp +++ b/src/Crosshair.cpp @@ -15,8 +15,19 @@ Crosshair::Crosshair() : Crosshair::~Crosshair() {} void Crosshair::_ready() { - m_Player = Object::cast_to(get_parent()->get_parent()->get_parent()->find_child("FirstPersonPlayer")); - DEV_ASSERT(m_Player); + // Catch the parent node directly without SubViewport problem in the editor thx to "recursivity" (if we go up the tree one more + // notch its bugging) + Node* root_node = get_parent(); + while (root_node != nullptr && !root_node->is_class("FirstPersonPlayer")) { + root_node = root_node->get_parent(); + } + if (root_node != nullptr && root_node->is_class("FirstPersonPlayer")) { + m_Player = Object::cast_to(root_node); + DEV_ASSERT(m_Player); + } else { + ERR_PRINT("FirstPersonPlayer not found in parent hierarchy."); + } + m_CrosshairLines = {Object::cast_to(find_child("Top")), Object::cast_to(find_child("Right")), Object::cast_to(find_child("Bottom")), Object::cast_to(find_child("Left"))}; for (size_t i = 0; i < m_CrosshairLines.size(); i++) {