Skip to content

Instantly share code, notes, and snippets.

View Shilo's full-sized avatar

Shilo Shilo

View GitHub Profile
@Shilo
Shilo / screenshot_viewport_tool.gd
Last active November 7, 2024 11:37
Godot 4 tool to screenshot viewport when "screenshot" export boolean is pressed.
@tool
class_name ScreenshotViewportTool extends Node
@export var screenshot: bool:
set(__):
screenshot_viewport()
func screenshot_viewport(transparent_background: bool = false, hide_debug_colors: bool = false) -> void:
var time_string: String = Time.get_datetime_string_from_system().replace(":", "-").replace("T", "-")
var scene_file_name: String = self.get_owner().scene_file_path.get_file().get_basename()
@Shilo
Shilo / screenshot_viewport.gd
Created November 7, 2024 09:02
Godot 4 method to screenshot at resource path viewport with or without transparent background.
func screenshot_viewport(transparent_background: bool = false) -> void:
var time_string: String = Time.get_datetime_string_from_system().replace(":", "-").replace("T", "-")
var path := "res://screenshot-" + time_string + '-' + str(randi() % 8999 + 1000) + ".png"
var viewport := get_viewport()
if transparent_background:
viewport.transparent_bg = true;
await RenderingServer.frame_post_draw
var error: Error = viewport.get_texture().get_image().save_png(path)
@Shilo
Shilo / fps_counter.gd
Created November 7, 2024 07:25
FPS counter in Godot 4.
extends Label
func _process(_delta: float) -> void:
text = format_number(Engine.get_frames_per_second()) + " FPS"
func format_number(number: Variant, thousand_separator: String = ",") -> String:
var number_str: String = str(number)
var start_index: int = number_str.find(".")
if start_index == -1:
@Shilo
Shilo / format_number.gd
Created November 7, 2024 07:23
Number formatt method to add thousand separator to number strings.
func format_number(number: Variant, thousand_separator: String = ",") -> String:
var number_str: String = str(number)
var start_index: int = number_str.find(".")
if start_index == -1:
start_index = number_str.length()
for i in range(start_index - 3, 0, -3):
number_str = number_str.insert(i, thousand_separator)
@Shilo
Shilo / fog2d.gdshader
Last active November 5, 2024 19:08
Godot 4 shader for 2d fog for ColorRect. Useful for top down games. Note: Use Parallax2D and seamless NoiseTexture2D for best results.
shader_type canvas_item;
uniform sampler2D noise_texture: repeat_enable, filter_nearest;
uniform vec2 speed = vec2(0.01, 0.01);
uniform float sparsity: hint_range(0.0, 5.0) = 1.0;
uniform float alpha: hint_range(0.0, 1.0) = 0.5;
uniform float edge_falloff: hint_range(0.0, 1.0) = 0.0;
uniform float edge_falloff_sensitivity: hint_range(1.0, 20.0) = 1.0;
void fragment() {
@Shilo
Shilo / toggle_button.gd
Last active October 5, 2024 10:59
Godot toggle button for toggle state icon.
@tool
class_name ToggleButton extends Button
@export var icon_toggled: Texture2D
@onready var _icon: Texture2D = icon
func _ready() -> void:
toggle_mode = true
if not theme_type_variation:
theme_type_variation = &"ToggleButton"
@Shilo
Shilo / polygon_circle.gd
Last active July 22, 2024 09:55
Polygon circle example. (Godot 4)
@tool
extends Node2D
@export var radius = 50.0:
set(value):
radius = value
queue_redraw()
@export var start_angle_deg = 0.0:
set(value):
@Shilo
Shilo / circle_2d.gd
Last active July 22, 2024 13:12
Dynamic circle polygon shape with start and end angles. (Godot 4)
@tool
class_name Circle2D extends Polygon2D
@export_range(0.0, 360.0, 0.001, "or_greater", "or_less") var start_degree: float = 0.0:
set(value):
start_degree = _wrap_degree(value)
queue_update()
@export_range(0.0, 360.0, 0.001, "or_greater", "or_less") var end_degree: float = 360.0:
set(value):
@Shilo
Shilo / color_util.gd
Last active June 26, 2024 06:23
Color utility methods. [Godot 4]
class_name ColorUtil extends Object
static func parse_html(rgba_or_preset: String, ignore_alpha: bool = false, default_color: Variant = null) -> Color:
var color: Variant = PRESET.get(rgba_or_preset.to_upper())
if color == null and Color.html_is_valid(rgba_or_preset):
color = Color.html(rgba_or_preset)
if color == null:
return default_color
@Shilo
Shilo / eval.gd
Created June 26, 2024 02:31
Godot GDScript eval helper function.
func eval(code: String) -> Error:
var script := GDScript.new()
script.source_code = "static func _static_init(): pass; " + code.strip_edges().replace("\n", ";")
return script.reload()