Last active
July 19, 2017 13:20
-
-
Save willnationsdev/b5c0eb9534a78900e9dbdfbec4c87b8d to your computer and use it in GitHub Desktop.
GodotTemplate - for creating run-time/design-time scripts dynamically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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