summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2022-01-03 20:40:33 +0100
committerGitHub <noreply@github.com>2022-01-03 20:40:33 +0100
commit98b3ba1842a47a34097499a9eee6aa31083a99df (patch)
tree33efc4bb525ed4452c4924efc459bacbf4c5d68b /modules
parent20563b081bcbfe853622a139422751dba6a75714 (diff)
parent9d5b80705912d85c3c7301ac0ea0afbf9372a660 (diff)
Merge pull request #53957 from fabriceci/new-template-workflow
Diffstat (limited to 'modules')
-rw-r--r--modules/gdscript/SCsub2
-rw-r--r--modules/gdscript/editor_templates/CharacterBody2D/basic_movement.gd29
-rw-r--r--modules/gdscript/editor_templates/CharacterBody3D/basic_movement.gd32
-rw-r--r--modules/gdscript/editor_templates/EditorPlugin/plugin.gd11
-rw-r--r--modules/gdscript/editor_templates/EditorScript/basic_editor_script.gd7
-rw-r--r--modules/gdscript/editor_templates/Node/default.gd11
-rw-r--r--modules/gdscript/editor_templates/Object/empty.gd3
-rw-r--r--modules/gdscript/editor_templates/SCsub16
-rw-r--r--modules/gdscript/gdscript.cpp14
-rw-r--r--modules/gdscript/gdscript.h101
-rw-r--r--modules/gdscript/gdscript_editor.cpp91
-rw-r--r--modules/mono/SCsub2
-rw-r--r--modules/mono/csharp_script.cpp65
-rw-r--r--modules/mono/csharp_script.h4
-rw-r--r--modules/mono/editor_templates/CharacterBody2D/basic_movement.cs41
-rw-r--r--modules/mono/editor_templates/CharacterBody3D/basic_movement.cs44
-rw-r--r--modules/mono/editor_templates/EditorPlugin/plugin.cs19
-rw-r--r--modules/mono/editor_templates/EditorScript/basic_editor_script.cs14
-rw-r--r--modules/mono/editor_templates/Node/default.cs19
-rw-r--r--modules/mono/editor_templates/Object/empty.cs9
-rw-r--r--modules/mono/editor_templates/SCsub16
-rw-r--r--modules/visual_script/visual_script.cpp15
-rw-r--r--modules/visual_script/visual_script.h79
23 files changed, 438 insertions, 206 deletions
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<StackInfo> debug_get_current_stack_info() {
+ virtual Vector<StackInfo> debug_get_current_stack_info() override {
if (Thread::get_main_id() != Thread::get_caller_id()) {
return Vector<StackInfo>();
}
@@ -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<String> *p_words) const;
- virtual bool is_control_flow_keyword(String p_keywords) const;
- virtual void get_comment_delimiters(List<String> *p_delimiters) const;
- virtual void get_string_delimiters(List<String> *p_delimiters) const;
- virtual String _get_processed_template(const String &p_template, const String &p_base_class_name) const;
- virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const;
- virtual bool is_using_templates();
- virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script);
- virtual bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<ScriptLanguage::ScriptError> *r_errors = nullptr, List<ScriptLanguage::Warning> *r_warnings = nullptr, Set<int> *r_safe_lines = nullptr) const;
- virtual Script *create_script() const;
- virtual bool has_named_classes() const;
- virtual bool supports_builtin_mode() const;
- virtual bool supports_documentation() const;
- virtual bool can_inherit_from_file() const { return true; }
- virtual int find_function(const String &p_function, const String &p_code) const;
- virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const;
- virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint);
+ virtual void get_reserved_words(List<String> *p_words) const override;
+ virtual bool is_control_flow_keyword(String p_keywords) const override;
+ virtual void get_comment_delimiters(List<String> *p_delimiters) const override;
+ virtual void get_string_delimiters(List<String> *p_delimiters) const override;
+ virtual bool is_using_templates() override;
+ virtual Ref<Script> make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const override;
+ virtual Vector<ScriptTemplate> get_built_in_templates(StringName p_object) override;
+ virtual bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<ScriptLanguage::ScriptError> *r_errors = nullptr, List<ScriptLanguage::Warning> *r_warnings = nullptr, Set<int> *r_safe_lines = nullptr) const override;
+ virtual Script *create_script() const override;
+ virtual bool has_named_classes() const override;
+ virtual bool supports_builtin_mode() const override;
+ virtual bool supports_documentation() const override;
+ virtual bool can_inherit_from_file() const override { return true; }
+ virtual int find_function(const String &p_function, const String &p_code) const override;
+ virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override;
+ virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptCodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) override;
#ifdef TOOLS_ENABLED
- virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result);
+ virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override;
#endif
virtual String _get_indentation() const;
- virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const;
- virtual void add_global_constant(const StringName &p_variable, const Variant &p_value);
- virtual void add_named_global_constant(const StringName &p_name, const Variant &p_value);
- virtual void remove_named_global_constant(const StringName &p_name);
+ virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override;
+ virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) override;
+ virtual void add_named_global_constant(const StringName &p_name, const Variant &p_value) override;
+ virtual void remove_named_global_constant(const StringName &p_name) override;
/* DEBUGGER FUNCTIONS */
- virtual String debug_get_error() const;
- virtual int debug_get_stack_level_count() const;
- virtual int debug_get_stack_level_line(int p_level) const;
- virtual String debug_get_stack_level_function(int p_level) const;
- virtual String debug_get_stack_level_source(int p_level) const;
- virtual void debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1);
- virtual void debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1);
- virtual ScriptInstance *debug_get_stack_level_instance(int p_level);
- virtual void debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1);
- virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems = -1, int p_max_depth = -1);
+ virtual String debug_get_error() const override;
+ virtual int debug_get_stack_level_count() const override;
+ virtual int debug_get_stack_level_line(int p_level) const override;
+ virtual String debug_get_stack_level_function(int p_level) const override;
+ virtual String debug_get_stack_level_source(int p_level) const override;
+ virtual void debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override;
+ virtual void debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override;
+ virtual ScriptInstance *debug_get_stack_level_instance(int p_level) override;
+ virtual void debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override;
+ virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems = -1, int p_max_depth = -1) override;
- virtual void reload_all_scripts();
- virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload);
+ virtual void reload_all_scripts() override;
+ virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) override;
- virtual void frame();
+ virtual void frame() override;
- virtual void get_public_functions(List<MethodInfo> *p_functions) const;
- virtual void get_public_constants(List<Pair<String, Variant>> *p_constants) const;
+ virtual void get_public_functions(List<MethodInfo> *p_functions) const override;
+ virtual void get_public_constants(List<Pair<String, Variant>> *p_constants) const override;
- virtual void profiling_start();
- virtual void profiling_stop();
+ virtual void profiling_start() override;
+ virtual void profiling_stop() override;
- virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max);
- virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max);
+ virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) override;
+ virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) override;
/* LOADER FUNCTIONS */
- virtual void get_recognized_extensions(List<String> *p_extensions) const;
+ virtual void get_recognized_extensions(List<String> *p_extensions) const override;
/* GLOBAL CLASSES */
- virtual bool handles_global_class_type(const String &p_type) const;
- virtual String get_global_class_name(const String &p_path, String *r_base_type = nullptr, String *r_icon_path = nullptr) const;
+ virtual bool handles_global_class_type(const String &p_type) const override;
+ virtual String get_global_class_name(const String &p_path, String *r_base_type = nullptr, String *r_icon_path = nullptr) const override;
void add_orphan_subclass(const String &p_qualified_name, const ObjectID &p_subclass);
Ref<GDScript> get_orphan_subclass(const String &p_qualified_name);
diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp
index ec01c19295..3fb834590b 100644
--- a/modules/gdscript/gdscript_editor.cpp
+++ b/modules/gdscript/gdscript_editor.cpp
@@ -33,6 +33,7 @@
#include "core/config/engine.h"
#include "core/core_constants.h"
#include "core/io/file_access.h"
+#include "editor_templates/templates.gen.h"
#include "gdscript_analyzer.h"
#include "gdscript_compiler.h"
#include "gdscript_parser.h"
@@ -55,68 +56,44 @@ void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
p_delimiters->push_back("\"\"\" \"\"\"");
}
-String GDScriptLanguage::_get_processed_template(const String &p_template, const String &p_base_class_name) const {
- String processed_template = p_template;
+bool GDScriptLanguage::is_using_templates() {
+ return true;
+}
+Ref<Script> GDScriptLanguage::make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const {
+ Ref<GDScript> script;
+ script.instantiate();
+ String processed_template = p_template;
#ifdef TOOLS_ENABLED
- if (EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
- processed_template = processed_template.replace("%INT_TYPE%", ": int");
- processed_template = processed_template.replace("%STRING_TYPE%", ": String");
- processed_template = processed_template.replace("%FLOAT_TYPE%", ": float");
- processed_template = processed_template.replace("%VOID_RETURN%", " -> void");
- } else {
- processed_template = processed_template.replace("%INT_TYPE%", "");
- processed_template = processed_template.replace("%STRING_TYPE%", "");
- processed_template = processed_template.replace("%FLOAT_TYPE%", "");
- processed_template = processed_template.replace("%VOID_RETURN%", "");
+ if (!EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
+ processed_template = processed_template.replace(": int", "")
+ .replace(": String", "")
+ .replace(": float", "")
+ .replace(":=", "=")
+ .replace(" -> void", "");
}
#else
- processed_template = processed_template.replace("%INT_TYPE%", "");
- processed_template = processed_template.replace("%STRING_TYPE%", "");
- processed_template = processed_template.replace("%FLOAT_TYPE%", "");
- processed_template = processed_template.replace("%VOID_RETURN%", "");
+ processed_template = processed_template.replace(": int", "")
+ .replace(": String", "")
+ .replace(": float", "")
+ .replace(" -> void", "");
#endif
- processed_template = processed_template.replace("%BASE%", p_base_class_name);
- processed_template = processed_template.replace("%TS%", _get_indentation());
-
- return processed_template;
-}
-
-Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
- String _template = "extends %BASE%\n"
- "\n"
- "\n"
- "# Declare member variables here. Examples:\n"
- "# var a%INT_TYPE% = 2\n"
- "# var b%STRING_TYPE% = \"text\"\n"
- "\n"
- "\n"
- "# Called when the node enters the scene tree for the first time.\n"
- "func _ready()%VOID_RETURN%:\n"
- "%TS%pass # Replace with function body.\n"
- "\n"
- "\n"
- "# Called every frame. 'delta' is the elapsed time since the previous frame.\n"
- "#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n"
- "#%TS%pass\n";
-
- _template = _get_processed_template(_template, p_base_class_name);
-
- Ref<GDScript> script;
- script.instantiate();
- script->set_source_code(_template);
-
+ processed_template = processed_template.replace("_BASE_", p_base_class_name)
+ .replace("_CLASS_", p_class_name)
+ .replace("_TS_", _get_indentation());
+ script->set_source_code(processed_template);
return script;
}
-bool GDScriptLanguage::is_using_templates() {
- return true;
-}
-
-void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
- String _template = _get_processed_template(p_script->get_source_code(), p_base_class_name);
- p_script->set_source_code(_template);
+Vector<ScriptLanguage::ScriptTemplate> GDScriptLanguage::get_built_in_templates(StringName p_object) {
+ Vector<ScriptLanguage::ScriptTemplate> templates;
+ for (int i = 0; i < TEMPLATES_ARRAY_SIZE; i++) {
+ if (TEMPLATES[i].inherit == p_object) {
+ templates.append(TEMPLATES[i]);
+ }
+ }
+ return templates;
}
static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, Map<int, String> &r_funcs) {
@@ -236,7 +213,7 @@ Script *GDScriptLanguage::create_script() const {
/* DEBUGGER FUNCTIONS */
bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
- //break because of parse error
+ // break because of parse error
if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
_debug_parse_err_line = p_line;
@@ -1383,8 +1360,8 @@ static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context,
}
if (!script.ends_with(".gd")) {
- //not a script, try find the script anyway,
- //may have some success
+ // not a script, try find the script anyway,
+ // may have some success
script = script.get_basename() + ".gd";
}
@@ -2770,7 +2747,7 @@ void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_t
}
if (indent_stack.size() && indent_stack.back()->get() != tc) {
- indent_stack.push_back(tc); //this is not right but gets the job done
+ indent_stack.push_back(tc); // this is not right but gets the job done
}
}
diff --git a/modules/mono/SCsub b/modules/mono/SCsub
index 95c959235c..db35e804aa 100644
--- a/modules/mono/SCsub
+++ b/modules/mono/SCsub
@@ -63,3 +63,5 @@ elif env["platform"] == "android":
if env["tools"]:
env_mono.add_source_files(env.modules_sources, "editor/*.cpp")
+
+SConscript("editor_templates/SCsub")
diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp
index aa24f59fdb..4dcf975a9a 100644
--- a/modules/mono/csharp_script.cpp
+++ b/modules/mono/csharp_script.cpp
@@ -55,6 +55,7 @@
#endif
#include "editor/editor_internal_calls.h"
+#include "editor_templates/templates.gen.h"
#include "godotsharp_dirs.h"
#include "mono_gd/gd_mono_cache.h"
#include "mono_gd/gd_mono_class.h"
@@ -351,57 +352,33 @@ static String get_base_class_name(const String &p_base_class_name, const String
return base_class;
}
-Ref<Script> CSharpLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
- String script_template = "using " BINDINGS_NAMESPACE ";\n"
- "using System;\n"
- "\n"
- "public partial class %CLASS% : %BASE%\n"
- "{\n"
- " // Declare member variables here. Examples:\n"
- " // private int a = 2;\n"
- " // private string b = \"text\";\n"
- "\n"
- " // Called when the node enters the scene tree for the first time.\n"
- " public override void _Ready()\n"
- " {\n"
- " \n"
- " }\n"
- "\n"
- "// // Called every frame. 'delta' is the elapsed time since the previous frame.\n"
- "// public override void _Process(float delta)\n"
- "// {\n"
- "// \n"
- "// }\n"
- "}\n";
-
- // Replaces all spaces in p_class_name with underscores to prevent
- // invalid C# Script templates from being generated when the object name
- // has spaces in it.
- String class_name_no_spaces = p_class_name.replace(" ", "_");
- String base_class_name = get_base_class_name(p_base_class_name, class_name_no_spaces);
- script_template = script_template.replace("%BASE%", base_class_name)
- .replace("%CLASS%", class_name_no_spaces);
+bool CSharpLanguage::is_using_templates() {
+ return true;
+}
+Ref<Script> CSharpLanguage::make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const {
Ref<CSharpScript> script;
script.instantiate();
- script->set_source_code(script_template);
- script->set_name(class_name_no_spaces);
+ String class_name_no_spaces = p_class_name.replace(" ", "_");
+ String base_class_name = get_base_class_name(p_base_class_name, class_name_no_spaces);
+ String processed_template = p_template;
+ processed_template = processed_template.replace("_BINDINGS_NAMESPACE_", BINDINGS_NAMESPACE)
+ .replace("_BASE_", base_class_name)
+ .replace("_CLASS_", class_name_no_spaces)
+ .replace("_TS_", _get_indentation());
+ script->set_source_code(processed_template);
return script;
}
-bool CSharpLanguage::is_using_templates() {
- return true;
-}
-
-void CSharpLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
- String src = p_script->get_source_code();
- String class_name_no_spaces = p_class_name.replace(" ", "_");
- String base_class_name = get_base_class_name(p_base_class_name, class_name_no_spaces);
- src = src.replace("%BASE%", base_class_name)
- .replace("%CLASS%", class_name_no_spaces)
- .replace("%TS%", _get_indentation());
- p_script->set_source_code(src);
+Vector<ScriptLanguage::ScriptTemplate> CSharpLanguage::get_built_in_templates(StringName p_object) {
+ Vector<ScriptLanguage::ScriptTemplate> templates;
+ for (int i = 0; i < TEMPLATES_ARRAY_SIZE; i++) {
+ if (TEMPLATES[i].inherit == p_object) {
+ templates.append(TEMPLATES[i]);
+ }
+ }
+ return templates;
}
String CSharpLanguage::validate_path(const String &p_path) const {
diff --git a/modules/mono/csharp_script.h b/modules/mono/csharp_script.h
index c998d9c1e4..15a0326383 100644
--- a/modules/mono/csharp_script.h
+++ b/modules/mono/csharp_script.h
@@ -463,9 +463,9 @@ public:
bool is_control_flow_keyword(String p_keyword) const override;
void get_comment_delimiters(List<String> *p_delimiters) const override;
void get_string_delimiters(List<String> *p_delimiters) const override;
- Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const override;
bool is_using_templates() override;
- void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) override;
+ virtual Ref<Script> make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const override;
+ virtual Vector<ScriptTemplate> get_built_in_templates(StringName p_object) override;
/* TODO */ bool validate(const String &p_script, const String &p_path, List<String> *r_functions,
List<ScriptLanguage::ScriptError> *r_errors = nullptr, List<ScriptLanguage::Warning> *r_warnings = nullptr, Set<int> *r_safe_lines = nullptr) const override {
return true;
diff --git a/modules/mono/editor_templates/CharacterBody2D/basic_movement.cs b/modules/mono/editor_templates/CharacterBody2D/basic_movement.cs
new file mode 100644
index 0000000000..6e9547ce9b
--- /dev/null
+++ b/modules/mono/editor_templates/CharacterBody2D/basic_movement.cs
@@ -0,0 +1,41 @@
+// meta-description: Classic movement for gravity games (platformer, ...)
+
+using _BINDINGS_NAMESPACE_;
+using System;
+
+public partial class _CLASS_ : _BASE_
+{
+ public const float Speed = 300.0f;
+ public const float JumpForce = -400.0f;
+
+ // Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
+ public float gravity = (float)ProjectSettings.GetSetting("physics/2d/default_gravity");
+
+ public override void _PhysicsProcess(float delta)
+ {
+ Vector2 motionVelocity = MotionVelocity;
+
+ // Add the gravity.
+ if (!IsOnFloor())
+ motionVelocity.y += gravity * delta;
+
+ // Handle Jump.
+ if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
+ motionVelocity.y = JumpForce;
+
+ // Get the input direction and handle the movement/deceleration.
+ // As good practice, you should replace UI actions with custom gameplay actions.
+ Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
+ if (direction != Vector2.Zero)
+ {
+ motionVelocity.x = direction.x * Speed;
+ }
+ else
+ {
+ motionVelocity.x = Mathf.MoveToward(MotionVelocity.x, 0, Speed);
+ }
+
+ MotionVelocity = motionVelocity;
+ MoveAndSlide();
+ }
+}
diff --git a/modules/mono/editor_templates/CharacterBody3D/basic_movement.cs b/modules/mono/editor_templates/CharacterBody3D/basic_movement.cs
new file mode 100644
index 0000000000..13be4bbcb1
--- /dev/null
+++ b/modules/mono/editor_templates/CharacterBody3D/basic_movement.cs
@@ -0,0 +1,44 @@
+// meta-description: Classic movement for gravity games (FPS, TPS, ...)
+
+using _BINDINGS_NAMESPACE_;
+using System;
+
+public partial class _CLASS_ : _BASE_
+{
+ public const float Speed = 5.0f;
+ public const float JumpForce = 4.5f;
+
+ // Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
+ public float gravity = (float)ProjectSettings.GetSetting("physics/3d/default_gravity");
+
+ public override void _PhysicsProcess(float delta)
+ {
+ Vector3 motionVelocity = MotionVelocity;
+
+ // Add the gravity.
+ if (!IsOnFloor())
+ motionVelocity.y -= gravity * delta;
+
+ // Handle Jump.
+ if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
+ motionVelocity.y = JumpForce;
+
+ // Get the input direction and handle the movement/deceleration.
+ // As good practice, you should replace UI actions with custom gameplay actions.
+ Vector2 inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
+ Vector3 direction = Transform.basis.Xform(new Vector3(inputDir.x, 0, inputDir.y)).Normalized();
+ if (direction != Vector3.Zero)
+ {
+ motionVelocity.x = direction.x * Speed;
+ motionVelocity.z = direction.z * Speed;
+ }
+ else
+ {
+ motionVelocity.x = Mathf.MoveToward(MotionVelocity.x, 0, Speed);
+ motionVelocity.z = Mathf.MoveToward(MotionVelocity.z, 0, Speed);
+ }
+
+ MotionVelocity = motionVelocity;
+ MoveAndSlide();
+ }
+}
diff --git a/modules/mono/editor_templates/EditorPlugin/plugin.cs b/modules/mono/editor_templates/EditorPlugin/plugin.cs
new file mode 100644
index 0000000000..6e6a799be6
--- /dev/null
+++ b/modules/mono/editor_templates/EditorPlugin/plugin.cs
@@ -0,0 +1,19 @@
+// meta-description: Basic plugin template
+#if TOOLS
+using _BINDINGS_NAMESPACE_;
+using System;
+
+[Tool]
+public partial class _CLASS_ : _BASE_
+{
+ public override void _EnterTree()
+ {
+ // Initialization of the plugin goes here.
+ }
+
+ public override void _ExitTree()
+ {
+ // Clean-up of the plugin goes here.
+ }
+}
+#endif
diff --git a/modules/mono/editor_templates/EditorScript/basic_editor_script.cs b/modules/mono/editor_templates/EditorScript/basic_editor_script.cs
new file mode 100644
index 0000000000..2088822890
--- /dev/null
+++ b/modules/mono/editor_templates/EditorScript/basic_editor_script.cs
@@ -0,0 +1,14 @@
+// meta-description: Basic editor script template
+#if TOOLS
+using _BINDINGS_NAMESPACE_;
+using System;
+
+[Tool]
+public partial class _CLASS_ : _BASE_
+{
+ public override void _Run()
+ {
+ // Called when the script is executed (using File -> Run in Script Editor).
+ }
+}
+#endif
diff --git a/modules/mono/editor_templates/Node/default.cs b/modules/mono/editor_templates/Node/default.cs
new file mode 100644
index 0000000000..73d69dd993
--- /dev/null
+++ b/modules/mono/editor_templates/Node/default.cs
@@ -0,0 +1,19 @@
+// meta-description: Base template for Node with default Godot cycle methods
+
+using _BINDINGS_NAMESPACE_;
+using System;
+
+public partial class _CLASS_ : _BASE_
+{
+ // Called when the node enters the scene tree for the first time.
+ public override void _Ready()
+ {
+
+ }
+
+ // Called every frame. 'delta' is the elapsed time since the previous frame.
+ public override void _Process(float delta)
+ {
+
+ }
+}
diff --git a/modules/mono/editor_templates/Object/empty.cs b/modules/mono/editor_templates/Object/empty.cs
new file mode 100644
index 0000000000..e5bee64fe1
--- /dev/null
+++ b/modules/mono/editor_templates/Object/empty.cs
@@ -0,0 +1,9 @@
+// meta-description: Empty template suitable for all Objects
+
+using _BINDINGS_NAMESPACE_;
+using System;
+
+public partial class _CLASS_ : _BASE_
+{
+
+}
diff --git a/modules/mono/editor_templates/SCsub b/modules/mono/editor_templates/SCsub
new file mode 100644
index 0000000000..39f6cb5c01
--- /dev/null
+++ b/modules/mono/editor_templates/SCsub
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+
+Import("env")
+
+import editor.template_builders as build_template_cs
+
+env["BUILDERS"]["MakeCSharpTemplateBuilder"] = Builder(
+ action=env.Run(build_template_cs.make_templates, "Generating C# templates header."),
+ suffix=".h",
+ src_suffix=".cs",
+)
+
+# Template files
+templates_sources = Glob("*/*.cs")
+
+env.Alias("editor_template_cs", [env.MakeCSharpTemplateBuilder("templates.gen.h", templates_sources)])
diff --git a/modules/visual_script/visual_script.cpp b/modules/visual_script/visual_script.cpp
index 63bd9bbee4..50c24179ff 100644
--- a/modules/visual_script/visual_script.cpp
+++ b/modules/visual_script/visual_script.cpp
@@ -2241,20 +2241,15 @@ void VisualScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) co
void VisualScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
}
-Ref<Script> VisualScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
- Ref<VisualScript> script;
- script.instantiate();
- script->set_instance_base_type(p_base_class_name);
- return script;
-}
-
bool VisualScriptLanguage::is_using_templates() {
- return true;
+ return false;
}
-void VisualScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
- Ref<VisualScript> script = p_script;
+Ref<Script> VisualScriptLanguage::make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const {
+ Ref<VisualScript> script;
+ script.instantiate();
script->set_instance_base_type(p_base_class_name);
+ return script;
}
bool VisualScriptLanguage::validate(const String &p_script, const String &p_path, List<String> *r_functions, List<ScriptLanguage::ScriptError> *r_errors, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
diff --git a/modules/visual_script/visual_script.h b/modules/visual_script/visual_script.h
index 39cef8f68b..e321ef236d 100644
--- a/modules/visual_script/visual_script.h
+++ b/modules/visual_script/visual_script.h
@@ -554,57 +554,56 @@ public:
//////////////////////////////////////
- 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<String> *p_words) const;
- virtual bool is_control_flow_keyword(String p_keyword) const;
- virtual void get_comment_delimiters(List<String> *p_delimiters) const;
- virtual void get_string_delimiters(List<String> *p_delimiters) const;
- virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const;
- virtual bool is_using_templates();
- virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script);
- virtual bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<ScriptLanguage::ScriptError> *r_errors = nullptr, List<ScriptLanguage::Warning> *r_warnings = nullptr, Set<int> *r_safe_lines = nullptr) const;
- virtual Script *create_script() const;
- virtual bool has_named_classes() const;
- virtual bool supports_builtin_mode() const;
- virtual int find_function(const String &p_function, const String &p_code) const;
- virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const;
- virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const;
- virtual void add_global_constant(const StringName &p_variable, const Variant &p_value);
+ virtual void get_reserved_words(List<String> *p_words) const override;
+ virtual bool is_control_flow_keyword(String p_keyword) const override;
+ virtual void get_comment_delimiters(List<String> *p_delimiters) const override;
+ virtual void get_string_delimiters(List<String> *p_delimiters) const override;
+ virtual bool is_using_templates() override;
+ virtual Ref<Script> make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const override;
+ virtual bool validate(const String &p_script, const String &p_path = "", List<String> *r_functions = nullptr, List<ScriptLanguage::ScriptError> *r_errors = nullptr, List<ScriptLanguage::Warning> *r_warnings = nullptr, Set<int> *r_safe_lines = nullptr) const override;
+ virtual Script *create_script() const override;
+ virtual bool has_named_classes() const override;
+ virtual bool supports_builtin_mode() const override;
+ virtual int find_function(const String &p_function, const String &p_code) const override;
+ virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override;
+ virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override;
+ virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) override;
/* DEBUGGER FUNCTIONS */
- virtual String debug_get_error() const;
- virtual int debug_get_stack_level_count() const;
- virtual int debug_get_stack_level_line(int p_level) const;
- virtual String debug_get_stack_level_function(int p_level) const;
- virtual String debug_get_stack_level_source(int p_level) const;
- virtual void debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1);
- virtual void debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1);
- virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1);
- virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems = -1, int p_max_depth = -1);
-
- virtual void reload_all_scripts();
- virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload);
+ virtual String debug_get_error() const override;
+ virtual int debug_get_stack_level_count() const override;
+ virtual int debug_get_stack_level_line(int p_level) const override;
+ virtual String debug_get_stack_level_function(int p_level) const override;
+ virtual String debug_get_stack_level_source(int p_level) const override;
+ virtual void debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override;
+ virtual void debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override;
+ virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems = -1, int p_max_depth = -1) override;
+ virtual String debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems = -1, int p_max_depth = -1) override;
+
+ virtual void reload_all_scripts() override;
+ virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) override;
/* LOADER FUNCTIONS */
- virtual void get_recognized_extensions(List<String> *p_extensions) const;
- virtual void get_public_functions(List<MethodInfo> *p_functions) const;
- virtual void get_public_constants(List<Pair<String, Variant>> *p_constants) const;
+ virtual void get_recognized_extensions(List<String> *p_extensions) const override;
+ virtual void get_public_functions(List<MethodInfo> *p_functions) const override;
+ virtual void get_public_constants(List<Pair<String, Variant>> *p_constants) const override;
- virtual void profiling_start();
- virtual void profiling_stop();
+ virtual void profiling_start() override;
+ virtual void profiling_stop() override;
- virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max);
- virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max);
+ virtual int profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) override;
+ virtual int profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) override;
void add_register_func(const String &p_name, VisualScriptNodeRegisterFunc p_func);
void remove_register_func(const String &p_name);