Skip to content

Commit

Permalink
feat: access current scene by current
Browse files Browse the repository at this point in the history
refers to: #110
  • Loading branch information
Ark2000 committed May 25, 2023
1 parent 01f5b10 commit f94d53f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
21 changes: 19 additions & 2 deletions addons/panku_console/console.gd
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ var _envs = {}
var _envs_info = {}
var _expression = Expression.new()

# The current scene root node, which will be updated automatically when the scene changes.
var _current_scene_root:Node

## Register an environment that run expressions.
## [br][code]env_name[/code]: the name of the environment
## [br][code]env[/code]: The base instance that runs the expressions. For exmaple your player node.
Expand Down Expand Up @@ -119,7 +122,7 @@ func remove_env(env_name:String):
for k in _envs_info.keys():
if k.begins_with(env_name + "."):
_envs_info.erase(k)
notify("[color=green][Info][/color] [b]%s[/b] env unloaded!"%env_name)
#notify("[color=green][Info][/color] [b]%s[/b] env unloaded!"%env_name)

## Generate a notification
func notify(any) -> void:
Expand Down Expand Up @@ -208,7 +211,7 @@ func show_intro():
output("")
output("[b][color=#478cbf]❤️Contributors[/color][/b]: 🔗[url=https://github.com/Ark2000]Ark2000(Feo Wu)[/url], 🔗[url=https://github.com/scriptsengineer]scriptengineer(Rafael Correa)[/url], 🔗[url=https://github.com/winston-yallow]winston-yallow(Winston)[/url], 🔗[url=https://github.com/CheapMeow]CheapMeow[/url].")
output("")
output("> Hello, type [b]help[/b] for help")
output("> Tips: you can always access current scene root by `[b]current[/b]`.")
output("")

func open_expression_key_mapper():
Expand Down Expand Up @@ -268,11 +271,25 @@ func _ready():

load_data()

setup_scene_root_tracker()

func _notification(what):
#quit event
if what == NOTIFICATION_WM_CLOSE_REQUEST:
save_data()

# always register the current scene root as `current`
func setup_scene_root_tracker():
_current_scene_root = get_tree().root.get_child(get_tree().root.get_child_count() - 1)
register_env("current", _current_scene_root)
create_tween().set_loops().tween_callback(
func():
var r = get_tree().root.get_child(get_tree().root.get_child_count() - 1)
if r != _current_scene_root:
_current_scene_root = r
register_env("current", _current_scene_root)
).set_delay(0.1)

func load_data():
#load configs
var cfg = Config.get_config()
Expand Down
4 changes: 0 additions & 4 deletions addons/panku_console/demo/demo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ var logo_dir = Vector2(1, 1)
func random_seed():
seed = randi()

func _ready():
#That's the way you add some stuff to the console plugin.
console.register_env("demo", self)

func _process(delta):
var r = get_viewport_rect().size
if (_logo.position.x + _logo.size.x > r.x) or (_logo.position.x < 0):
Expand Down
2 changes: 1 addition & 1 deletion addons/panku_console/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="PankuConsole"
description="All-in-One Godot Engine runtime debugging tool."
author="Feo (k2kra) Wu"
version="1.4.105"
version="1.4.106"
script="plugin.gd"
1 change: 1 addition & 0 deletions docs/builtin_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The built-in commands are defined in the `addons/panku_console/default_repl_envs

| Command | Description |
| --- | --- |
| `current` | The current scene root, which will be automatically updated |
| `help` | List all environment variables |
| `helpe(obj:Object)` | Provide detailed information about one specific environment variable |
| `console.cls` | Clear interactive shell's output |
Expand Down
8 changes: 7 additions & 1 deletion docs/interactive_shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@ Now, you can type `player.hello("Jason")` in the REPL console to call the functi

Any methods or properties in the script that **do not begin with an underscore** will be added to the REPL console's auto-completion system (they will still be accessible, of course). Optionally, you can add description information to a method or property by defining a string constant named `_HELP_` + the corresponding method or property name.

Alternatively, the plugin's predefined registered objects can be viewed in the `default_repl_envs` folder in the plugin directory.
Alternatively, the plugin's predefined registered objects can be viewed in the `default_repl_envs` folder in the plugin directory.

## Access Scene Root

From `1.4.106`, your current scene root (the last child of `get_tre().root`) will be automatically registered and updated as `current` in the expression execution environment. This means that you can access anything in your scene root script directly in the expression, which is very convenient and you don't need to register something manually.

For example, if you have a `player` variable in your scene root script, you can access it directly in the expression by typing `current.player` or `current.player.hp`.

0 comments on commit f94d53f

Please sign in to comment.