Skip to content

Instantly share code, notes, and snippets.

@karlgluck
karlgluck / Inverted-Program-Flow-By-Abusing-IEnumerator.md
Last active December 4, 2016 23:56
A novel control flow pattern for easily writing a game's primary state machine

I'm working on some side projects and have been toying with ideas for more efficient ways to write games in Unity with C#. One of the major hangups for some of my larger side projects is that once it reaches sufficient complexity, it's hard to tell exactly what state the app is in. Even the simple wallet app I built has this issue. I've come up with the beginnings of a solution so I wanted to write it down before it gets lost.

The key problem is that you want your program to respond to inputs that require waiting for a response from something (a webserver, a user, etc). There are a number of options in Unity with C#.

These first 4 options are variations on a theme of control "elsewhere" in the program letting your code know that something has happened:

  • Virtual Callback E.g. if you derive from a class, and override a method which gets called in response to an event
  • Reflection Callback E.g. if you declare OnMouseMove on a class and that function gets called whenever the mouse moves
  • **Callb
@karlgluck
karlgluck / CollectTwitchGameStats.py
Created April 18, 2015 15:59
Script that can be run with crontab to collect viewer & channel statistics about the top games on Twitch.TV into CSV files
import requests, json
import os
import re
import time
import datetime
data_dir = os.path.dirname(os.path.realpath(__file__)) + '/data'
api_url = 'https://api.twitch.tv/kraken/games/top?limit=100'

Keybase proof

I hereby claim:

  • I am karlgluck on github.
  • I am karlgluck (https://keybase.io/karlgluck) on keybase.
  • I have a public key whose fingerprint is 6797 F0BC 1431 E8CB 5E34 D924 B4F7 4EB0 FF8B 9035

To claim this, I am signing this object:

// this lets you write bits to a buffer and scan them back:
//
// char buffer[3];
// bitfield writer(buffer);
// writer.write(2,6); // write "2" as a 6-bit uint
// writer.write(9,4); // write "9" as a 4-bit uint
// writer.write(0,1); // etc
// writer.write(1,2);
// size_t bytes = writer.bytes(buffer); // bytes == 2