Skip to content

Instantly share code, notes, and snippets.

View Shilo's full-sized avatar

Shilo Shilo

View GitHub Profile
@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()
Summary How to control (or Understand) your GIST page's files list order.
Notice not official documentation.
@Shilo
Shilo / value_holder.gd
Last active June 25, 2024 04:39
Godot 4 generic wrapper for holding any value. Allows passing a value between functions by reference instead of value (copy).
## Generic wrapper for holding any value.
## Allows passing a value between functions by reference instead of value (copy).
class_name ValueHolder extends RefCounted
## Triggers when value is updated. Current and previous values are passed.
signal changed(value: Variant, last_value: Variant)
## Current value. Changing this value will emit changed() signal.
var value: Variant:
set(new_value):
@Shilo
Shilo / align_node2d_to_mouse_editor_script.gd
Created June 23, 2024 06:10
Godot EditorScript to align selected Node2Ds to current mouse position.
@tool
extends EditorScript
# Align selected Node2Ds to current mouse position.
# Ctrl+Shift+X to run.
func _run():
var editor_interface = get_editor_interface()
var mouse_position = editor_interface.get_editor_viewport_2d().get_mouse_position()
@Shilo
Shilo / twitch_redeem_reward.js
Last active June 18, 2024 20:23
Twitch Javascript injected script to simulate reward redemption.
// Twitch Javascript injected script to simulate reward redemption.
// Note: Class names may need to be changed if outdated.
// Index of reward to click.
var rewardButtonIndex = 0
var amountOfRedeems = 1
// Class names for elements.
var rewardsListButtonClass = "ScCoreButton-sc-ocjdkq-0 ScCoreButtonText-sc-ocjdkq-3 ljgEdo fuXDrj"
var specificRewardButtonClass = "ScInteractableBase-sc-ofisyf-0 ScInteractableDefault-sc-ofisyf-1 dsnvLR etibmD tw-interactable"
@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():