Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blackboard #50

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions addons/btree/Runtime/runtime.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum Status {
}

class TNode:
var blackboard: Node
var name:String
var status: int = Status.RUNNING
var ticked := false
Expand Down Expand Up @@ -531,20 +532,21 @@ static func get_constructors() -> Dictionary:
constructors[TNodeTypes.RANDOM_WAIT] = RandomWaitNode
return constructors

static func create_runtime(data: Dictionary, target) -> TNode:
static func create_runtime(data: Dictionary, target, blackboard) -> TNode:
if data.empty():
return null

if data.type == TNodeTypes.MINIM:
return create_runtime(data.data.data.root, target)
return create_runtime(data.data.data.root, target, blackboard)

var tnode_type = get_constructors().get(data.type)
var current: TNode = tnode_type.new()
current.name = data.name
current.blackboard = blackboard
current.setup(data.data, target)

for child in data.child:
var tnode_child = create_runtime(child, target)
var tnode_child = create_runtime(child, target, blackboard)
if current is CompositeTNode:
current.children.append(tnode_child)
elif current is DecoratorTNode:
Expand Down
72 changes: 72 additions & 0 deletions addons/btree/blackboard.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class_name Blackboard
extends Node

# Author: GabrieleTorini (https://github.com/GabrieleTorini)
# https://github.com/GabrieleTorini/godot-behavior-tree/blob/main/addons/behavior_tree/src/blackboard.gd

# This is the database where all your variables go. Here you keep track of
# whether the player is nearby, your health is low, there is a cover available,
# or whatever.
#
# You just store the data here and use it for condition checks in BTCondition scripts,
# or as arguments for your function calls in BTAction.
#
# This is a good way to separate data from behavior, which is essential
# to avoid nasty bugs.

export(Dictionary) var _data: Dictionary

var data: Dictionary



func _enter_tree():
data = _data.duplicate()


func _ready():
for key in data.keys():
assert(key is String)


# Will return false if it's overwritting an existing value
func _set(key: String, value) -> bool:
var return_val = !has(key)
data[key] = value
return return_val



func get(key: String):
if data.has(key):
var value = data[key]
if value is NodePath:
if value.is_empty() or not get_tree().get_root().has_node(value):
data[key] = null
return null
else:
return get_node(value) # If you store NodePaths, will return the Node.
else:
return value


func has(key: String) -> bool:
return data.has(key)

func unset(key: String) -> bool:
var val = data.has(key)
data.erase(key)
return val

# Alias functions
func add(key: String, value):
_set(key, value)

func clear(key: String) -> bool:
return unset(key)

func remove(key: String) -> bool:
return unset(key)

func delete(key: String) -> bool:
return unset(key)
139 changes: 139 additions & 0 deletions addons/btree/icons/icon_blackboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions addons/btree/init.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ extends EditorPlugin

var btree = load("res://addons/btree/script/btree.gd")
var ibtree = load("res://addons/btree/icons/icon_tree.svg")
var blackboard = load("res://addons/btree/blackboard.gd")
var iblackboard = load("res://addons/btree/icons/icon_blackboard.svg")
var topi = load("res://addons/btree/icons/icon_tree_top.svg")
var dock_scene = preload("res://addons/btree/Editor/test.tscn")
var dock
Expand Down Expand Up @@ -77,6 +79,7 @@ func _enter_tree():
dock = dock_scene.instance()
dock.halt("Please select a BTREE node")
add_custom_type("BTREE", "Node", btree, ibtree)
add_custom_type("Blackboard", "Node", blackboard, iblackboard)
get_editor_interface().get_editor_viewport().add_child(dock)
get_editor_interface().get_selection().connect("selection_changed",self,"selection_changed");
var ps = ProjectSettings
Expand Down
4 changes: 3 additions & 1 deletion addons/btree/script/btree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(bool) var enable = true
export(int, '_process', '_physics_process') var run_on = 0
export(int, "resume", "restart") var on_enable = 0
export(String) var _tree_id = ""
export(NodePath) var _blackboard
var rtree: Runtime.TNode = null
var swap_tree

Expand All @@ -31,7 +32,8 @@ func _enter_tree():
return

func create_runtime():
rtree = Runtime.create_runtime(tree.get('root', {}), get_parent())
var blackboard: Blackboard = get_node(_blackboard)
rtree = Runtime.create_runtime(tree.get('root', {}), get_parent(), blackboard)
return

func swap_runtime(new_tree):
Expand Down