Skip to content

Instantly share code, notes, and snippets.

View HungryProton's full-sized avatar

HungryProton HungryProton

View GitHub Profile
@HungryProton
HungryProton / scene_util.gd
Last active August 18, 2025 11:09
Utility methods to find nodes by class
## Returns the first child node of a given class.
## If deep_search is true, search the whole tree below this node.
static func find_child_with_class(root: Node, type: Variant, deep_search: bool = false) -> Node:
if not is_instance_valid(root):
return null
if is_instance_of(root, type):
return root
var candidates: Array[Node] = root.get_children()
@HungryProton
HungryProton / auto_ui_sounds.gd
Last active August 18, 2025 10:33
Example script for adding sounds to every buttons in the whole project at runtime.
extends Node
## Automatically plays a sound on button click and hover
## Make it an autoload to work.
# Sound files
# Replace the path with your own
const BUTTON_CLICK := preload("res://ui/sfx/click_soft.ogg")
const BUTTON_FOCUS := preload("res://ui/sfx/bong_001.ogg")
@HungryProton
HungryProton / serializable_ref_counted.gd
Last active April 10, 2025 08:39
Serialize all script variables to a dictionary and back
class_name SerializableRefCounted
extends RefCounted
## Saves all user defined variables inside a dictionary
## Member variables from the built in class will not be included
func serialize() -> Dictionary:
var data := {}
for property in get_property_list():
var p_name: String = property.name
if property.usage != PROPERTY_USAGE_SCRIPT_VARIABLE:
@HungryProton
HungryProton / library_player_3d.gd
Created March 31, 2025 12:17
Randomly play a bunch of sounds from a single node
class_name LibraryPlayer3D
extends AudioStreamPlayer3D
## Randomly play different sounds
@export var audio_streams: Array[AudioStream] = []
@export var volume_range := Vector2(-3, 3)
@HungryProton
HungryProton / pom.gdshader
Last active July 31, 2024 16:12
Random experiments for POM from the "Godot Effects and Shaders" discord group, based on Tentabrobpy's shader.
shader_type spatial;
render_mode skip_vertex_transform;
uniform vec4 albedo : source_color = vec4(1.0);
uniform sampler2D texture_albedo : source_color,filter_linear_mipmap,repeat_enable;
uniform sampler2D texture_normal : hint_roughness_normal,filter_linear_mipmap,repeat_enable;
uniform float normal_scale : hint_range(-16,16) = 1.0;
uniform sampler2D texture_heightmap : hint_default_black,filter_linear_mipmap,repeat_enable;
uniform float heightmap_scale = 1.0;
uniform int heightmap_min_layers: hint_range(0, 128, 1) = 8;
@HungryProton
HungryProton / format_values.gd
Last active October 8, 2023 10:35
Print large and small numbers with their prefix multipliers
func format_value(value: float) -> String:
var result := ""
var negative := value < 0.0
value = abs(value)
if _last_value > 1000.0:
var scale = ["K", "M", "G", "T", "P", "E"]
var v = _last_value
var index = -1
while v > 1000.0 and index < (scale.size() - 1):
@HungryProton
HungryProton / autosmooth.gd
Last active July 8, 2023 14:46
Autosmooth meshes in GDScript
class_name MeshUtils
static func auto_smooth(mesh: Mesh, threshold_degrees := 30.0) -> Mesh:
var result := ArrayMesh.new()
var threshold := deg_to_rad(threshold_degrees)
var sanitized_mesh := merge_duplicate_vertices(mesh)
# Auto smooth each surfaces.
@HungryProton
HungryProton / underwater.gdshader
Created June 7, 2023 17:42
Under water shader with caustics
shader_type spatial;
uniform sampler2D depth_texture : hint_depth_texture, repeat_disable, filter_nearest;
uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
uniform float v_depth_offset = 0.0;
uniform float v_depth_fade = 1.0;
uniform sampler2D underwater_color : repeat_disable;
uniform sampler2D caustics_texture : hint_default_black;
@HungryProton
HungryProton / depth_override_shader.gdshader
Last active March 29, 2024 12:45
A Godot 4 shader to make things appear on top of other things within a range.
// A Godot 4 shader to make things appear on top of other things within a range.
// Initially, this was made so my characters' facial features would be rendered on top of their hair.
shader_type spatial;
render_mode unshaded;
uniform sampler2D depth_texture : hint_depth_texture, repeat_disable, filter_nearest;
// Maximum depth we can overdraw relative to the object original depth, in ENGINE UNITS.
@HungryProton
HungryProton / box_gizmo_plugin.gd
Last active May 29, 2024 11:11
Box gizmo example
extends EditorNode3DGizmoPlugin
var editor_plugin: EditorPlugin
var _previous_size
func _init():
create_material("lines", Color(1, 1, 1))
create_material("box", Color(1.0, 1.0, 1.0, 0.1))