Skip to content

Instantly share code, notes, and snippets.

View Shilo's full-sized avatar

Shilo Shilo

View GitHub Profile
@Shilo
Shilo / export_inspector_toggle_example.gd
Created April 1, 2024 03:53
Godot 4 example on how to toggle @export visibility and readonly.
@tool
extends Node2D
@export var show_number: bool = false:
set(value):
show_number = value
notify_property_list_changed()
@export var editable_number: bool = false:
set(value):
@Shilo
Shilo / sway_component.gd
Created March 30, 2024 23:28
Godot 4 modular sway component to bounce the y position of a parent, up and down.
class_name SwayComponent extends Node
@export var sway_distance: float = 32
@export var sway_duration: float = 2
@onready var start_y: float = y
var y: float:
set(value):
var parent := get_parent()
@Shilo
Shilo / pd_line_2d.gd
Created March 29, 2024 02:37
Godot 4, PixelDream Line2D node for better line width and anti-aliasing.
@tool
class_name PDLine2D extends Node2D
@export var points: Array[Vector2i] = []:
set(value):
points = value
queue_redraw()
@export var color: Color = Color.WHITE:
set(value):
@Shilo
Shilo / space_player.gd
Created March 23, 2024 16:26
Godot Rigidbody2D that allows for movement and jump input. Input is relative the gravity direction. Useful for "Super Mario Galaxy" type planet game.
class_name SpacePlayer extends RigidBody2D
const JUMP_STRENGTH: float = 200
const MOVE_SPEED: float = 200
func _process(_delta: float) -> void:
update_input()
func update_input() -> void:
if !_is_on_ground():
@Shilo
Shilo / log.gd
Last active March 10, 2024 08:24
Godot Log AutoLoad. Helper methods to prefix network status in message.
# Log AutoLoad
extends Node
const nil = StringName("<NIL>")
func print(message: Variant, m2: Variant = nil, m3: Variant = nil, m4: Variant = nil, m5: Variant = nil, m6: Variant = nil) -> void:
print(build_message(message, m2, m3, m4, m5, m6))
func prints(message: Variant, m2: Variant = nil, m3: Variant = nil, m4: Variant = nil, m5: Variant = nil, m6: Variant = nil) -> void:
print(build_message(message, m2, m3, m4, m5, m6, " "))
@Shilo
Shilo / ClassUtil.gd
Created March 5, 2024 00:36
Class helper methods to compare global/custom classes (class_name) in Godot 4.
class_name ClassUtil
static func is_class_name(object: Object, classname: String) -> bool:
if object.is_class(classname):
return true
return is_global_class_name(object, classname)
static func is_exact_class_name(object: Object, classname: String) -> bool:
if !has_global_class_name(object):
return object.get_class() == classname
@Shilo
Shilo / NodeExtensions.cs
Last active March 1, 2024 16:42
Godot 4 extensions for Node.
using Godot;
public static class NodeExtensions
{
public static void RemoveAndFree(this Node node)
{
Node parent = node.GetParent();
if (!GodotObject.IsInstanceValid(parent))
return;
@Shilo
Shilo / benchmark.cs
Created February 21, 2024 04:47
Simple Benchmark methods in Godot.
public static void Benchmark(Action callable, string tag = null)
{
var start = Time.GetTicksMsec();
callable();
var duration = Time.GetTicksMsec() - start;
if (tag != null)
GD.PrintT(tag, duration);
else
@Shilo
Shilo / SoundManager.cs
Last active February 21, 2024 02:40
Audio manager singleton that pools audio players.
using Godot;
using System.Collections.Generic;
public partial class SoundManager : Node
{
private const int MaxSimultaneousSounds = 32;
private const int SuppressSoundTimeMs = 100;
private readonly List<AudioStreamPlayer2D> _inactiveAudioPlayers = new();
private readonly List<AudioStreamPlayer2D> _activeAudioPlayers = new();
@Shilo
Shilo / Camera.cs
Last active February 9, 2024 03:40
Simple camera shake script in Godot 4.
using Godot;
using System;
public partial class Camera : Camera2D
{
public enum ShakeStrengthType
{
Low,
Medium,
High