Skip to content

Instantly share code, notes, and snippets.

@willnationsdev
Last active July 19, 2017 13:20
Show Gist options
  • Save willnationsdev/b5c0eb9534a78900e9dbdfbec4c87b8d to your computer and use it in GitHub Desktop.
Save willnationsdev/b5c0eb9534a78900e9dbdfbec4c87b8d to your computer and use it in GitHub Desktop.
GodotTemplate - for creating run-time/design-time scripts dynamically
# Class: CreateEnemyButton
# Author: willnationsdev
# License: MIT
# Description: This class demonstrations a sample use-case for the Template class.
# A button which triggers the creation of enemy scripts when pressed.
# Godot Version: 3.0 pre-alpha
tool
extends Button
signal script_generated(file, parameters)
var Template = preload("res://path/to/Template.gd")
var template_path = "res://scripts/templates/enemies/template_base_enemy.gd"
var generation_directory = "res://scripts/generated/enemies/"
var extension = ".gd"
var template_parameters = {
BASE_TYPE = "Enemy",
BASE_PATH = "res://scripts/enemy.gd"
DERIVED_TYPE = null
}
func _ready():
connect("pressed", self, "_on_pressed")
func _on_pressed():
var name = get_node("../enemy_label").get_text()
template_parameters["DERIVED_TYPE"] = name
var created_file = Template.generate(template_path, generation_directory + name + extension, template_parameters)
emit_signal("script_generated", created_file, template_parameters)
# Class: Template
# Author: willnationsdev
# License: MIT
# Description: Provides utility methods for generating code at run-time / design-time from templates.
# Godot Version: 3.0 pre-alpha
extends Reference
enum {
TEMPLATE_ERROR_BAD_TEMPLATE_PATH = 1, # Unable to open template file at the given template filepath.
TEMPLATE_ERROR_BAD_DESTINATION_PATH = 2 # Unable to create file at the given new filepath.
}
static func generate(p_template_filepath, p_new_filepath = "res://new_file_" + str(randi()), p_template_parameters = {}):
# Initialize the files
var v_template_file = File.new()
var v_new_file = File.new()
# Validation: Unable to open template file at the given template filepath.
if v_template_file.open(p_template_filepath, v_new_file.READ) != OK:
print(p_template_filepath + " could not be opened")
return TEMPLATE_ERROR_BAD_TEMPLATE_PATH
# Validation: Unable to create file at the given new filepath.
if v_new_file.open(p_new_filepath, v_new_file.WRITE) != OK:
print(p_new_filepath + " could not be created or overwritten")
return TEMPLATE_ERROR_BAD_DESTINATION_PATH
# Get the template content
var v_content = v_template_file.get_as_text()
# For each expression...
for i_expression in p_template_parameters:
# Replace all instances of the template keys with their values
v_content = v_content.replace(i_expression, p_template_parameters[i_expression])
# Re-insert all of the content into the new file
v_new_file.store_string(v_content)
# Return the new file
return v_new_file
extends "BASE_PATH"
var BASE_TYPE = preload("BASE_PATH")
func _ready():
pass
func get_name():
return "DERIVED_TYPE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment