Skip to content

Instantly share code, notes, and snippets.

View andrew-wilkes's full-sized avatar

Andrew Wilkes andrew-wilkes

View GitHub Profile
@andrew-wilkes
andrew-wilkes / Fibonacci.gd
Last active February 23, 2022 08:54
Optimized Fibonacci number calculator using GDScript
extends Control
# Fibonacci number calculation using recursion
# This has some optimization where prior numbers are stored in an array
# Also, the array is expanded in size as needed
func _ready():
# Calculate the nth number and pass in the array to be referenced
print(fibo(34, [1, 1]))
extends SceneTree
var t = []
func _init():
for j in 5:
var k = []
var p_time = OS.get_ticks_msec()
var target = get_node("new_parent_node")
var source = get_node("child")
self.remove_child(source)
target.add_child(source)
source.set_owner(target) # I think that this line of code is only needed in Editor scripts
@andrew-wilkes
andrew-wilkes / color.gd
Created December 24, 2021 05:01
Color code
# Switch between saturated and pastel palettes
var s = 0.5 if n > 15 else 1.0
button.modulate = Color.from_hsv(fmod(n / 15.0, 2.0), s, 1.0)
@andrew-wilkes
andrew-wilkes / clouds.gdshader
Last active October 8, 2021 06:09
Godot Clouds Shader
// https://www.shadertoy.com/view/4tdSWr
shader_type canvas_item;
uniform float cloudscale = 1.1;
uniform float speed = 0.03;
uniform float clouddark = 0.5;
uniform float cloudlight = 0.3;
uniform float cloudcover = 0.2;
uniform float cloudalpha = 8.0;
uniform float skytint = 0.5;
@andrew-wilkes
andrew-wilkes / count-bits-in-number.gd
Created August 9, 2021 11:22
Count number of bits in a number
extends Node2D
"""
See: https://www.geeksforgeeks.org/count-total-bits-number/
Given a positive number n, count total bit in it.
Input : 183
Output : 8
@andrew-wilkes
andrew-wilkes / key-repeat-ignore.gd
Last active July 30, 2021 12:05
GDScript Code to Ignore Key Repeats
var keys = {}
func _unhandled_input(event):
if event is InputEventKey:
# Add new key to dictionary
if not keys.has(event.scancode):
keys[event.scancode] = false
# Respond to change in key state (pressed or released)
if event.pressed != keys[event.scancode]:
# Remember the current state
@andrew-wilkes
andrew-wilkes / prime-numbers.gd
Created July 11, 2021 08:00
Prime Numbers with GDScript
extends Node2D
export var nmax = 300
func _ready():
# Find prime numbers
sieve_of_eratosthenes(nmax)
func sieve_of_eratosthenes(n: int):
@andrew-wilkes
andrew-wilkes / int2binary.gd
Last active May 26, 2021 12:01
Convert an Integer to a Binary String
var num_bits_index = 0
var bit_lengths = [4, 8, 16]
func int2binary(x: int) -> String:
var b = ""
for n in bit_lengths[num_bits_index]:
b = String(x % 2) + b
x /= 2
return b
@andrew-wilkes
andrew-wilkes / array-to-string.gd
Last active May 21, 2021 07:18
Convert string array to string
func array_to_string(a: Array) -> String:
return PoolStringArray(a).join("\n")