Skip to content

Instantly share code, notes, and snippets.

What is Garbage Collection?

Garbage Collection is a form of automatic memory management. It is a feature of many modern languages that you may have already used (C#, Python, Javascript, etc.) so you may already be using it without even knowing! In a garbage collected environment objects are automatically removed from memory after you stop using them. This means you can create a new object, use it for a while, and when you are done using it you set the variable that points to it as null and that's the end of your worries. Behind the scene the garbage collector ("GC") is keeping track of what objects are still being used. When an object is no longer being used the garbage collector automatically frees up the memory.

Lower level languages such as C and C++ do not provide a garbage collector out of the box. This means you have to manually keep track of what memory is being used and free it when you no longer wish to use it. This can be bug prone and is harder for a programmer to manage so Unreal Engine 4 has c

@LordNed
LordNed / GearRotator.cs
Last active October 21, 2022 23:49
A quick example of creating a gear or object that can spin with mouse cursor. Expects object to rotate around its Forward vector.
using UnityEngine;
public class GearRotator : MonoBehaviour
{
[SerializeField] private float m_radiusThreshold = 4f;
private float m_startingAngle;
private bool m_isBeingRotated;
private void OnDrawGizmos()
@LordNed
LordNed / ReactiveGrassSway.cs
Last active April 13, 2022 21:17
A 2D grass that sways and springs back when you walk through it or land on it. Place onto a 2D quad.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider2D))]
public class ReactiveGrassSway : MonoBehaviour
{
[SerializeField] private float m_bendForceOnExit = -0.1f;
[SerializeField] private bool m_windIsEnabled;
[SerializeField] private float m_baseWindForce = 0f;
[SerializeField] private float m_windPeriod = 0f;
@LordNed
LordNed / DiffuseSpriteFlash.shader
Last active November 23, 2020 14:08
2D Sprite Blinking
Shader "Sprites/Diffuse Flash"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_SelfIllum ("Self Illumination",Range(0.0,1.0)) = 0.0
_FlashAmount ("Flash Amount",Range(0.0,1.0)) = 0.0
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
using UnityEngine;
using System.Collections;
using Newtonsoft.Json;
public class GlobalState : MonoBehaviour
{
public static GameSettings Settings;
/// <summary>
/// This is as close to as we can come in Unity to a entry point to the program. The GlobalState
@LordNed
LordNed / JustCause3TaskSaveEdit.cs
Created October 23, 2018 03:54
This is a super quick exploration into the Just Cause 3 "Task" save file. This is the state of things discovered on your map (such as Settlements, Wingsuit Challenges, etc.).
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tasks
{
class Program
@LordNed
LordNed / MEHUD.cpp
Created October 18, 2017 03:45
Unreal Engine 4 C++ UMG Crossover
// Fill out your copyright notice in the Description page of Project Settings.
#include "MEHUD.h"
void AMEHUD::BeginPlay()
{
ResourceDisplayWidget = CreateWidget<UUserWidget>(PlayerOwner, ResourceDisplayClass);
ResourceDisplayWidget->AddToViewport();
}
// ToDo: This fixes the OoB issue when an animation doesn't have a key on the final frame,
// but this isn't totally correct as the time calculation below doesn't take the shifted
// t value into account.
int i = 1;
while (keys[i].Time < t)
{
i++;
if (i >= keys.Count)
{
i = 1;
@LordNed
LordNed / Types.md
Created November 22, 2017 08:23
Unreal Shader and Pipeline Types

LogTemp: FShaderType: FARKitCameraOverlayPS /Engine/Private/PostProcessMaterialShaders.usf LogTemp: FShaderType: FARKitCameraOverlayVS /Engine/Private/PostProcessMaterialShaders.usf LogTemp: FShaderType: TSlateElementPSLineSegmenttruetrueA /Engine/Private/SlateElementPixelShader.usf LogTemp: FShaderType: TSlateElementPSFonttruetrueA /Engine/Private/SlateElementPixelShader.usf LogTemp: FShaderType: TSlateElementPSLineSegmentfalsetrueA /Engine/Private/SlateElementPixelShader.usf LogTemp: FShaderType: TSlateElementPSFontfalsetrueA /Engine/Private/SlateElementPixelShader.usf LogTemp: FShaderType: TSlateElementPSBordertruefalseA /Engine/Private/SlateElementPixelShader.usf LogTemp: FShaderType: TSlateElementPSDefaulttruefalseA /Engine/Private/SlateElementPixelShader.usf LogTemp: FShaderType: TSlateElementPSBorderfalsefalseA /Engine/Private/SlateElementPixelShader.usf LogTemp: FShaderType: TSlateElementPSDefaultfalsefalseA /Engine/Private/SlateElementPixelShader.usf

using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class EditorEvents
{
// InitializeOnLoad causes this to get called when the editor starts up
static EditorEvents()
{