Skip to content

Commit

Permalink
Added the cache to the visibility nodes and the cache check to the no…
Browse files Browse the repository at this point in the history
…de reference bt node reset method.
  • Loading branch information
JarkkoPar committed Jan 5, 2024
1 parent e0aaedf commit 6b6a1ef
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/agent_behaviours/sensors/area2d_visibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,18 @@ void UtilityAIArea2DVisibilitySensor::_notification(int p_what) {

// Handling functions.

void UtilityAIArea2DVisibilitySensor::_update_cache() {
_cache = ObjectID();
if( _visibility_volume != nullptr ) {
_cache = _visibility_volume->get_instance_id();
}
}

void UtilityAIArea2DVisibilitySensor::initialize_sensor() {
if( !get_is_active() ) return;
if( Engine::get_singleton()->is_editor_hint() ) return;

ERR_FAIL_COND_MSG( _visibility_volume == nullptr, "UtilityAIArea2DVisibilitySensor::initialize_sensor() - Error, the node set as the Area2D is not of type Area2D.");
ERR_FAIL_COND_MSG( _visibility_volume == nullptr, "UtilityAIArea2DVisibilitySensor::initialize_sensor() - Error, the visibility volume is not set.");

// Connect to the area entered and exited signals.
Error error_visibility_volume_on_entered = _visibility_volume->connect("area_entered", Callable(this, "on_area_entered"));
Expand All @@ -163,6 +170,7 @@ void UtilityAIArea2DVisibilitySensor::initialize_sensor() {


void UtilityAIArea2DVisibilitySensor::uninitialize_sensor() {
if( Engine::get_singleton()->is_editor_hint() ) return;
if( _visibility_volume != nullptr ) {
_visibility_volume->disconnect("area_entered", Callable(this, "on_area_entered"));
_visibility_volume->disconnect("area_exited", Callable(this, "on_area_exited"));
Expand All @@ -176,6 +184,11 @@ float UtilityAIArea2DVisibilitySensor::evaluate_sensor_value() {
if( _visibility_volume == nullptr ) {
return get_sensor_value();
}
if( _cache.is_null() || !_cache.is_valid() ) {
_visibility_volume = nullptr; // Cache shows that the node reference has become invalid.
return get_sensor_value();
}


//Ref<World3D> w3d = _visibility_volume_node->get_world_3d();
//ERR_FAIL_COND_V(w3d.is_null(), get_sensor_value());
Expand Down Expand Up @@ -341,6 +354,7 @@ Vector2 UtilityAIArea2DVisibilitySensor::get_offset_vector2() const {
void UtilityAIArea2DVisibilitySensor::set_visibility_volume( Area2D* visibility_volume ) {
_has_sensor_value_changed = _has_sensor_value_changed || (_visibility_volume != visibility_volume);
_visibility_volume = visibility_volume;
_update_cache();
}


Expand Down
5 changes: 3 additions & 2 deletions src/agent_behaviours/sensors/area2d_visibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class UtilityAIArea2DVisibilitySensor : public UtilityAISensor {
GDCLASS(UtilityAIArea2DVisibilitySensor, UtilityAISensor )

private:
Area2D* _visibility_volume;
Area2D* _visibility_volume;
ObjectID _cache;

Vector2 _from_vector;
Vector2 _offset_vector;
Expand All @@ -37,7 +38,7 @@ class UtilityAIArea2DVisibilitySensor : public UtilityAISensor {

protected:
static void _bind_methods();

void _update_cache();
public:
UtilityAIArea2DVisibilitySensor();
~UtilityAIArea2DVisibilitySensor();
Expand Down
15 changes: 14 additions & 1 deletion src/agent_behaviours/sensors/area3d_visibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,18 @@ void UtilityAIArea3DVisibilitySensor::_notification(int p_what) {

// Handling functions.

void UtilityAIArea3DVisibilitySensor::_update_cache() {
_cache = ObjectID();
if( _visibility_volume != nullptr ) {
_cache = _visibility_volume->get_instance_id();
}
}

void UtilityAIArea3DVisibilitySensor::initialize_sensor() {
//if( !get_is_active() ) return;
if( Engine::get_singleton()->is_editor_hint() ) return;

ERR_FAIL_COND_MSG( _visibility_volume == nullptr, "UtilityAIArea3DVisibilitySensor::initialize_sensor() - Error, the node set as the Area3D is not of type Area3D.");
ERR_FAIL_COND_MSG( _visibility_volume == nullptr, "UtilityAIArea3DVisibilitySensor::initialize_sensor() - Error, the visibility volume is not set.");

// Connect to the area entered and exited signals.
Error error_visibility_volume_on_entered = _visibility_volume->connect("area_entered", Callable(this, "on_area_entered"));
Expand All @@ -158,6 +165,7 @@ void UtilityAIArea3DVisibilitySensor::initialize_sensor() {


void UtilityAIArea3DVisibilitySensor::uninitialize_sensor() {
if( Engine::get_singleton()->is_editor_hint() ) return;
if( _visibility_volume != nullptr ) {
_visibility_volume->disconnect("area_entered", Callable(this, "on_area_entered"));
_visibility_volume->disconnect("area_exited", Callable(this, "on_area_exited"));
Expand All @@ -170,6 +178,10 @@ float UtilityAIArea3DVisibilitySensor::evaluate_sensor_value() {
if( _visibility_volume == nullptr ) {
return get_sensor_value();
}
if( _cache.is_null() || !_cache.is_valid() ) {
_visibility_volume = nullptr; // Cache shows that the node reference has become invalid.
return get_sensor_value();
}

//Ref<World3D> w3d = _visibility_volume_node->get_world_3d();
//ERR_FAIL_COND_V(w3d.is_null(), get_sensor_value());
Expand Down Expand Up @@ -333,6 +345,7 @@ Vector3 UtilityAIArea3DVisibilitySensor::get_offset_vector3() const {
void UtilityAIArea3DVisibilitySensor::set_visibility_volume( Area3D* visibility_volume ) {
_has_sensor_value_changed = _has_sensor_value_changed || ( _visibility_volume != visibility_volume );
_visibility_volume = visibility_volume;
_update_cache();
}


Expand Down
6 changes: 4 additions & 2 deletions src/agent_behaviours/sensors/area3d_visibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class UtilityAIArea3DVisibilitySensor : public UtilityAISensor {
GDCLASS(UtilityAIArea3DVisibilitySensor, UtilityAISensor )

private:
Area3D* _visibility_volume;
Area3D* _visibility_volume;
ObjectID _cache;


Vector3 _from_vector;
Vector3 _offset_vector;
Expand All @@ -36,7 +38,7 @@ class UtilityAIArea3DVisibilitySensor : public UtilityAISensor {
bool _use_owner_global_position;
protected:
static void _bind_methods();

void _update_cache();
public:
UtilityAIArea3DVisibilitySensor();
~UtilityAIArea3DVisibilitySensor();
Expand Down
3 changes: 3 additions & 0 deletions src/behaviour_tree/node_reference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ void UtilityAIBTNodeReference::_update_cache() {


void UtilityAIBTNodeReference::reset() {
if( _cache.is_null() || !_cache.is_valid() ) {
return;
}
if(!_node_reference) {
return;
}
Expand Down

0 comments on commit 6b6a1ef

Please sign in to comment.