2023-08-08 13:06:01 +00:00
/**
* bt_check_var . cpp
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2024-03-04 11:49:09 +00:00
* Copyright 2021 - 2024 Serhii Snitsaruk
2023-08-08 13:06:01 +00:00
*
* Use of this source code is governed by an MIT - style
* license that can be found in the LICENSE file or at
* https : //opensource.org/licenses/MIT.
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
# include "bt_check_var.h"
2024-03-04 11:49:09 +00:00
void BTCheckVar : : set_variable ( const StringName & p_variable ) {
2023-08-08 13:06:01 +00:00
variable = p_variable ;
emit_changed ( ) ;
}
2023-08-10 11:12:13 +00:00
void BTCheckVar : : set_check_type ( LimboUtility : : CheckType p_check_type ) {
2023-08-08 13:06:01 +00:00
check_type = p_check_type ;
emit_changed ( ) ;
}
2024-03-04 11:49:09 +00:00
void BTCheckVar : : set_value ( const Ref < BBVariant > & p_value ) {
2023-08-08 13:06:01 +00:00
value = p_value ;
emit_changed ( ) ;
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) & & value . is_valid ( ) ) {
2024-01-09 12:42:54 +00:00
value - > connect ( LW_NAME ( changed ) , Callable ( this , LW_NAME ( emit_changed ) ) ) ;
2023-08-08 13:06:01 +00:00
}
}
2024-01-06 23:47:46 +00:00
PackedStringArray BTCheckVar : : get_configuration_warnings ( ) {
2023-08-15 15:05:30 +00:00
PackedStringArray warnings = BTCondition : : get_configuration_warnings ( ) ;
2024-03-04 11:49:09 +00:00
if ( variable = = StringName ( ) ) {
2023-08-15 15:05:30 +00:00
warnings . append ( " `variable` should be assigned. " ) ;
2023-08-08 13:06:01 +00:00
}
if ( ! value . is_valid ( ) ) {
2023-08-15 15:05:30 +00:00
warnings . append ( " `value` should be assigned. " ) ;
2023-08-08 13:06:01 +00:00
}
2023-08-15 15:05:30 +00:00
return warnings ;
2023-08-08 13:06:01 +00:00
}
2024-01-06 23:47:46 +00:00
String BTCheckVar : : _generate_name ( ) {
2024-03-04 11:49:09 +00:00
if ( variable = = StringName ( ) ) {
2023-08-08 13:06:01 +00:00
return " CheckVar ??? " ;
}
2023-08-10 11:12:13 +00:00
return vformat ( " Check if: %s %s %s " , LimboUtility : : get_singleton ( ) - > decorate_var ( variable ) ,
LimboUtility : : get_singleton ( ) - > get_check_operator_string ( check_type ) ,
2023-08-08 13:06:01 +00:00
value . is_valid ( ) ? Variant ( value ) : Variant ( " ??? " ) ) ;
}
2023-09-19 11:43:26 +00:00
BT : : Status BTCheckVar : : _tick ( double p_delta ) {
2024-03-04 11:49:09 +00:00
ERR_FAIL_COND_V_MSG ( variable = = StringName ( ) , FAILURE , " BTCheckVar: `variable` is not set. " ) ;
2023-08-10 11:12:13 +00:00
ERR_FAIL_COND_V_MSG ( ! value . is_valid ( ) , FAILURE , " BTCheckVar: `value` is not set. " ) ;
2023-08-08 13:06:01 +00:00
2023-08-10 11:12:13 +00:00
ERR_FAIL_COND_V_MSG ( ! get_blackboard ( ) - > has_var ( variable ) , FAILURE , vformat ( " BTCheckVar: Blackboard variable doesn't exist: \" %s \" . Returning FAILURE. " , variable ) ) ;
2023-08-08 13:06:01 +00:00
Variant left_value = get_blackboard ( ) - > get_var ( variable , Variant ( ) ) ;
2024-05-01 21:32:44 +00:00
Variant right_value = value - > get_value ( get_scene_root ( ) , get_blackboard ( ) ) ;
2023-08-08 13:06:01 +00:00
2023-08-10 11:12:13 +00:00
return LimboUtility : : get_singleton ( ) - > perform_check ( check_type , left_value , right_value ) ? SUCCESS : FAILURE ;
2023-08-08 13:06:01 +00:00
}
void BTCheckVar : : _bind_methods ( ) {
2024-03-04 20:36:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_variable " , " variable " ) , & BTCheckVar : : set_variable ) ;
2023-08-08 13:06:01 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_variable " ) , & BTCheckVar : : get_variable ) ;
2024-03-04 20:36:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_check_type " , " check_type " ) , & BTCheckVar : : set_check_type ) ;
2023-08-08 13:06:01 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_check_type " ) , & BTCheckVar : : get_check_type ) ;
2024-03-04 20:36:16 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_value " , " value " ) , & BTCheckVar : : set_value ) ;
2023-08-08 13:06:01 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_value " ) , & BTCheckVar : : get_value ) ;
2024-03-04 11:49:09 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : STRING_NAME , " variable " ) , " set_variable " , " get_variable " ) ;
2023-08-08 13:06:01 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " check_type " , PROPERTY_HINT_ENUM , " Equal,Less Than,Less Than Or Equal,Greater Than,Greater Than Or Equal,Not Equal " ) , " set_check_type " , " get_check_type " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : OBJECT , " value " , PROPERTY_HINT_RESOURCE_TYPE , " BBVariant " ) , " set_value " , " get_value " ) ;
}