Skip to content

Commit c2272b7

Browse files
committed
CharacterSpriteBehavior: Automatically assign character
In the same way as we assign sprite if not set and the parent is an AnimatedSprite2D, do it to the parent's parent if it is a CharacterBody2D. The rationale is that usually this is what you'll have in the Scene Tree, while at the same time it keeps being configurable. Also update the format in the existing documentation comment.
1 parent 6005117 commit c2272b7

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

scenes/game_logic/character_sprite_behavior.gd

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ extends Node2D
99
## to the velocity of [member character].
1010

1111
## The [member CharacterBody2D.velocity] is used to change the [member sprite].
12+
## [br][br]
13+
## [b]Note:[/b] If the grandparent node is a CharacterBody2D and this isn't set,
14+
## the grandparent node will be automatically assigned to this variable.
1215
@export var character: CharacterBody2D
1316

1417
## Whether to play the sprite animations or not. If not, the only thing that will happen is that
1518
## the sprite will be flipped horizontally according to the velocity of [member character].
1619
## Use this when using more advanced animation through an AnimationPlayer node.
1720
@export var play_animations: bool = true
1821

19-
## The controlled sprite.[br][br]
20-
##
21-
## [b]Note:[/b] If the parent node is a AnimatedSprite2D and sprite isn't set,
22+
## The controlled sprite.
23+
## [br][br]
24+
## [b]Note:[/b] If the parent node is a AnimatedSprite2D and this isn't set,
2225
## the parent node will be automatically assigned to this variable.
2326
@export var sprite: AnimatedSprite2D:
2427
set = _set_sprite
@@ -29,6 +32,8 @@ var _is_character_running: bool = false
2932
func _enter_tree() -> void:
3033
if not sprite and get_parent() is AnimatedSprite2D:
3134
sprite = get_parent()
35+
if not character and get_parent() and get_parent().get_parent() is CharacterBody2D:
36+
character = get_parent().get_parent()
3237

3338

3439
func _set_sprite(new_sprite: AnimatedSprite2D) -> void:

0 commit comments

Comments
 (0)