Skip to content

Instantly share code, notes, and snippets.

@ratkingsminion
ratkingsminion / preferences.gd
Created February 7, 2024 15:17
Simple script for user preferences in Godot/GDScript
class_name Preferences
extends Resource
## # Usage example:
## var pref := Preferences.load_or_create("game_settings")
## mouse_sensitivity = pref.get_data("mouse_sensitivity", 1.0)
## # later, when changing the setting:
## pref.set_data("mouse_sensitivity", mouse_sensitivity)
static var _all:Dictionary
@ratkingsminion
ratkingsminion / property_list_example.gd
Created February 4, 2024 11:27
GDScript example for _get_property_list() in combination with a resizable array
@tool
extends Node
@export var hammers:Array = [ 0, 0, 0 ]
func _get(property:StringName) -> Variant:
if property.begins_with("hammer_type"):
return hammers[int(str(property))]
match property:
&"hammer_count": return hammers.size()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TerrainUtils;
namespace RatKing {
public class ShowTerrainHeightsOfBounds : MonoBehaviour {
void Awake() {
var showSeconds = 15f;
@ratkingsminion
ratkingsminion / TerrainFillByNeighbour.cs
Last active February 20, 2023 14:20
C# - Unity tool for filling a new neighbour terrain tile so it's on the origin XZ plane and still considering its neighbours
using UnityEditor;
using UnityEngine;
namespace RatKing {
// Usage: Put this file into an Editor folder in your Unity project
// When creating a new neighbour tile for terrain, select it and then choose
// "Tools -> Terrain -> Fill Terrain Height by Neighbours" - it will flatten the
// tile and move the ground to the origin XZ plane. NO UNDO!!! So be careful.
@ratkingsminion
ratkingsminion / ResetStaticsAttribute.cs
Last active November 23, 2022 17:29
C# - An attribute that lets you reset static variables if needed
using System;
using System.Reflection;
using System.Collections.Generic;
// Usage:
// [ResetStatics(nameof(BooleanField))]
// public class MyClass : MonoBehaviour {
// public static bool BooleanField;
// // ...
// }
@ratkingsminion
ratkingsminion / ExtensionMethods.cs
Created August 17, 2018 12:33
Unity Extension Methods
using UnityEngine;
namespace RatKing {
public static class ExtensionMethods {
public static Vector2 ToVec2(this Vector3 v) { return new Vector2(v.x, v.y); }
public static Vector3 ToVec3(this Vector2 v, float z = 0f) { return new Vector3(v.x, v.y, z); }
public static Vector2 WithX(this Vector2 v, float x) { v.x = x; return v; } // "this" on structs is always by value, never by reference
public static Vector2 WithY(this Vector2 v, float y) { v.y = y; return v; } // thus the methods only edit a copy and return a copy