Skip to content

Commit

Permalink
Fix Blackboard class (#53)
Browse files Browse the repository at this point in the history
* Fix icons in Godot 4

Fixes #49 and ports new icon design from godot-3.x to godot-4.x.

* Disable bootsplash fullsize

* Fix Blackboard class
  • Loading branch information
Wichamir authored Nov 13, 2022
1 parent 2a4dca5 commit 7e2388a
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 34 deletions.
17 changes: 9 additions & 8 deletions addons/beehave/blackboard.gd
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
extends Object
class_name Blackboard
var blackboard = {}
class_name Blackboard extends RefCounted

func set(key, value, blackboard_name = 'default'):
var blackboard: Dictionary = {}


func set_value(key: Variant, value: Variant, blackboard_name: String = 'default') -> void:
if not blackboard.has(blackboard_name):
blackboard[blackboard_name] = {}

blackboard[blackboard_name][key] = value


func get(key, default_value = null, blackboard_name = 'default'):
if has(key, blackboard_name):
func get_value(key: Variant, default_value: Variant = null, blackboard_name: String = 'default') -> Variant:
if has_value(key, blackboard_name):
return blackboard[blackboard_name].get(key, default_value)
return default_value


func has(key, blackboard_name = 'default'):
func has_value(key: Variant, blackboard_name: String = 'default') -> bool:
return blackboard.has(blackboard_name) and blackboard[blackboard_name].has(key) and blackboard[blackboard_name][key] != null


func erase(key, blackboard_name = 'default'):
func erase_value(key: Variant, blackboard_name: String = 'default') -> void:
if blackboard.has(blackboard_name):
blackboard[blackboard_name][key] = null
19 changes: 9 additions & 10 deletions addons/beehave/nodes/beehave_root.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class_name BeehaveRoot extends BeehaveTree
@icon("../icons/tree.svg")

var Blackboard = load("res://addons/beehave/blackboard.gd")
const SUCCESS = 0
const FAILURE = 1
const RUNNING = 2
Expand All @@ -12,7 +11,7 @@ const RUNNING = 2

var actor : Node

@onready var blackboard = Blackboard.new()
@onready var blackboard: Blackboard = Blackboard.new()

func _ready():
if self.get_child_count() != 1:
Expand All @@ -27,26 +26,26 @@ func _ready():
set_physics_process(enabled)

func _physics_process(delta):
blackboard.set("delta", delta)
blackboard.set_value("delta", delta)

var status = self.get_child(0).tick(actor, blackboard)

if status != RUNNING:
blackboard.set("running_action", null)
blackboard.set_value("running_action", null)

func get_running_action():
if blackboard.has("running_action"):
return blackboard.get("running_action")
if blackboard.has_value("running_action"):
return blackboard.get_value("running_action")
return null

func get_last_condition():
if blackboard.has("last_condition"):
return blackboard.get("last_condition")
if blackboard.has_value("last_condition"):
return blackboard.get_value("last_condition")
return null

func get_last_condition_status():
if blackboard.has("last_condition_status"):
var status = blackboard.get("last_condition_status")
if blackboard.has_value("last_condition_status"):
var status = blackboard.get_value("last_condition_status")
if status == SUCCESS:
return "SUCCESS"
elif status == FAILURE:
Expand Down
6 changes: 3 additions & 3 deletions addons/beehave/nodes/composites/selector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ func tick(actor, blackboard):
var response = c.tick(actor, blackboard)

if c is ConditionLeaf:
blackboard.set("last_condition", c)
blackboard.set("last_condition_status", response)
blackboard.set_value("last_condition", c)
blackboard.set_value("last_condition_status", response)

if response != FAILURE:
if c is ActionLeaf and response == RUNNING:
blackboard.set("running_action", c)
blackboard.set_value("running_action", c)
return response

return FAILURE
6 changes: 3 additions & 3 deletions addons/beehave/nodes/composites/selector_star.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func tick(actor, blackboard):
var response = c.tick(actor, blackboard)

if c is ConditionLeaf:
blackboard.set("last_condition", c)
blackboard.set("last_condition_status", response)
blackboard.set_value("last_condition", c)
blackboard.set_value("last_condition_status", response)

if response != FAILURE:
if c is ActionLeaf and response == RUNNING:
blackboard.set("running_action", c)
blackboard.set_value("running_action", c)
if response == SUCCESS:
last_execution_index = 0
return response
Expand Down
6 changes: 3 additions & 3 deletions addons/beehave/nodes/composites/sequence.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ func tick(actor, blackboard):
var response = c.tick(actor, blackboard)

if c is ConditionLeaf:
blackboard.set("last_condition", c)
blackboard.set("last_condition_status", response)
blackboard.set_value("last_condition", c)
blackboard.set_value("last_condition_status", response)

if response != SUCCESS:
if c is ActionLeaf and response == RUNNING:
blackboard.set("running_action", c)
blackboard.set_value("running_action", c)
return response

return SUCCESS
6 changes: 3 additions & 3 deletions addons/beehave/nodes/composites/sequence_star.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func tick(actor, blackboard):
var response = c.tick(actor, blackboard)

if c is ConditionLeaf:
blackboard.set("last_condition", c)
blackboard.set("last_condition_status", response)
blackboard.set_value("last_condition", c)
blackboard.set_value("last_condition_status", response)

if response != SUCCESS:
if response == FAILURE:
successful_index = 0
if c is ActionLeaf and response == RUNNING:
blackboard.set("running_action", c)
blackboard.set_value("running_action", c)
return response
else:
successful_index += 1
Expand Down
2 changes: 1 addition & 1 deletion addons/beehave/nodes/decorators/inverter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ func tick(action, blackboard):
return SUCCESS

if c is Leaf and response == RUNNING:
blackboard.set("running_action", c)
blackboard.set_value("running_action", c)
return RUNNING
4 changes: 2 additions & 2 deletions addons/beehave/nodes/decorators/limiter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class_name LimiterDecorator
@export var max_count : float = 0

func tick(actor, blackboard):
var current_count = blackboard.get(cache_key)
var current_count = blackboard.get_value(cache_key)

if current_count == null:
current_count = 0

if current_count <= max_count:
blackboard.set(cache_key, current_count + 1)
blackboard.set_value(cache_key, current_count + 1)
return self.get_child(0).tick(actor, blackboard)
else:
return FAILED
3 changes: 2 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ _global_script_classes=[{
"language": &"GDScript",
"path": "res://addons/beehave/nodes/beehave_tree.gd"
}, {
"base": "Object",
"base": "RefCounted",
"class": &"Blackboard",
"language": &"GDScript",
"path": "res://addons/beehave/blackboard.gd"
Expand Down Expand Up @@ -119,6 +119,7 @@ _global_script_class_icons={
config/name="Beehave"
config/features=PackedStringArray("4.0")
boot_splash/image="res://splash.png"
boot_splash/fullsize=false
config/icon="res://icon.png"

[editor_plugins]
Expand Down

0 comments on commit 7e2388a

Please sign in to comment.