Skip to content

Instantly share code, notes, and snippets.

@nothke
nothke / AStarSearchNonAlloc.cs
Last active January 4, 2025 17:43
Vector2Int in description
// This is a C# example from Red Blob Games (https://www.redblobgames.com/pathfinding/a-star/implementation.html#csharp). Remade to have no GCAllocs
// Changes:
// - Made to work in Unity and draw gizmos on screen instead of console text;
// - Cache all collections in Init(), then when Search() is called, no allocs are being made;
// - Uses Unity's Vector2Int that avoids boxing when comparing structs;
// - graph.Neighbors() no longer uses an IEnumerable<>. instead, fill a list with neighbors;
using System;
using System.Collections.Generic;
using UnityEngine;
// A very basic and silly example of some class that contains a slow operation we'd like to compile with Burst.
// This only shows how to CONVERT a -theoretically- already existing piece of code, and still reap the benefits of the Burst compiler.
// If you started with knowing it will be Burst compiled from the beginning you might design everything differently.
// This is an example of how we might do a thing in classic C# OOP
public class ClassicExample
{
public int multiplier = 2;
const int CONST = 42;
public int[] array;
template <typename T>
class SwappingPool
{
public:
const size_t size;
private:
size_t headi{ 0 };
T* data;
public:
  • gifcam records frames uncompressed, and it's a 32bit software, so you can easily run out of memory if you record a large piece of the screen for a long time. If that happens, it usually hangs and becomes unresponsive, and you need to stop the process, but sometimes it pings a warning, or just silently crashes.
  • To check how big your gif is, without saving the file, expand the menu beside Save > and click on Preview, after it's done compressing it will preview and show the size in the window title. This is important to know because there are gif upload limits everywhere, on Twitter it's 15MB, and on Discord it's 8MB (as of August 2020);
  • To reduce the size of the gif to fit in ^ those sizes, I usually try not to change a compression method (to something else than quantize, because it's almost always superior), but instead crop, trim or resize gifs.
  • Gifs ALWAYS have a one pixel wide black border (not sure why!?). You can actually remove it by just cropping the outside a bit in Edit > right click > Crop.
using System;
using System.Collections.Generic;
class Program
{
static List<int> pare;
static void Main(string[] args)
{
pare = new List<int>();
public static void SafeDestroy<T>(T obj) where T : Object
{
if (Application.isPlaying)
Object.Destroy(obj);
else
Object.DestroyImmediate(obj);
}
// Call in OnValidate, OnTriggerEnter, etc, with StartCoroutine() out of play mode
public static IEnumerator Catch22Destroy<T>(T obj) where T : Object
{
"files.exclude": {
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/*.meta": true
},
"editor.renderWhitespace": "none",
"csharp.suppressDotnetInstallWarning": true,
// Rebinding using InputSystem from Prokuv'o
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Rebinder : MonoBehaviour
/// A little Minesweeper for Unity using GUI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Minesweeper : MonoBehaviour
{
public enum TileState
{
#include "pch.h"
#include <iostream>
#include <vector>
#include <array>
#include <map>
#define LOG(x) std::cout << x << std::endl
struct Node
{