From 9d5b80705912d85c3c7301ac0ea0afbf9372a660 Mon Sep 17 00:00:00 2001 From: fabriceci Date: Mon, 11 Oct 2021 11:30:59 +0200 Subject: Improve editor template workflow Co-Authored-By: jmb462 --- modules/gdscript/SCsub | 2 + .../CharacterBody2D/basic_movement.gd | 29 ++++++ .../CharacterBody3D/basic_movement.gd | 32 +++++++ .../editor_templates/EditorPlugin/plugin.gd | 11 +++ .../EditorScript/basic_editor_script.gd | 7 ++ modules/gdscript/editor_templates/Node/default.gd | 11 +++ modules/gdscript/editor_templates/Object/empty.gd | 3 + modules/gdscript/editor_templates/SCsub | 16 ++++ modules/gdscript/gdscript.cpp | 14 ++- modules/gdscript/gdscript.h | 101 ++++++++++----------- modules/gdscript/gdscript_editor.cpp | 91 +++++++------------ modules/mono/SCsub | 2 + modules/mono/csharp_script.cpp | 65 +++++-------- modules/mono/csharp_script.h | 4 +- .../CharacterBody2D/basic_movement.cs | 41 +++++++++ .../CharacterBody3D/basic_movement.cs | 44 +++++++++ .../mono/editor_templates/EditorPlugin/plugin.cs | 19 ++++ .../EditorScript/basic_editor_script.cs | 14 +++ modules/mono/editor_templates/Node/default.cs | 19 ++++ modules/mono/editor_templates/Object/empty.cs | 9 ++ modules/mono/editor_templates/SCsub | 16 ++++ modules/visual_script/visual_script.cpp | 15 +-- modules/visual_script/visual_script.h | 79 ++++++++-------- 23 files changed, 438 insertions(+), 206 deletions(-) create mode 100644 modules/gdscript/editor_templates/CharacterBody2D/basic_movement.gd create mode 100644 modules/gdscript/editor_templates/CharacterBody3D/basic_movement.gd create mode 100644 modules/gdscript/editor_templates/EditorPlugin/plugin.gd create mode 100644 modules/gdscript/editor_templates/EditorScript/basic_editor_script.gd create mode 100644 modules/gdscript/editor_templates/Node/default.gd create mode 100644 modules/gdscript/editor_templates/Object/empty.gd create mode 100644 modules/gdscript/editor_templates/SCsub create mode 100644 modules/mono/editor_templates/CharacterBody2D/basic_movement.cs create mode 100644 modules/mono/editor_templates/CharacterBody3D/basic_movement.cs create mode 100644 modules/mono/editor_templates/EditorPlugin/plugin.cs create mode 100644 modules/mono/editor_templates/EditorScript/basic_editor_script.cs create mode 100644 modules/mono/editor_templates/Node/default.cs create mode 100644 modules/mono/editor_templates/Object/empty.cs create mode 100644 modules/mono/editor_templates/SCsub (limited to 'modules') diff --git a/modules/gdscript/SCsub b/modules/gdscript/SCsub index 5c8cbdf869..c6121ec7fe 100644 --- a/modules/gdscript/SCsub +++ b/modules/gdscript/SCsub @@ -21,3 +21,5 @@ if env["tools"]: if env["tests"]: env_gdscript.Append(CPPDEFINES=["TESTS_ENABLED"]) env_gdscript.add_source_files(env.modules_sources, "./tests/*.cpp") + +SConscript("editor_templates/SCsub") diff --git a/modules/gdscript/editor_templates/CharacterBody2D/basic_movement.gd b/modules/gdscript/editor_templates/CharacterBody2D/basic_movement.gd new file mode 100644 index 0000000000..0824d894c5 --- /dev/null +++ b/modules/gdscript/editor_templates/CharacterBody2D/basic_movement.gd @@ -0,0 +1,29 @@ +# meta-description: Classic movement for gravity games (platformer, ...) + +extends _BASE_ + +const SPEED: float = 300.0 +const JUMP_FORCE: float = -400.0 + +# Get the gravity from the project settings to be synced with RigidDynamicBody nodes. +var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity") + + +func _physics_process(delta: float) -> void: + # Add the gravity. + if not is_on_floor(): + motion_velocity.y += gravity * delta + + # Handle Jump. + if Input.is_action_just_pressed("ui_accept") and is_on_floor(): + motion_velocity.y = JUMP_FORCE + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var direction := Input.get_axis("ui_left", "ui_right") + if direction: + motion_velocity.x = direction * SPEED + else: + motion_velocity.x = move_toward(motion_velocity.x, 0, SPEED) + + move_and_slide() diff --git a/modules/gdscript/editor_templates/CharacterBody3D/basic_movement.gd b/modules/gdscript/editor_templates/CharacterBody3D/basic_movement.gd new file mode 100644 index 0000000000..ce6d67ae84 --- /dev/null +++ b/modules/gdscript/editor_templates/CharacterBody3D/basic_movement.gd @@ -0,0 +1,32 @@ +# meta-description: Classic movement for gravity games (FPS, TPS, ...) + +extends _BASE_ + +const SPEED: float = 5.0 +const JUMP_FORCE: float = 4.5 + +# Get the gravity from the project settings to be synced with RigidDynamicBody nodes. +var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity") + + +func _physics_process(delta: float) -> void: + # Add the gravity. + if not is_on_floor(): + motion_velocity.y -= gravity * delta + + # Handle Jump. + if Input.is_action_just_pressed("ui_accept") and is_on_floor(): + motion_velocity.y = JUMP_FORCE + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") + var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + motion_velocity.x = direction.x * SPEED + motion_velocity.z = direction.z * SPEED + else: + motion_velocity.x = move_toward(motion_velocity.x, 0, SPEED) + motion_velocity.z = move_toward(motion_velocity.z, 0, SPEED) + + move_and_slide() diff --git a/modules/gdscript/editor_templates/EditorPlugin/plugin.gd b/modules/gdscript/editor_templates/EditorPlugin/plugin.gd new file mode 100644 index 0000000000..8614bb8b17 --- /dev/null +++ b/modules/gdscript/editor_templates/EditorPlugin/plugin.gd @@ -0,0 +1,11 @@ +# meta-description: Basic plugin template +@tool +extends EditorPlugin + +func _enter_tree() -> void: + # Initialization of the plugin goes here. + pass + +func _exit_tree() -> void: + # Clean-up of the plugin goes here. + pass diff --git a/modules/gdscript/editor_templates/EditorScript/basic_editor_script.gd b/modules/gdscript/editor_templates/EditorScript/basic_editor_script.gd new file mode 100644 index 0000000000..fdb174c7ed --- /dev/null +++ b/modules/gdscript/editor_templates/EditorScript/basic_editor_script.gd @@ -0,0 +1,7 @@ +# meta-description: Basic editor script template +@tool +extends EditorScript + +func _run() -> void: + # Called when the script is executed (using File -> Run in Script Editor). + pass diff --git a/modules/gdscript/editor_templates/Node/default.gd b/modules/gdscript/editor_templates/Node/default.gd new file mode 100644 index 0000000000..ee5c0b99cc --- /dev/null +++ b/modules/gdscript/editor_templates/Node/default.gd @@ -0,0 +1,11 @@ +# meta-description: Base template for Node with default Godot cycle methods + +extends _BASE_ + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + pass diff --git a/modules/gdscript/editor_templates/Object/empty.gd b/modules/gdscript/editor_templates/Object/empty.gd new file mode 100644 index 0000000000..387786b0a4 --- /dev/null +++ b/modules/gdscript/editor_templates/Object/empty.gd @@ -0,0 +1,3 @@ +# meta-description: Empty template suitable for all Objects + +extends _BASE_ diff --git a/modules/gdscript/editor_templates/SCsub b/modules/gdscript/editor_templates/SCsub new file mode 100644 index 0000000000..2266ef2d01 --- /dev/null +++ b/modules/gdscript/editor_templates/SCsub @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +Import("env") + +import editor.template_builders as build_template_gd + +env["BUILDERS"]["MakeGDTemplateBuilder"] = Builder( + action=env.Run(build_template_gd.make_templates, "Generating GDScript templates header."), + suffix=".h", + src_suffix=".gd", +) + +# Template files +templates_sources = Glob("*/*.gd") + +env.Alias("editor_template_gd", [env.MakeGDTemplateBuilder("templates.gen.h", templates_sources)]) diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 4822e411ce..02c1bfb40c 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -49,6 +49,10 @@ #include "tests/gdscript_test_runner.h" #endif +#ifdef TOOLS_ENABLED +#include "editor/editor_settings.h" +#endif + /////////////////////////// GDScriptNativeClass::GDScriptNativeClass(const StringName &p_name) { @@ -817,10 +821,16 @@ Error GDScript::reload(bool p_keep_state) { basedir = basedir.get_base_dir(); } - if (source.find("%BASE%") != -1) { - //loading a template, don't parse +// Loading a template, don't parse. +#ifdef TOOLS_ENABLED + if (basedir.begins_with(EditorSettings::get_singleton()->get_project_script_templates_dir())) { return OK; } +#else + if (source.find("_BASE_") != -1) { + return OK; + } +#endif { String source_path = path; diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h index ade4f247c9..2f1187e5b1 100644 --- a/modules/gdscript/gdscript.h +++ b/modules/gdscript/gdscript.h @@ -396,7 +396,7 @@ public: _debug_call_stack_pos--; } - virtual Vector debug_get_current_stack_info() { + virtual Vector debug_get_current_stack_info() override { if (Thread::get_main_id() != Thread::get_caller_id()) { return Vector(); } @@ -430,77 +430,76 @@ public: _FORCE_INLINE_ static GDScriptLanguage *get_singleton() { return singleton; } - virtual String get_name() const; + virtual String get_name() const override; /* LANGUAGE FUNCTIONS */ - virtual void init(); - virtual String get_type() const; - virtual String get_extension() const; - virtual Error execute_file(const String &p_path); - virtual void finish(); + virtual void init() override; + virtual String get_type() const override; + virtual String get_extension() const override; + virtual Error execute_file(const String &p_path) override; + virtual void finish() override; /* EDITOR FUNCTIONS */ - virtual void get_reserved_words(List *p_words) const; - virtual bool is_control_flow_keyword(String p_keywords) const; - virtual void get_comment_delimiters(List *p_delimiters) const; - virtual void get_string_delimiters(List *p_delimiters) const; - virtual String _get_processed_template(const String &p_template, const String &p_base_class_name) const; - virtual Ref