2022-08-28 20:58:30 +00:00
|
|
|
/* bt_probability.cpp */
|
|
|
|
|
|
|
|
#include "bt_probability.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/object/object.h"
|
2022-08-28 20:58:30 +00:00
|
|
|
|
|
|
|
String BTProbability::_generate_name() const {
|
|
|
|
return vformat("Probability %.1f%%", run_chance);
|
|
|
|
}
|
|
|
|
|
2023-04-10 08:08:11 +00:00
|
|
|
int BTProbability::_tick(double p_delta) {
|
2022-09-21 10:37:19 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child.");
|
2022-08-28 20:58:30 +00:00
|
|
|
if (get_child(0)->get_status() == RUNNING or Math::randf() <= run_chance) {
|
|
|
|
return get_child(0)->execute(p_delta);
|
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTProbability::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_run_chance", "p_value"), &BTProbability::set_run_chance);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_run_chance"), &BTProbability::get_run_chance);
|
2022-12-15 07:26:52 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "run_chance", PROPERTY_HINT_RANGE, "0.0,1.0"), "set_run_chance", "get_run_chance");
|
2022-08-28 20:58:30 +00:00
|
|
|
}
|