Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Bitwise operator-safe state values. #1

merged 2 commits into from

2 participants


Hey Mike, here's my changes. I added the exception to warn users if they try to make more than 30 states and cleaned up some variables in the loops for efficiency. Plus, of course, the power-of-2 based state values. You should be able to hit "merge" (or similar) on this pull request and it will merge my changes into your master branch. Thanks again, and great work!


xdissent added some commits
@xdissent xdissent Added bitwise operator compatible state values.
Fixed unscoped iterator variable.
@xdissent xdissent Added 30 symbol limit exception.
Optimized array length and index lookups in loops.
Replaced name test against empty string with boolean test (empty string evaluates to false).

Yeah, unfortunately Javascript uses 64bit double precision floats to represent all numbers. We're lucky they're implemented the way they are or we might not even get the familiar 32bit integer style lower bits, which would mean no practical binary operations at all. With double floats, you can represent some very large integers - far larger than 2^30 (which is where my symbols implementation starts to choke). The problem is that the underlying bits don't increment in powers of 2 anymore, which means your bitwise operations to determine which state(s) you're in will break down at 2^31 as you define your 31st state. =/


Dropping the singleton requirement does sound like a good idea, but only in the context that you could apply a different set of symbols to different base classes. For example, I'd like to have some global game states (indicating the current level or loading state perhaps) and some entity states (running, shooting etc) that don't necessarily even make sense together and shouldn't be available on both the ig.Game and ig.Entity classes. That being said, there is also a desire to share states with subclasses, which could optionally add additional states (only available to that subclass or subclasses of it). Since the only source of potential confusion is when a subclass accidentally tries to compare its state to a state on the subclass, but referenced on the parent class:

new ig.Symbols("RUNNING JUMPING", [ig.Entity]); // Or whatever
new ig.Symbols("SHOOTING", [EntityEnemy]);

var e =, 0, 0);
e.state = ig.Entity.RUNNING;        // Fine
e.state = ig.Entity.SHOOTING;       // Error - only defined on subclass
e.state = EntityEnemy.SHOOTING;     // Error - not an instance of EntityEnemy

var ee =, 0, 0);
ee.state = EntityEnemy.SHOOTING;    // Fine
ee.state = EntityEnemy.RUNNING;     // Also fine - inherited
ee.state = ig.Entity.RUNNING;       // Ok (and bit compatible) but confusing
ee.state = ig.Entity.SHOOTING;      // Error - undefined on ig.Entity

But that also brings up the question of where the states should be defined, because we're putting the onus on the class to know which states are available to it through the inheritance chain (and to add any it needs). It doesn't make sense to declare states in the init() because they'd be redeclared with every instantiation of the class.

I'm gonna marinate on some of these points for a while and see what I can come up with. I know we're probably over thinking everything at this point, but it also feels like we could be close to an invaluable tool for Impact games. We'll see which it is I guess ;-)

@boneheadmed boneheadmed merged commit 02767b2 into boneheadmed:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 10, 2011
  1. @xdissent

    Added bitwise operator compatible state values.

    xdissent committed
    Fixed unscoped iterator variable.
  2. @xdissent

    Added 30 symbol limit exception.

    xdissent committed
    Optimized array length and index lookups in loops.
    Replaced name test against empty string with boolean test (empty string evaluates to false).
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 7 deletions.
  1. +13 −7 symbols.js
20 symbols.js
@@ -27,15 +27,21 @@ ig.Symbols = ig.Class.extend({
if (typeof classes == "undefined")
classes = [ig.Entity];
- var nameArray = names.split(" ");
+ var nameArray = names.split(" "),
+ nameArrayLen = nameArray.length;
+ if (nameArrayLen > 30) {
+ throw("Error: ig.Symbols allows a maximum of 30 symbol definitions.");
+ }
- for(i =0; i < classes.length; i++){
+ for(var i = 0; i < classes.length; i++){
var currentClass = classes[i];
- var j = 0;
- for(j = 0; j < nameArray.length; j++){
- nameArray[j].replace(/^\s+|\s+$/g,"");
- if (nameArray[j] != "")
- currentClass[nameArray[j]] = j;
+ for(var j = 0; j < nameArrayLen; j++){
+ var name = nameArray[j];
+ name.replace(/^\s+|\s+$/g, "");
+ if (name) {
+ currentClass[name] = 0x1 << j;
+ }
ig.Symbols.instance = this;
Something went wrong with that request. Please try again.