Skip to content

Commit

Permalink
deps: add custom runner
Browse files Browse the repository at this point in the history
  • Loading branch information
russmatney committed Mar 24, 2024
1 parent ce5dc4a commit fc073f9
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
20 changes: 20 additions & 0 deletions addons/CustomRunner/Config.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extends CustomRunner

## The shortcut that will trigger the plugin.
func _init() -> void:
SHORTCUT = KEY_F7

## If true, pressing the shortcut will invoke CustomRunner for that scene.
func _can_play_scene(scene: Node) -> bool:
return true

## Add variables that will be passed to the game.
## Variable "scene", containing currently opened scene's path, is added automatically.
func _gather_variables(scene: Node):
if scene is CanvasItem:
add_variable("mouse_pos", scene.get_local_mouse_position())

## Return the path of the "game" scene of your project (i.e. main gameplay scene).
## If you return empty string, the current scene will play instead.
func _get_game_scene(for_scene: Node) -> String:
return ""
74 changes: 74 additions & 0 deletions addons/CustomRunner/CustomRunner.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@tool
extends Node
class_name CustomRunner

## Custom Runner main script. You can customize it by modifying the Config.gd file.

## The shortcut that will trigger the plugin.
var SHORTCUT = KEY_F7

## If true, pressing the shortcut will invoke CustomRunner for that scene.
func _can_play_scene(scene: Node) -> bool:
return true

## Add variables that will be passed to the game.
## Variable "scene", containing currently opened scene's path, is added automatically.
func _gather_variables(scene: Node):
pass

## Return the path of the "game" scene of your project (i.e. main gameplay scene).
## If you return empty string, the current scene will play instead.
func _get_game_scene(for_scene: Node) -> String:
return ""

## Returns true if the game was ran via CustomRunner.
static func is_custom_running() -> bool:
return not OS.get_environment("__custom_runner_data__").is_empty()

## Retrieves a passed variable's value.
static func get_variable(variable: String):
assert(is_custom_running(), "Can't retrieve data if not running via plugin.")
var data: Dictionary = str_to_var(OS.get_environment("__custom_runner_data__"))
return data[variable]

var plugin: Node
var data: Dictionary
var prev_game_scene: String

func _unhandled_key_input(event: InputEvent):
if plugin.get_editor_interface().is_playing_scene():
return

if event is InputEventKey and event.pressed and event.keycode == SHORTCUT:
if event.shift_pressed:
if data.is_empty() or prev_game_scene.is_empty():
push_warning("CustomRunner: Can't do Quick Run before running mormally at least once.")
return
else:
var root: Node = plugin.get_editor_interface().get_edited_scene_root()
if not _can_play_scene(root):
push_warning("CustomRunner: Invalid scene to play.")
return

data.clear()
add_variable("scene", root.scene_file_path)
_gather_variables(root)

var game_scene := _get_game_scene(root)
if game_scene.is_empty():
game_scene = root.scene_file_path

prev_game_scene = game_scene

OS.set_environment("__custom_runner_data__", var_to_str(data))
plugin.get_editor_interface().play_custom_scene(prev_game_scene)
OS.set_environment("__custom_runner_data__", "")
get_viewport().set_input_as_handled()

## Adds a variable to be passed to the running game. Use in [method _gather_variables].
func add_variable(variable: String, value):
if value is Object:
push_error("The value can be non-Object only.")
return

data[variable] = value
7 changes: 7 additions & 0 deletions addons/CustomRunner/Plugin.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@tool
extends EditorPlugin

func _enter_tree():
var runner := preload("Config.gd").new()
runner.plugin = self
add_child(runner)
7 changes: 7 additions & 0 deletions addons/CustomRunner/plugin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[plugin]

name="Custom Project Runner"
description="Configure how you run your project and pass data from editor."
author="KoBeWi"
version="1.3.1"
script="Plugin.gd"
1 change: 1 addition & 0 deletions plug.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ func _plugging():
plug("viniciusgerevini/godot-aseprite-wizard", {include=["addons/AsepriteWizard"]})
plug("russmatney/log.gd", {include=["addons/log"]})
plug("KoBeWi/Metroidvania-System", {include=["addons/MetroidvaniaSystem"]})
plug("KoBeWi/Godot-Custom-Runner", {include=["addons/CustomRunner"], exclude=["ExampleProject"]})
4 changes: 3 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Metro="*res://addons/metro/Metro.gd"
Records="*res://src/dino/Records.gd"
Dino="*res://src/dino/Dino.gd"
MetSys="*res://addons/MetroidvaniaSystem/Nodes/Singleton.tscn"
InputHelper="*res://addons/input_helper/input_helper.gd"
SoundManager="*res://addons/sound_manager/sound_manager.gd"

[display]

Expand All @@ -55,7 +57,7 @@ export/convert_text_resources_to_binary=false

[editor_plugins]

enabled=PackedStringArray("res://addons/AsepriteWizard/plugin.cfg", "res://addons/MetroidvaniaSystem/EditorExtension/plugin.cfg", "res://addons/MetroidvaniaSystem/plugin.cfg", "res://addons/beehive/plugin.cfg", "res://addons/brick/plugin.cfg", "res://addons/camera/plugin.cfg", "res://addons/core/plugin.cfg", "res://addons/custom-scene-launcher/plugin.cfg", "res://addons/dj/plugin.cfg", "res://addons/gdfxr/plugin.cfg", "res://addons/hood/plugin.cfg", "res://addons/hotel/plugin.cfg", "res://addons/metro/plugin.cfg", "res://addons/navi/plugin.cfg", "res://addons/pandora/plugin.cfg", "res://addons/quest/plugin.cfg", "res://addons/reptile/plugin.cfg", "res://addons/thanks/plugin.cfg", "res://addons/trolley/plugin.cfg")
enabled=PackedStringArray("res://addons/AsepriteWizard/plugin.cfg", "res://addons/CustomRunner/plugin.cfg", "res://addons/MetroidvaniaSystem/EditorExtension/plugin.cfg", "res://addons/MetroidvaniaSystem/plugin.cfg", "res://addons/beehive/plugin.cfg", "res://addons/brick/plugin.cfg", "res://addons/camera/plugin.cfg", "res://addons/core/plugin.cfg", "res://addons/custom-scene-launcher/plugin.cfg", "res://addons/dj/plugin.cfg", "res://addons/gd-plug-ui/plugin.cfg", "res://addons/gdfxr/plugin.cfg", "res://addons/hood/plugin.cfg", "res://addons/hotel/plugin.cfg", "res://addons/input_helper/plugin.cfg", "res://addons/log/plugin.cfg", "res://addons/metro/plugin.cfg", "res://addons/navi/plugin.cfg", "res://addons/pandora/plugin.cfg", "res://addons/quest/plugin.cfg", "res://addons/reptile/plugin.cfg", "res://addons/sound_manager/plugin.cfg", "res://addons/thanks/plugin.cfg", "res://addons/trolley/plugin.cfg")

[gdunit4]

Expand Down

0 comments on commit fc073f9

Please sign in to comment.