deltaluca / nape

Haxe/AS3 Physics Engine
http://napephys.com
Other
542 stars 77 forks source link

CbType created before a Space doesn't work #81

Closed bartvanheukelom closed 10 years ago

bartvanheukelom commented 10 years ago

A CbType that is constructed before a Space is constructed will have id = NaN and not function properly in callbacks. This adds inconvenience because you can't use a construct like this:

class Platform {
    public static var CBTYPE:CbType;
    public static function __init__():Void {
        CBTYPE = new CbType();
    }
}
deltaluca commented 10 years ago

This has nothing to do with a Space being constructed or not, this is to do with constructing objects during a static __init__ which runs 'really' early during boot time, before anything else is initialised (in this case the id counter for CbType itself)

bartvanheukelom commented 10 years ago

I've tried finding where the id counter is initialised but couldn't find it due to (presumably) a lack of Caxe knowledge. However, could it not be initialised in a static __init__ also?

bartvanheukelom commented 10 years ago

Never mind, I've found that one can simply use public static var CBTYPE = new CbType();. I hadn't tried because previously when I tried something similar Haxe had complained that field initialisation should be a constant expression. However, it works, and is apparently executed later than __init__. Sorry, kinda new to Haxe :*) Edit: Yeah, I should have RTFM: "Then, all static function init method body code is generated. [...] static variables have not yet been initialized; these functions should only be used to perform very low level initializations.". I ass-umed they worked like Java's static {} blocks.