Skip to content

Instantly share code, notes, and snippets.

@xphere
xphere / bug.gd
Created October 18, 2018 15:11
Variable extraction changes outcome when inserting into dictionary
extends Node
func _ready() -> void:
var values
# Bad setup: prints {a:40, b:42}
values = { "a": 0, "b": 0 }
for idx in range(0, 100):
values["a" if randf() > 0.9 else "b"] += 1
print(values)
@xphere
xphere / BackgroundLoader.gd
Created October 10, 2018 09:53
Load resources in the background, based on the original Godot `resource_queue.gd`
extends Node
signal load_finished(path)
signal load_stage(path, stage, total)
var loader : Thread
var mutex : Mutex
var ready : Semaphore
var resources = {}
var queued = []
@xphere
xphere / 4-color.shader
Created October 2, 2018 07:53
Godot 4-color shader
shader_type canvas_item;
render_mode blend_mix;
uniform vec4 color_1 = vec4(0.784313725, 0.788235294, 0.262745098, 1.0);
uniform vec4 color_2 = vec4(0.490196078, 0.521568627, 0.152941176, 1.0);
uniform vec4 color_3 = vec4(0.000000000, 0.415686275, 0.000000000, 1.0);
uniform vec4 color_4 = vec4(0.015686275, 0.243137255, 0.000000000, 1.0);
uniform float offset = 0.5;
@xphere
xphere / Notifier.gd
Last active September 30, 2018 22:48
GDscript helper to notify all nodes in a group when some event raises. Use it as AutoLoad.
extends Node
var _groups = {}
func listen(event_name, group_name, method_name):
if not method_name:
method_name = "on_" + event_name
if _groups.has(event_name):
@xphere
xphere / godot_clean_before_git.sh
Last active September 13, 2018 17:41
Remove useless fields in Godot projects
#!env sh
FILES="$(find -type f -name *.tscn)" && sed -i "/^editor\/display_folded/d" -- $FILES && sed -i "/^_sections_unfolded/d" -- $FILES && sed -i '/^\[node /s/ index="0"//g' -- $FILES
@xphere
xphere / PhpArrayRouteLoader.php
Created September 28, 2017 01:11
PhpArrayRouteLoader
<?php
namespace Berny\Symfony\Routing;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\RouteCollectionBuilder;
final class PhpArrayRouteLoader extends FileLoader
{
@xphere
xphere / CachedIterator.php
Last active October 16, 2017 21:09
Memoize generators in order to be able to rewind them
<?php
use Iterator;
use IteratorAggregate;
final class CachedIterator implements IteratorAggregate
{
private $cached;
private $iterator;
extends Area2D
func _ready():
if get_tree().is_editor_hint():
return
#old_workaround()
fixed_workaround()
func fixed_workaround():
var children = get_children()
<?php // Full license: https://gist.github.com/xphere/710839fba51cdd582ebd4ddf635cc0c4
use DateTimeImmutable as Time;
final class Watch
{
/** @var Time|null */
private static $frozenTime = null;
public static function fromFormat($dateString, $format = 'Y-m-d H:i:s'): Time
@xphere
xphere / PathDumper.php
Created November 23, 2016 22:45
Dump paths defined in composer.json into lib/paths.php to load in the kernel
<?php
use Composer\Script\Event;
class PathDumper
{
static $defaults = [
'symfony-app-dir' => 'boot',
'symfony-bin-dir' => 'bin',
'symfony-config-dir' => 'etc',